ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2057B - Gorilla and the Exam
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 11):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
#define vi vector<int>
#define MP make_pair
#define REP(i, a, b) for(int i=a;i<b;i++)

void solve() {
    int n, k;
    cin>>n>>k;

    unordered_map<int, int> mp;

    vi arr(n);
    REP(i, 0, n) {
        cin>>arr[i];
        mp[arr[i]]++;
    }
    
    vi freq;

    for(auto it : mp) {
        freq.push_back(it.second);
    }
    int unique = freq.size();

    sort(freq.begin(), freq.end());

    REP(i, 0, freq.size()) {
        if(freq[i] > k)
            break;
        k -= freq[i];
        unique--;
    }

    cout << (unique == 0 ? 1 : unique) << "\n";
}

int main()
{
    // #ifndef ONLINE_JUDGE
    //   freopen("input.txt", "r", stdin);
    //   freopen("output.txt", "w", stdout);
    // #endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


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

typedef long long ll;
typedef pair<int, int> pi;
#define vi vector<int>
#define MP make_pair
#define REP(i, a, b) for(int i=a;i<b;i++)

void solve() {
    int n, k;
    cin>>n>>k;

    vi arr(n);
    REP(i, 0, n) {
        cin>>arr[i];
    }

    sort(arr.begin(), arr.end());
    vi freq = {1};
    REP(i, 1, n) {
        if(arr[i] == arr[i-1])
            freq.back()++;
        else {
            freq.push_back(1);
        }
    }

    sort(freq.begin(), freq.end());
    int m = freq.size();
    REP(i, 0, m) {
        if(freq[i] > k) {
            cout << m - i << "\n";
            return;
        }
        k -= freq[i];
    }
    cout << 1 << "\n";
}

int main()
{
    // #ifndef ONLINE_JUDGE
    //   freopen("input.txt", "r", stdin);
    //   freopen("output.txt", "w", stdout);
    // #endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 11):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
#define vi vector<int>
#define MP make_pair
#define REP(i, a, b) for(int i=a;i<b;i++)

void solve() {
    int n, k;
    cin>>n>>k;

    unordered_map<int, int> mp;

    vi arr(n);
    REP(i, 0, n) {
        cin>>arr[i];
        mp[arr[i]]++;
    }
    
    priority_queue<int, vector<int>, greater<int> > pq;

    for(auto it : mp) {
        pq.push(it.second);
    }

    while((!pq.empty()) && k) {
        int temp = pq.top();
        if(temp > k)
            break;
        k -= temp;
        pq.pop();
    }

    if(pq.empty())
        cout << 1 << "\n";
    else
        cout << (pq.size()) << "\n";
}

int main()
{
    // #ifndef ONLINE_JUDGE
    //   freopen("input.txt", "r", stdin);
    //   freopen("output.txt", "w", stdout);
    // #endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


Submission (verdict: Time limit exceeded on test 11):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
#define vi vector<int>
#define MP make_pair
#define REP(i, a, b) for(int i=a;i<b;i++)

void solve() {
    int n, k;
    cin>>n>>k;

    unordered_map<int, int> mp;

    vi arr(n);
    REP(i, 0, n) {
        cin>>arr[i];
        mp[arr[i]]++;
    }
    
    vi freq;

    for(auto it : mp) {
        freq.push_back(it.second);
    }
    int unique = freq.size();

    sort(freq.begin(), freq.end());

    REP(i, 0, freq.size()) {
        if(freq[i] > k)
            break;
        k -= freq[i];
        unique--;
    }

    cout << (unique == 0 ? 1 : unique) << "\n";
}

int main()
{
    // #ifndef ONLINE_JUDGE
    //   freopen("input.txt", "r", stdin);
    //   freopen("output.txt", "w", stdout);
    // #endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Time limit exceeded on test 11):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
#define vi vector<int>
#define MP make_pair
#define REP(i, a, b) for(int i=a;i<b;i++)

void solve() {
    int n, k;
    cin>>n>>k;

    unordered_map<int, int> mp;

    vi arr(n);
    REP(i, 0, n) {
        cin>>arr[i];
        mp[arr[i]]++;
    }
    
    priority_queue<int, vector<int>, greater<int> > pq;

    for(auto it : mp) {
        pq.push(it.second);
    }

    while((!pq.empty()) && k) {
        int temp = pq.top();
        if(temp > k)
            break;
        k -= temp;
        pq.pop();
    }

    if(pq.empty())
        cout << 1 << "\n";
    else
        cout << (pq.size()) << "\n";
}

int main()
{
    // #ifndef ONLINE_JUDGE
    //   freopen("input.txt", "r", stdin);
    //   freopen("output.txt", "w", stdout);
    // #endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
Problem: 2063B - Subsequence Update
Language: C++23 (GCC 14-64, msys2)


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

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef vector<vector<long long> > vvll;
typedef vector<vector<string> > vvs;
typedef vector<vector<double> > vvd;
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i >= (b); i--)
// #define FOR2D(i, j, n, m) for (int i = 0; i < (n); i++) for (int j = 0; j < (m); j++)
#define FOR2D(i, j, start_i, end_i, start_j, end_j) \
    for (int i = (start_i); i < (end_i); ++i)       \
        for (int j = (start_j); j < (end_j); ++j)
#define FORE(item, container) for (auto &item : container)
#define FORSTEP(i, a, b, step) for (int i = (a); i < (b); i += (step))
#define NL std::cout << "\n"
#define MOD 1000000007

void solve()
{
    int n, l, r;
    cin>>n>>l>>r;
    vi arr(n);
    FOR(i, 0, n) {
        cin>>arr[i];
    }
    priority_queue<int> pq;
    priority_queue<int, vector<int>, greater<int> > a, b;
    FOR(i, l-1, r)
        pq.push(arr[i]);
    FOR(i, 0, l-1)
        a.push(arr[i]);
    FOR(i, r, n)
        b.push(arr[i]);
    
    int sum = 0;
    int minSum = INT_MAX;
    while(!pq.empty()) {
        if(a.empty()) {
            sum += pq.top();
        }
        else {
            sum += min(a.top(), pq.top());
            a.pop();
        }
        pq.pop();
    }
    minSum = min(minSum, sum);

    FOR(i, l-1, r)
        pq.push(arr[i]);

    sum = 0;
    while(!pq.empty()) {
        if(b.empty()) {
            sum += pq.top();
        }
        else {
            sum += min(b.top(), pq.top());
            b.pop();
        }
        pq.pop();
    }
    minSum = min(minSum, sum);

    cout << minSum;
    NL;
}

int main()
{
    // #ifndef ONLINE_JUDGE
    //   freopen("input.txt", "r", stdin);
    //   freopen("output.txt", "w", stdout);
    // #endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 4):
#include<bits/stdc++.h>
using namespace std; 
#define int long long
#define endl '\n'
#define inf 0x3f3f3f3f3f3f3f3f
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N = 1e6+10;
const int mod=998244353;
int h[N];
int a[N],b[N];
void solve() {
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		b[i]=i;
	}
	vector<PII>v;
	if(a[n]==n){
		for(int i=1;i<=n;i++){
			if(a[i]!=b[i]){
				int j=a[i];
				for(int l=1;l<=(j-i+1);l++){
					v.push_back({b[j],b[j-1]});
					swap(b[j],b[j-1]);
					j--;
				}
			}
		}
		for(int i=n;i>1;i--){
			v.push_back({b[i],b[i-1]});
			v.push_back({b[i-1],b[i]});
		}
	}
	else{
		for(int i=1;i<=n;i++){
			if(a[i]!=b[i]){
				int j=a[i];
				for(int l=1;l<=(j-i+1);l++){
					v.push_back({b[j],b[j-1]});
					swap(b[j],b[j-1]);
					j--;
				}
			}
		}
		for(int i=n-1;i>1;i--){
			v.push_back({b[i],b[i-1]});
			v.push_back({b[i-1],b[i]});
		}
	}
	cout<<v.size()<<endl;
	for(auto l : v){
		cout<<l.first<<" "<<l.second<<endl;
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T=1;
	//cin>>T;
	while(T--) {
		solve();
	}
	return 0;
}

Submission (verdict: Wrong answer on test 5):
#include<bits/stdc++.h>
using namespace std; 
#define int long long
#define endl '\n'
#define inf 0x3f3f3f3f3f3f3f3f
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N = 1e6+10;
const int mod=998244353;
int h[N];
int a[N],b[N];
void solve() {
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		b[i]=i;
	}
	vector<PII>v;
	if(a[n]==n){
		for(int i=1;i<=n;i++){
			if(a[i]!=b[i]){
				int j=a[i];
				for(int l=1;l<=(j-i+1);l++){
					v.push_back({b[j],b[j-1]});
					swap(b[j],b[j-1]);
					j--;
				}
			}
		}
		for(int i=n;i>1;i--){
			int j=i;
			while(1){
				v.push_back({b[j],b[j-1]});
				swap(b[j],b[j-1]);
				j--;
				if(j==1){
					break;
				}
			}
//			cout<<b[j]<<" ----\n";
			while(1){
				v.push_back({b[j+1],b[j]});
				swap(b[j],b[j+1]);
				j++;
				if(j==i){
					break;
				}
			}
		}
	}
	else{
		for(int i=1;i<=n;i++){
			if(a[i]!=b[i]){
				int j=a[i];
				for(int l=1;l<=(j-i+1);l++){
					v.push_back({b[j],b[j-1]});
					swap(b[j],b[j-1]);
					j--;
				}
			}
		}
		for(int i=n-1;i>1;i--){
			int j=i;
			while(1){
				v.push_back({b[j],b[j-1]});
				swap(b[j],b[j-1]);
				j--;
				if(j==1){
					break;
				}
			}
			while(1){
				v.push_back({b[j+1],b[j]});
				swap(b[j],b[j+1]);
				j++;
				if(j==i){
					break;
				}
			}
		}
	}
	cout<<v.size()<<endl;
	for(auto l : v){
		cout<<l.first<<" "<<l.second<<endl;
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T=1;
	//cin>>T;
	while(T--) {
		solve();
	}
	return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 4):
#include<bits/stdc++.h>
using namespace std; 
#define int long long
#define endl '\n'
#define inf 0x3f3f3f3f3f3f3f3f
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N = 1e6+10;
const int mod=998244353;
int h[N];
int a[N],b[N];
void solve() {
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		b[i]=i;
	}
	vector<PII>v;
	if(a[n]==n){
		for(int i=1;i<=n;i++){
			if(a[i]!=b[i]){
				int j=a[i];
				for(int l=1;l<=(j-i+1);l++){
					v.push_back({b[j],b[j-1]});
					swap(b[j],b[j-1]);
					j--;
				}
			}
		}
		for(int i=n;i>1;i--){
			v.push_back({b[i],b[i-1]});
			v.push_back({b[i-1],b[i]});
		}
	}
	else{
		for(int i=1;i<=n;i++){
			if(a[i]!=b[i]){
				int j=a[i];
				for(int l=1;l<=(j-i+1);l++){
					v.push_back({b[j],b[j-1]});
					swap(b[j],b[j-1]);
					j--;
				}
			}
		}
		for(int i=n-1;i>1;i--){
			v.push_back({b[i],b[i-1]});
			v.push_back({b[i-1],b[i]});
		}
	}
	cout<<v.size()<<endl;
	for(auto l : v){
		cout<<l.first<<" "<<l.second<<endl;
	}
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T=1;
	//cin>>T;
	while(T--) {
		solve();
	}
	return 0;
}
Problem: 2062C - Cirno and Operations
Language: C++23 (GCC 14-64, msys2)


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

    #define int long long
    #define ull unsigned long long
    #define all(x)  (x).begin(), (x).end()
    #define ed '\n' 
    #define sz size
    #define pb push_back
    #define vt vector
    #define pro priority_queue<int, vector<int>, greater<int> >
    #define TEST int t;  cin>>t;  while(t--)
    #define yes cout << "Yes" << '\n'
    #define no cout << "No" << '\n'
    typedef pair<int,int> PII;	
    void show1(string s1, int x) {cout<< s1 << " = "<< x << endl;}
    void show2(string s1, string s2, int x, int y) {cout<<s1 << " = "<< x << ' ' << s2 << " = " << y << endl;}
    void show3(string s1,string s2, string s3,int x, int y, int z) {cout<< s1 <<" = " << x << ' ' <<s2 << " = " << y << ' ' << s3 << " = "<<  z << endl;}
    void stop() {cout<<"***" << endl; return ;}
    // ---------------------------------------------------------------------------//
    
    // an - a1
    void solve() {
        int n;
        cin >> n;
        vt<int> a(n + 1);
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        if (n == 1) {
            cout << a[1] << ed;
            return ;
        }
        int ans = abs(a[1] - a[n]);
        int tot = n;
        for (int i = 0; i < n; i++) {
            int s = 0;
            for (int j = 1; j <= tot; j++) {
                s += a[j];
            } 
            ans = max(ans, abs(s));
            for (int j = 1; j < tot; j++) {
                a[j] = a[j + 1] - a[j];
            }
            tot--;
        }
        cout << ans << ed;
    }
    signed main() {
    	ios::sync_with_stdio(false);
		cin.tie(nullptr);
		cout.tie(nullptr);
        TEST
        solve();
        return 0;
    }
Problem: 2062C - Cirno and Operations
Language: PyPy 3-64


Submission (verdict: Wrong answer on test 1):
import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")

INF = int(1e18)


def solve():
    N = int(input())
    a = [int(v) for v in input().split()]
    ans = -INF
    while len(a) > 1:
        ans = max(ans, abs(sum(a)))
        a = [a[i] - a[i - 1] for i in range(1, len(a))]
    ans = max(ans, abs(a[0]))
    return ans


for _ in range(int(input())):
    # solve()
    print(solve())
    # print("YES" if solve() else "NO")
Problem: 2062C - Cirno and Operations
Language: PyPy 3-64

Previous submission (verdict: Wrong answer on test 2):
import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")

INF = int(1e18)


def solve():
    N = int(input())
    a = [int(v) for v in input().split()]
    if N == 1:
        return a[0]
    ans = -INF
    while len(a) > 1:
        ans = max(ans, abs(sum(a)))
        a = [a[i] - a[i - 1] for i in range(1, len(a))]
    ans = max(ans, abs(a[0]))
    return ans


for _ in range(int(input())):
    # solve()
    print(solve())
    # print("YES" if solve() else "NO")


Submission (verdict: Accepted):
import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")

INF = int(1e18)


def solve():
    N = int(input())
    a = [int(v) for v in input().split()]
    ans = sum(a)
    while len(a) > 1:
        ans = max(ans, abs(a[0] - a[-1]))
        a = [a[i] - a[i - 1] for i in range(1, len(a))]
    return ans


for _ in range(int(input())):
    # solve()
    print(solve())
    # print("YES" if solve() else "NO")
Problem: 2062C - Cirno and Operations
Language: PyPy 3-64

Previous submission (verdict: Wrong answer on test 1):
import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")

INF = int(1e18)


def solve():
    N = int(input())
    a = [int(v) for v in input().split()]
    ans = -INF
    while len(a) > 1:
        ans = max(ans, abs(sum(a)))
        a = [a[i] - a[i - 1] for i in range(1, len(a))]
    ans = max(ans, abs(a[0]))
    return ans


for _ in range(int(input())):
    # solve()
    print(solve())
    # print("YES" if solve() else "NO")


Submission (verdict: Wrong answer on test 2):
import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")

INF = int(1e18)


def solve():
    N = int(input())
    a = [int(v) for v in input().split()]
    if N == 1:
        return a[0]
    ans = -INF
    while len(a) > 1:
        ans = max(ans, abs(sum(a)))
        a = [a[i] - a[i - 1] for i in range(1, len(a))]
    ans = max(ans, abs(a[0]))
    return ans


for _ in range(int(input())):
    # solve()
    print(solve())
    # print("YES" if solve() else "NO")
Problem: 2057C - Trip to the Olympiad
Language: C++23 (GCC 14-64, msys2)

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

#define int long long

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int tc; cin >> tc;
    while (tc--) {
        int l, r; cin >> l >> r;
        vector<int> ans(3, l);

        for (int i = 30; i >= 0; i--) {
            int bit = 1LL << i;
            int lst = l / bit + 1;
            int pivot = lst * bit;
            if (r >= pivot) {
                ans[0] = pivot, ans[1] = pivot - 1;
                break;
            }
        }

        sort(ans.begin(), ans.end());

        for (auto &i: ans) cout << i << " "; cout << "\n";
    }
}


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

#define int long long

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int tc; cin >> tc;
    while (tc--) {
        int l, r; cin >> l >> r;
        vector<int> ans(3, l);

        for (int i = 30; i >= 0; i--) {
            int bit = 1LL << i;
            int lst = l / bit + 1;
            int pivot = lst * bit;
            if (r >= pivot) {
                ans[0] = pivot, ans[1] = pivot - 1;
                if (ans[1] == ans[2]) {
                    ans[2]++;
                }
                break;
            }
        }

        sort(ans.begin(), ans.end());

        for (auto &i: ans) cout << i << " "; cout << "\n";
    }
}
Problem: 2057C - Trip to the Olympiad
Language: C++23 (GCC 14-64, msys2)


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

#define int long long

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int tc; cin >> tc;
    while (tc--) {
        int l, r; cin >> l >> r;
        vector<int> ans(3, l);

        for (int i = 30; i >= 0; i--) {
            int bit = 1LL << i;
            int lst = l / bit + 1;
            int pivot = lst * bit;
            if (r >= pivot) {
                ans[0] = pivot, ans[1] = pivot - 1;
                break;
            }
        }

        sort(ans.begin(), ans.end());

        for (auto &i: ans) cout << i << " "; cout << "\n";
    }
}
Problem: 2057C - Trip to the Olympiad
Language: C++23 (GCC 14-64, msys2)

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

#define int long long

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int tc; cin >> tc;
    while (tc--) {
        int l, r; cin >> l >> r;
        vector<int> ans(3, l);

        for (int i = 30; i >= 0; i--) {
            int bit = 1LL << i;
            int lst = l / bit + 1;
            int pivot = lst * bit;
            if (r >= pivot) {
                ans[0] = pivot, ans[1] = pivot - 1;
                if (ans[1] == ans[2]) {
                    ans[2]++;
                }
                break;
            }
        }

        sort(ans.begin(), ans.end());

        for (auto &i: ans) cout << i << " "; cout << "\n";
    }
}


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

#define int long long

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int tc; cin >> tc;
    while (tc--) {
        int l, r; cin >> l >> r;
        vector<int> ans(3, l);

        for (int i = 30; i >= 0; i--) {
            int bit = 1LL << i;
            int lst = l / bit + 1;
            int pivot = lst * bit;
            if (r >= pivot) {
                ans[0] = pivot, ans[1] = pivot - 1;
                while (ans[2] == ans[1] || ans[2] == ans[0] && ans[2] < r) {
                    ans[2]++;
                }
                break;
            }
        }

        sort(ans.begin(), ans.end());

        for (auto &i: ans) cout << i << " "; cout << "\n";
    }
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
int mx=0;
vector<vector<int>>v;
int dfs(int idx, int prv){
    int cur = v[idx].size();
    int mxchild=0;
    for(int child : v[idx]){
        if(child != prv){
            mx = max(mx, cur+dfs(child,idx)-1);
            mxchild = max(mxchild, (int)v[child].size());
            mx = max(mx, cur+(int)v[child].size()-2);
        }
    }
    
    return mxchild;


}
void solve(){
    //max children + max children - 2 or max other - 1

    int n;
    cin >> n;
    v.resize(0);
    v.resize(n+1);
    mx =0;
    for(int i=1; i<n; i++){
        int x, y;
        cin >>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1,1);
    cout << mx << endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cout.tie(NULL);
    int t=1;
    cin >> t;
    while(t--)
    solve();
    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
int mx=0;
vector<vector<int>>v;
int dfs(int idx, int prv){
    int cur = v[idx].size();
    int mxchild=-1e9;
    for(int child : v[idx]){
        if(child != prv){
            mx = max(mx, cur+dfs(child,idx)-1);
            mxchild = max(mxchild, (int)v[child].size());
            mx = max(mx, cur+(int)v[child].size()-2);
        }
    }
    
    return mxchild;


}
void solve(){
    //max children + max children - 2 or max other - 1

    int n;
    cin >> n;
    v.resize(0);
    v.resize(n+1);
    mx =0;
    for(int i=1; i<n; i++){
        int x, y;
        cin >>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1,1);
    cout << mx << endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cout.tie(NULL);
    int t=1;
    cin >> t;
    while(t--)
    solve();
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
int mx=0;
vector<vector<int>>v;
vector<int> children;
void dfs(int idx, int prv){
    int cur = v[idx].size();
    vector<int> ch;
    ch.push_back(cur);
    for(int child : v[idx]){
        ch.push_back(v[child].size());
        if(child != prv){
            dfs(child,idx);
            mx = max(mx, cur+(int)v[child].size()-2);
        }
    }
    sort(ch.begin(),ch.end());
    int i=ch.size()-1, j=children.size()-1;
    for(; i>=0 && j>=0; i--, j--){
        if(ch[i] != children[j]) break;
    }
    // cout << j << " " << ch.size() << " " << children.size() << endl;
    if(j>=0)
    mx = max(mx, cur + children[j]-1);
    
    


}
void solve(){
    //max children + max children - 2 or max other - 1

    int n;
    cin >> n;
    v.resize(0);
    v.resize(n+1);
    children.resize(0);
    mx =0;
    for(int i=1; i<n; i++){
        int x, y;
        cin >>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for(int i=1; i<=n; i++){
        children.push_back(v[i].size());
    }
    sort(children.begin(),children.end());
    dfs(1,0);
    cout << mx << endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cout.tie(NULL);
    int t=1;
    cin >> t;
    while(t--)
    solve();
    return 0;
}