ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2052A - Adrenaline Rush
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
#define int long long 
#define  nl "\n"
#define f first
#define s second
const int mod = 1e9 + 7, inf = 1e18;
#define all(a) a.begin(),a.end()
#define forn(a, c) for (int (a) = 0; (a) < (c); (a)++) 
#define cin(v) for(auto &i:v){cin>>i;}
#define cout(v) for(auto &i:v){cout<<i<<" ";}cout<<nl;
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); pri(_it, args); cout<<nl;}
void pri(istream_iterator<string> it) {}
template<typename T, typename... Args>
void pri(istream_iterator<string> it, T a, Args... args) {cout << *it << "  " << a << nl;pri(++it, args...);}
string dtobin(int n,int k = 0){string res = "";while(n>0 || k>0){k--;if(n&1){res += "1";}else{res += "0";}n>>= 1;}reverse(res.begin(),res.end());return res;}
int binexp(int a,int b){if(b==0){return 1;}int res = 1;while(b){if(b&1){res*= a;}b>>=1; a *= a;}return res;}
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

void solve()
{
    int n;cin>>n;std::vector<int> v(n+1), id(n+1);
    forn(i, n)cin>>v[i+1];
    if(n == 1){cout<<0<<nl;return;}
    forn(i, n){
        id[v[i+1]]  = i+1;
    }
    vector<pair<int,int>>ans;
    for(int i = 1; i <= n;i++)
    {   
        int ps = id[i];
        for(int j = ps + 1; j <= n;j++)
        {
            ans.push_back({v[j], v[ps]});
        }
        for(int j = ps + 1; j <= n;j++)
        {
            ans.push_back({v[ps], v[j]});
        }

        for(int j = ps - 1 ;j >= i;j--)
        {
            ans.push_back({i, v[j]});
            id[v[j]]++;
            v[j+1] = v[j]; 
        }
    }
    reverse(all(ans));
    cout<<ans.size()<<nl;

    for(auto &x : ans)cout<<x.f<<" "<<x.s<<nl;
}
signed main()
{
    (ios_base::sync_with_stdio(false), cin.tie(NULL));
    auto begin = std::chrono::high_resolution_clock::now();
    int t;t = 1;
    // cin>>t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: PyPy 3-64

Previous submission (verdict: Wrong answer on test 4):
import sys
from queue import deque
from bisect import bisect_left, bisect_right
from heapq import *
from collections import *

sys.setrecursionlimit(10**5)

input = lambda: sys.stdin.readline().rstrip('\r\n')
iint = lambda: int(input())
mint = lambda: map(int, input().split())
aint = lambda: list(map(int, input().split()))

MOD = 10**9 + 7

def solve():
    n = iint()
    a = aint()
    rank = [0] * (n + 1)
    for i in range(n):
        rank[a[i]] = i+1
   
    mx_len = n * (n-1) // 2
    curr = list(range(n, 0, -1))
    sq = []
    for i in range(n-1, -1, -1):
        j = i
        while j>0 and rank[curr[j]] < rank[curr[j-1]]:
            sq.append([curr[j], curr[j-1]])
            curr[j], curr[j-1] = curr[j-1], curr[j]
            mx_len += 1
            j -= 1

    print(mx_len)
    for i in range(1, n+1):
        for j in range(i+1, n+1):
            print(j, i)
    for a, b in sq:
        print(a, b)

solve()

Submission (verdict: Accepted):
import sys
from queue import deque
from bisect import bisect_left, bisect_right
from heapq import *
from collections import *

sys.setrecursionlimit(10**5)

input = lambda: sys.stdin.readline().rstrip('\r\n')
iint = lambda: int(input())
mint = lambda: map(int, input().split())
aint = lambda: list(map(int, input().split()))

MOD = 10**9 + 7

def solve():
    n = iint()
    a = aint()
    rank = [0] * (n + 1)
    for i in range(n):
        rank[a[i]] = i+1
   
    mx_len = n * (n-1) // 2
    curr = list(range(n, 0, -1))
    sq = []
    for i in range(n):
        j = i
        while j>0 and rank[curr[j]] < rank[curr[j-1]]:
            sq.append([curr[j], curr[j-1]])
            curr[j], curr[j-1] = curr[j-1], curr[j]
            mx_len += 1
            j -= 1

    print(mx_len)
    for i in range(1, n+1):
        for j in range(i+1, n+1):
            print(j, i)
    for a, b in sq:
        print(a, b)

solve()
Problem: 2052A - Adrenaline Rush
Language: PyPy 3-64


Submission (verdict: Wrong answer on test 1):
import sys
from queue import deque
from bisect import bisect_left, bisect_right
from heapq import *
from collections import *

sys.setrecursionlimit(10**5)

input = lambda: sys.stdin.readline().rstrip('\r\n')
iint = lambda: int(input())
mint = lambda: map(int, input().split())
aint = lambda: list(map(int, input().split()))

MOD = 10**9 + 7

def solve():
    n = iint()
    a = aint()
    rank = [0] * (n + 1)
    for i in range(n):
        rank[a[i]] = i+1
   
    mx_len = n * (n-1) // 2
    curr = list(range(n, 0, -1))
    sq = []
    for i in range(n-1, -1, -1):
        j = i
        while j<n-1 and rank[curr[j]] > rank[curr[j+1]]:
            sq.append([curr[j], curr[j+1]])
            curr[j], curr[j+1] = curr[j+1], curr[j]
            mx_len += 1
            j += 1

    print(mx_len)
    for i in range(1, n+1):
        for j in range(i+1, n+1):
            print(i, j)
    for a, b in sq:
        print(a, b)

solve()
Problem: 2052A - Adrenaline Rush
Language: PyPy 3-64

Previous submission (verdict: Wrong answer on test 1):
import sys
from queue import deque
from bisect import bisect_left, bisect_right
from heapq import *
from collections import *

sys.setrecursionlimit(10**5)

input = lambda: sys.stdin.readline().rstrip('\r\n')
iint = lambda: int(input())
mint = lambda: map(int, input().split())
aint = lambda: list(map(int, input().split()))

MOD = 10**9 + 7

def solve():
    n = iint()
    a = aint()
    rank = [0] * (n + 1)
    for i in range(n):
        rank[a[i]] = i+1
   
    mx_len = n * (n-1) // 2
    curr = list(range(n, 0, -1))
    sq = []
    for i in range(n-1, -1, -1):
        j = i
        while j<n-1 and rank[curr[j]] > rank[curr[j+1]]:
            sq.append([curr[j], curr[j+1]])
            curr[j], curr[j+1] = curr[j+1], curr[j]
            mx_len += 1
            j += 1

    print(mx_len)
    for i in range(1, n+1):
        for j in range(i+1, n+1):
            print(i, j)
    for a, b in sq:
        print(a, b)

solve()

Submission (verdict: Wrong answer on test 4):
import sys
from queue import deque
from bisect import bisect_left, bisect_right
from heapq import *
from collections import *

sys.setrecursionlimit(10**5)

input = lambda: sys.stdin.readline().rstrip('\r\n')
iint = lambda: int(input())
mint = lambda: map(int, input().split())
aint = lambda: list(map(int, input().split()))

MOD = 10**9 + 7

def solve():
    n = iint()
    a = aint()
    rank = [0] * (n + 1)
    for i in range(n):
        rank[a[i]] = i+1
   
    mx_len = n * (n-1) // 2
    curr = list(range(n, 0, -1))
    sq = []
    for i in range(n-1, -1, -1):
        j = i
        while j>0 and rank[curr[j]] < rank[curr[j-1]]:
            sq.append([curr[j], curr[j-1]])
            curr[j], curr[j-1] = curr[j-1], curr[j]
            mx_len += 1
            j -= 1

    print(mx_len)
    for i in range(1, n+1):
        for j in range(i+1, n+1):
            print(j, i)
    for a, b in sq:
        print(a, b)

solve()
Problem: 2057B - Gorilla and the Exam
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;

#define _DvlNus_ int main
#define yone ios_base::sync_with_stdio (0); cin.tie (0);
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pii pair <int, int>

const int maxn = 1e5 + 5;
const int inf = 1e9 + 7;

int t, n, k;

_DvlNus_() {
	yone
	
	cin >> t;
	while (t--) {
		cin >> n >> k;
		map<int, int> mp;
		vector<int> ans;
		
		for (int i = 0; i < n; i++) {
			int x; cin >> x;
			mp[x]++;
		}
	
		for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
			ans.pb(it->se);
			
		sort(ans.begin(), ans.end());
			
		int l = 0, r = ans.size() - 1;
		int res = 0;
		
		while (l <= r) {
			int tong = 0;
			while (tong <= k) {
				if (tong + ans[l] > k) break;
				tong += ans[l];
				l++;
			}
			res++; r--;
		}
		
		cout << res << '\n';
		
	}
}
Problem: 2057B - Gorilla and the Exam
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;

#define _DvlNus_ int main
#define yone ios_base::sync_with_stdio (0); cin.tie (0);
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pii pair <int, int>

const int maxn = 2e5 + 5;
const int inf = 1e9 + 7;

_DvlNus_() {
	yone
	
	int t; cin >> t;
	while (t--) {
		int n, k;
		cin >> n >> k;
		map<int, int> mp;
		vector<int> freq;
		
		for (int i = 0; i < n; i++) {
			int x; cin >> x;
			mp[x]++;
		}
		
		for (pair<int, int> x : mp) 
			freq.pb(x.se);
		
		sort(freq.begin(), freq.end());
		
		int ans = freq.size();
		
		for (int i = 0; i < freq.size(); i++) {
			if (k >= freq[i]) {
				k -= freq[i];
				ans--;
			} else break;
		}
		
		if (ans == 0) cout << 1;
		else cout << ans << '\n';
	}
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

#define _DvlNus_ int main
#define yone ios_base::sync_with_stdio (0); cin.tie (0);
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pii pair <int, int>

const int maxn = 2e5 + 5;
const int inf = 1e9 + 7;

_DvlNus_() {
	yone
	
	int t; cin >> t;
	while (t--) {
		int n, k;
		cin >> n >> k;
		map<int, int> mp;
		vector<int> freq;
		
		for (int i = 0; i < n; i++) {
			int x; cin >> x;
			mp[x]++;
		}
		
		for (pair<int, int> x : mp) 
			freq.pb(x.se);
		
		sort(freq.begin(), freq.end());
		
		int ans = freq.size();
		
		for (int i = 0; i < freq.size(); i++) {
			if (k >= freq[i]) {
				k -= freq[i];
				ans--;
			} else break;
		}
		
		if (ans == 0) cout << 1 << '\n';
		else cout << ans << '\n';
	}
}
Problem: 2057B - Gorilla and the Exam
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;

#define _DvlNus_ int main
#define yone ios_base::sync_with_stdio (0); cin.tie (0);
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pii pair <int, int>

const int maxn = 1e5 + 5;
const int inf = 1e9 + 7;

int t, n, k;

_DvlNus_() {
	yone
	
	cin >> t;
	while (t--) {
		cin >> n >> k;
		map<int, int> mp;
		vector<int> ans;
		
		for (int i = 0; i < n; i++) {
			int x; cin >> x;
			mp[x]++;
		}
	
		for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
			ans.pb(it->se);
			
		sort(ans.begin(), ans.end());
			
		int l = 0, r = ans.size() - 1;
		int res = 0;
		
		while (l <= r) {
			int tong = 0;
			while (tong <= k) {
				if (tong + ans[l] > k) break;
				tong += ans[l];
				l++;
			}
			res++; r--;
		}
		
		cout << res << '\n';
		
	}
}


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;

#define _DvlNus_ int main
#define yone ios_base::sync_with_stdio (0); cin.tie (0);
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pii pair <int, int>

const int maxn = 2e5 + 5;
const int inf = 1e9 + 7;

_DvlNus_() {
	yone
	
	int t; cin >> t;
	while (t--) {
		int n, k;
		cin >> n >> k;
		map<int, int> mp;
		vector<int> freq;
		
		for (int i = 0; i < n; i++) {
			int x; cin >> x;
			mp[x]++;
		}
		
		for (pair<int, int> x : mp) 
			freq.pb(x.se);
		
		sort(freq.begin(), freq.end());
		
		int ans = freq.size();
		
		for (int i = 0; i < freq.size(); i++) {
			if (k >= freq[i]) {
				k -= freq[i];
				ans--;
			} else break;
		}
		
		if (ans == 0) cout << 1;
		else cout << ans << '\n';
	}
}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 1):
#include <bits/stdc++.h>



using namespace std;



int main()

{

    ios::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

    int t;

    cin >> t;

    while(t != 0){

        int n;

        cin >> n;

        int a, b;

        cin >> a;

        int check = 1;

        for(int i = 0; i < n - 1; i++){

            cin >> b;

            if(b >= a){

                a = b - a;

            }

            else{

                check = 0;

            }

        }

        if(check == 1) cout << "YES" << endl;

        else cout << "NO" << endl;

    }

    return 0;

}

Submission (verdict: Time limit exceeded on test 1):
#include <bits/stdc++.h>



using namespace std;



int main()

{

    ios::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

    int t;

    cin >> t;

    while(t != 0){

        int n;

        cin >> n;

        int arr[200000];

        int check = 1;

        for(int i = 0; i < n; i++){

            cin >> arr[i];

        }

        for(int i = 0; i < n - 1; i++){

            if(arr[i] <= arr[i + 1]){

                arr[i + 1] -= arr[i];

            }

            else{

                check = 0;

                break;

            }

        }

        if(check == 1) cout << "YES" << endl;

        else cout << "NO" << endl;

    }

    return 0;

}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 1):
#include <bits/stdc++.h>



using namespace std;



int main()

{

    ios::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

    int t;

    cin >> t;

    while(t != 0){

        int n;

        cin >> n;

        vector<int> v;

        int a;

        int check = 1;

        for(int i = 0; i < n; i++){

            cin >> a;

            v.push_back(a);

        }

        for(int i = 0; i < n - 1; i++){

            if(v[i] <= v[i + 1]){

                v[i + 1] -= v[i];

            }

            else{

                check = 0;

                break;

            }

        }

        if(check == 1) cout << "YES" << endl;

        else cout << "NO" << endl;

    }

    return 0;

}

Submission (verdict: Accepted):
#include <bits/stdc++.h>



using namespace std;



int main()

{

    ios::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

    int t;

    cin >> t;

    while(t != 0){

        -- t;

        int n;

        cin >> n;

        int a, b, check = 1;

        cin >> a;

        for(int i = 0; i < n - 1; i++){

            cin >> b;

            if(a <= b){

                a = b - a;

            }

            else{

                check = 0;

            }

        }

        if(check == 1) cout << "YES" << endl;

        else cout << "NO" << endl;

    }

    return 0;

}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 1):
#include <bits/stdc++.h>



using namespace std;



int main()

{

    ios::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

    int t;

    cin >> t;

    while(t != 0){

        int n;

        cin >> n;

        int arr[200000];

        int check = 1;

        for(int i = 0; i < n; i++){

            cin >> arr[i];

        }

        for(int i = 0; i < n - 1; i++){

            if(arr[i] <= arr[i + 1]){

                arr[i + 1] -= arr[i];

            }

            else{

                check = 0;

                break;

            }

        }

        if(check == 1) cout << "YES" << endl;

        else cout << "NO" << endl;

    }

    return 0;

}

Submission (verdict: Time limit exceeded on test 1):
#include <bits/stdc++.h>



using namespace std;



int main()

{

    ios::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

    int t;

    cin >> t;

    while(t != 0){

        int n;

        cin >> n;

        vector<int> v;

        int a;

        int check = 1;

        for(int i = 0; i < n; i++){

            cin >> a;

            v.push_back(a);

        }

        for(int i = 0; i < n - 1; i++){

            if(v[i] <= v[i + 1]){

                v[i + 1] -= v[i];

            }

            else{

                check = 0;

                break;

            }

        }

        if(check == 1) cout << "YES" << endl;

        else cout << "NO" << endl;

    }

    return 0;

}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)


Submission (verdict: Time limit exceeded on test 1):
#include <bits/stdc++.h>



using namespace std;



int main()

{

    ios::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

    int t;

    cin >> t;

    while(t != 0){

        int n;

        cin >> n;

        int a, b;

        cin >> a;

        int check = 1;

        for(int i = 0; i < n - 1; i++){

            cin >> b;

            if(b >= a){

                a = b - a;

            }

            else{

                check = 0;

            }

        }

        if(check == 1) cout << "YES" << endl;

        else cout << "NO" << endl;

    }

    return 0;

}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

using namespace std;

int main() {
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<vector<int>> adjs(n);
		vector<pair<int, int>> edgs(n, pair<int, int>(0, 0));
		for (int i = 0; i < n; i++) edgs[i].second = i;
		for (int i = 0; i < n-1; i++) {
			int u, v; cin >> u >> v; u--; v--;
			adjs[u].push_back(v); adjs[v].push_back(u);
			edgs[u].first++; edgs[v].first++;
		}
		sort(edgs.begin(), edgs.end());
		if (edgs[n-1].first != edgs[n-2].first) {
				int i = 0;
				int ss = edgs[n-2].first;
				while ((n-i-2) >= 0 && edgs[n-i-2].first == ss) i++;
				auto& _ = adjs[edgs[n-1].second];
				sort(_.begin(), _.end());
				bool found = false;
				for (int j = n-2; j > (n-2-i); j--) {
					if (!binary_search(_.begin(), _.end(), edgs[j].second)) {
						found = true;
						cout << (edgs[n-1].first + edgs[n-2].first - 1) << '\n';
						break;
					}
				}
				if (!found) cout << (edgs[n-1].first + edgs[n-2].first - 2) << '\n';
		} else {
			int i = 0;
			int ss = edgs[n-1].first;
			while ((n-i-1) >= 0 && edgs[n-i-1].first == ss) i++;
			if (i >= 3) {
				cout << (ss + ss - 2) << '\n';
			} else {
				auto& _ = adjs[edgs[n-1].second];
				if (find(_.begin(), _.end(), edgs[n-2].second) == _.end()) {
					cout << (edgs[n-1].first + edgs[n-2].first - 1) << '\n';
				} else {
					cout << (edgs[n-1].first + edgs[n-2].first - 2) << '\n';
				}
			}

		}
	}

	return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>

using namespace std;

int main() {
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<vector<int>> adjs(n);
		vector<pair<int, int>> edgs(n, pair<int, int>(0, 0));
		for (int i = 0; i < n; i++) edgs[i].second = i;
		for (int i = 0; i < n-1; i++) {
			int u, v; cin >> u >> v; u--; v--;
			adjs[u].push_back(v); adjs[v].push_back(u);
			edgs[u].first++; edgs[v].first++;
		}
		sort(edgs.begin(), edgs.end());
		if (edgs[n-1].first != edgs[n-2].first) {
				int i = 0;
				int ss = edgs[n-2].first;
				while ((n-i-2) >= 0 && edgs[n-i-2].first == ss) i++;
				auto& _ = adjs[edgs[n-1].second];
				sort(_.begin(), _.end());
				bool found = false;
				for (int j = n-2; j > (n-2-i); j--) {
					if (!binary_search(_.begin(), _.end(), edgs[j].second)) {
						found = true;
						cout << (edgs[n-1].first + edgs[n-2].first - 1) << '\n';
						break;
					}
				}
				if (!found) cout << (edgs[n-1].first + edgs[n-2].first - 2) << '\n';
		} else {
			int i = 0;
			int ss = edgs[n-1].first;
			while ((n-i-1) >= 0 && edgs[n-i-1].first == ss) i++;
			if (i >= 3) {
				cout << (ss + ss - 1) << '\n';
			} else {
				auto& _ = adjs[edgs[n-1].second];
				if (find(_.begin(), _.end(), edgs[n-2].second) == _.end()) {
					cout << (edgs[n-1].first + edgs[n-2].first - 1) << '\n';
				} else {
					cout << (edgs[n-1].first + edgs[n-2].first - 2) << '\n';
				}
			}

		}
	}

	return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Compilation error):
#include <bits/stdc++.h>

using namespace std;

int main() {
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<vector<int>> adjs(n);
		vector<pair<int, int>> edgs(n, pair<int, int>(0, 0));
		for (int i = 0; i < n; i++) edgs[i].second = i;
		for (int i = 0; i < n-1; i++) {
			int u, v; cin >> u >> v; u--; v--;
			adjs[u].push_back(v); adjs[v].push_back(u);
			edgs[u].first++; edgs[v].first++;
		}
		sort(edgs.begin(), edgs.end());
		if (edgs[n-1].first != edgs[n-2].first) {
				int i = 0;
				int ss = edgs[n-2].first;
				while ((n-i-2) >= 0 && edgs[n-i-2].second == ss) i++;
				auto& _ = adjs[edgs[n-1].second];
				_.sort();
				bool found = false;
				for (int j = n-2; j > (n-2-i); j--) {
					if (!binary_search(_.begin(), _.end(), edgs[j].second)) {
						found = true;
						cout << (edgs[n-1].first + edgs[n-2].first - 1) << '\n';
					}
				}
				if (!found) cout << (edgs[n-1].first + edgs[n-2].first - 2) << '\n';
		} else {
			int i = 0;
			int ss = edgs[n-1].first;
			while ((n-i-1) >= 0 && edgs[n-i-1].first == ss) i++;
			if (i >= 3) {
				cout << (ss + ss - 2) << '\n';
			} else {
				auto& _ = adjs[edgs[n-1].second];
				if (find(_.begin(), _.end(), edgs[n-2].second) == _.end()) {
					cout << (edgs[n-1].first + edgs[n-2].first - 1) << '\n';
				} else {
					cout << (edgs[n-1].first + edgs[n-2].first - 2) << '\n';
				}
			}

		}
	}

	return 0;
}


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

using namespace std;

int main() {
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<vector<int>> adjs(n);
		vector<pair<int, int>> edgs(n, pair<int, int>(0, 0));
		for (int i = 0; i < n; i++) edgs[i].second = i;
		for (int i = 0; i < n-1; i++) {
			int u, v; cin >> u >> v; u--; v--;
			adjs[u].push_back(v); adjs[v].push_back(u);
			edgs[u].first++; edgs[v].first++;
		}
		sort(edgs.begin(), edgs.end());
		if (edgs[n-1].first != edgs[n-2].first) {
				int i = 0;
				int ss = edgs[n-2].first;
				while ((n-i-2) >= 0 && edgs[n-i-2].first == ss) i++;
				auto& _ = adjs[edgs[n-1].second];
				sort(_.begin(), _.end());
				bool found = false;
				for (int j = n-2; j > (n-2-i); j--) {
					if (!binary_search(_.begin(), _.end(), edgs[j].second)) {
						found = true;
						cout << (edgs[n-1].first + edgs[n-2].first - 1) << '\n';
						break;
					}
				}
				if (!found) cout << (edgs[n-1].first + edgs[n-2].first - 2) << '\n';
		} else {
			int i = 0;
			int ss = edgs[n-1].first;
			while ((n-i-1) >= 0 && edgs[n-i-1].first == ss) i++;
			if (i >= 3) {
				cout << (ss + ss - 2) << '\n';
			} else {
				auto& _ = adjs[edgs[n-1].second];
				if (find(_.begin(), _.end(), edgs[n-2].second) == _.end()) {
					cout << (edgs[n-1].first + edgs[n-2].first - 1) << '\n';
				} else {
					cout << (edgs[n-1].first + edgs[n-2].first - 2) << '\n';
				}
			}

		}
	}

	return 0;
}