ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050C - Uninteresting Number
Language: PyPy 3-64

Previous submission (verdict: Runtime error on test 4):
t=int(input())
for z in range(t):
    n=int(input())
    a=[0,0]
    num=0
    while n!=0:
        if n%10!=2 and n%10!=3:
            num=(num+n%10)%9
            n=n//10
        else:
            if n%10!=3:
                a[0]+=1
                num=(num+n%10)%9
            else:
                a[1]+=1
                num=(num+n%10)%9
            n=n//10
    flag=False
    for i in range(a[0]+1):
        if not flag:
            for j in range(a[1]+1):
                if (num+2*i+6*j)%9==0:
                    print('YES')
                    flag=True
                    break
        else:
            break
    if not flag:
        print('NO')

Submission (verdict: Runtime error on test 4):
t=int(input())
for z in range(t):
    n=int(input())
    a=[0,0]
    num=0
    while n!=0:
        if n%10!=2 and n%10!=3:
            num=(num+n%10)%9
            n=n//10
        else:
            if n%10!=3:
                a[0]+=1
                num=(num+n%10)%9
            else:
                a[1]+=1
                num=(num+n%10)%9
            n=n//10
    flag=False
    for i in range(min(10,a[0]+1)):
        if not flag:
            for j in range(min(10,a[1]+1)):
                if (num+2*i+6*j)%9==0:
                    print('YES')
                    flag=True
                    break
        else:
            break
    if not flag:
        print('NO')
Problem: 2061D - Kevin and Numbers
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
//#define int long long
using namespace std;
typedef long long ll;
const int N = 3e6 + 10, p = 2;
int T = 1;
int n, m;
int a[N], b[N];
void solve() {
    cin >> n >> m;
    map<int, int> mp;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        mp[a[i]]++;
    }
    priority_queue<int,vector<int>,greater<int>> q;
    for (int i = 1; i <= m; i++) {
        cin >> b[i];
        q.push(b[i]);
    }
    int cnt = 0;
    while (!q.empty()) {
        int x = q.top();
        //cout << x << endl;
        int y1 = x / 2, y2 = x - x / 2;
        if (mp[x] > 0) {
            mp[x]--;
            q.pop();
        }
        else if (mp[y1] > 0 && mp[y2] > 0 && y1 != y2) {
            mp[y1]--;
            mp[y2]--;
            q.pop();
        }
        else if (mp[y1] >= 2 && y1 == y2) {
            mp[y1] -= 2;
            q.pop();
        }
        else if (mp[y1] > 0) {
            mp[y1]--;
            q.pop();
            if(y2 > 0) q.push(y2);
        }
        else if (mp[y2] > 0) {
            mp[y2]--;
            q.pop();
            if(y1 > 0) q.push(y1);
        }
        else if(x==1) {
            cout << "NO" << endl;
            return;
        }
        else {
            if (y1) q.push(y1);
            if (y2) q.push(y2);
        }
    }
    for (auto x : mp) {
        if (x.second > 0) {
            cout << "NO" << endl;
            return;
        }
    }
    cout << "YES" << endl;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> T;
    while (T--) {
        solve();
        //cout << endl;
    }
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
//#define int long long
using namespace std;
typedef long long ll;
const int N = 3e6 + 10, p = 2;
int T = 1;
int n, m;
int a[N], b[N];
void solve() {
    cin >> n >> m;
    priority_queue<int> q1,q2;
    ll sum = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        sum += a[i];
        q1.push(a[i]);
    }
    for (int i = 1; i <= m; i++) {
        cin >> b[i];
        q2.push(b[i]);
    }
    int cnt = 0;
    while (!q2.empty()) {
        int x = q2.top();
        if (q1.empty()) {
            cout << "NO" << endl;
            return;
        }
        int y = q1.top();
        if (x == y) {
            q2.pop();
            q1.pop();
            sum -= y;
        }
        else {
            if (y > x) {
                cout << "NO" << endl;
                return;
            }
            if (sum < x) {
                cout << "NO" << endl;
                return;
            }
            if(x/2) q2.push(x / 2);
            if(x - x/2) q2.push(x - x / 2);
            q2.pop();
        }
    }
    if (!q1.empty()) {
        cout << "NO" << endl;
        return;
    }
    cout << "YES" << endl;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> T;
    while (T--) {
        solve();
        //cout << endl;
    }
    return 0;
}
Problem: 2061D - Kevin and Numbers
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
//#define int long long
using namespace std;
typedef long long ll;
const int N = 3e6 + 10, p = 2;
int T = 1;
int n, m;
int a[N], b[N];
void solve() {
    cin >> n >> m;
    map<int, int> mp;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        mp[a[i]]++;
    }
    priority_queue<int,vector<int>,greater<int>> q;
    for (int i = 1; i <= m; i++) {
        cin >> b[i];
        q.push(b[i]);
    }
    while (!q.empty()) {
        int x = q.top();
        //cout << x << endl;
        int y1 = x / 2, y2 = x - x / 2;
        if (mp[x] > 0) {
            mp[x]--;
            q.pop();
        }
        else if (mp[y1] > 0 && mp[y2] > 0 && y1 != y2) {
            mp[y1]--;
            mp[y2]--;
            q.pop();
        }
        else if (mp[y1] >= 2 && y1 == y2) {
            mp[y1] -= 2;
            q.pop();
        }
        else if (mp[y1] > 0) {
            mp[y1]--;
            q.pop();
            if(y2 > 0) q.push(y2);
        }
        else if (mp[y2] > 0) {
            mp[y2]--;
            q.pop();
            if(y1 > 0) q.push(y1);
        }
        else {
            cout << "NO" << endl;
            return;
        }
    }
    for (auto x : mp) {
        if (x.second > 0) {
            cout << "NO" << endl;
            return;
        }
    }
    cout << "YES" << endl;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> T;
    while (T--) {
        solve();
        //cout << endl;
    }
    return 0;
}
Problem: 2062C - Cirno and Operations
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const int N = 3e6 + 10;
int t = 1;
int n;
int a[N];
int ans = -2e9;
void dfs(int b[], int len,int now) {
	if (len == 0) return;
	int sum = 0;
	for (int i = 1; i <= len; i++) {
		sum += b[i];
	}
	ans = max(ans, sum);
	int c[51];
	if (now == 1) {
		for (int i = 1; i <= len - 1; i++) {
			c[i] = b[i + 1] - b[i];
		}
		dfs(b, len, 2);
		dfs(c, len - 1, 1);
	}
	if (now == 2) {
		reverse(b + 1, b + 1 + len);
		for (int i = 1; i <= len - 1; i++) {
			c[i] = b[i + 1] - b[i];
		}
		dfs(c, len - 1, 3);
	}
	if (now == 3) {
		return;
	}
}
void solve() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	dfs(a, n, 1);
	cout << ans << endl;
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> t;
	while (t--) {
		solve();
		//cout << endl;
	}
	return 0;
}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define int long long
using namespace std;
typedef long long ll;
const int N = 3e6 + 10;
int t = 1;
int n;
int a[N];
int ans = -9e18;
void dfs(int b[], int len,int now) {
	if (len == 0) return;
	int sum = 0;
	for (int i = 1; i <= len; i++) {
		sum += b[i];
	}
	ans = max(ans, sum);
	int c[51];
	if (now == 1) {
		for (int i = 1; i <= len - 1; i++) {
			c[i] = b[i + 1] - b[i];
		}
		dfs(b, len, 2);
		dfs(c, len - 1, 1);
	}
	if (now == 2) {
		reverse(b + 1, b + 1 + len);
		for (int i = 1; i <= len - 1; i++) {
			c[i] = b[i + 1] - b[i];
		}
		dfs(c, len - 1, 3);
	}
	if (now == 3) {
		return;
	}
}
void solve() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	dfs(a, n, 1);
	cout << ans << endl;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> t;
	while (t--) {
		solve();
		//cout << endl;
	}
	return 0;
}
Problem: 2062C - Cirno and Operations
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define int long long
using namespace std;
typedef long long ll;
const int N = 3e6 + 10;
int t = 1;
int n;
int a[N];
int ans = -9e18;
void dfs(int b[], int len,int now) {
	if (len == 0) return;
	int sum = 0;
	for (int i = 1; i <= len; i++) {
		sum += b[i];
	}
	ans = max(ans, sum);
	int c[51];
	if (now == 1) {
		for (int i = 1; i <= len - 1; i++) {
			c[i] = b[i + 1] - b[i];
		}
		dfs(b, len, 2);
		dfs(c, len - 1, 1);
	}
	if (now == 2) {
		reverse(b + 1, b + 1 + len);
		for (int i = 1; i <= len - 1; i++) {
			c[i] = b[i + 1] - b[i];
		}
		dfs(c, len - 1, 3);
	}
	if (now == 3) {
		return;
	}
}
void solve() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	dfs(a, n, 1);
	cout << ans << endl;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> t;
	while (t--) {
		solve();
		//cout << endl;
	}
	return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define int long long
using namespace std;
typedef long long ll;
const int N = 3e6 + 10;
int t = 1;
int n;
int a[N];
int ans = -9e18;
void dfs(int b[], int len,int now) {
	if (len == 0) return;
	int sum = 0;
	for (int i = 1; i <= len; i++) {
		sum += b[i];
	}
	ans = max(ans, sum);
	int c[51];
	if (now == 1) {
		for (int i = 1; i <= len - 1; i++) {
			c[i] = b[i + 1] - b[i];
		}
		dfs(b, len, 2);
		dfs(c, len - 1, 1);
	}
	if (now == 2) {
		reverse(b + 1, b + 1 + len);
		for (int i = 1; i <= len - 1; i++) {
			c[i] = b[i + 1] - b[i];
		}
		dfs(c, len - 1, 3);
	}
	if (now == 3) {
		return;
	}
}
void solve() {
	cin >> n;
	ans = -9e18;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	dfs(a, n, 1);
	cout << ans << endl;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> t;
	while (t--) {
		solve();
		//cout << endl;
	}
	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>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const int N = 3e6 + 10;
int t = 1;
int n;
int a[N];
int ans = -2e9;
void dfs(int b[], int len,int now) {
	if (len == 0) return;
	int sum = 0;
	for (int i = 1; i <= len; i++) {
		sum += b[i];
	}
	ans = max(ans, sum);
	int c[51];
	if (now == 1) {
		for (int i = 1; i <= len - 1; i++) {
			c[i] = b[i + 1] - b[i];
		}
		dfs(b, len, 2);
		dfs(c, len - 1, 1);
	}
	if (now == 2) {
		reverse(b + 1, b + 1 + len);
		for (int i = 1; i <= len - 1; i++) {
			c[i] = b[i + 1] - b[i];
		}
		dfs(c, len - 1, 3);
	}
	if (now == 3) {
		return;
	}
}
void solve() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	dfs(a, n, 1);
	cout << ans << endl;
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> t;
	while (t--) {
		solve();
		//cout << endl;
	}
	return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 7):
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> ns;
vector<int> cur;
vector<int> x;

void f(int it, int y, int j){
    int co = 0;
    it--;
    if(it != - 1){
        while(cur[it] <= j){
            ns.push_back({cur[it+1], cur[it]});
            swap(cur[it], cur[it+1]);
            it--;
            if(it == -1){
                break;
            }
        }
    }
    it++;
    while(it < y){
        ns.push_back({cur[it+1], cur[it]});
        swap(cur[it], cur[it+1]);
        it++;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    x.resize(n);
    for(int i = 0; i < n; i++){
        cin >> x[i];
    }
    cur.resize(n);
    for(int i = 0; i < n; i++){
        cur[i] = i+1;
    }
    for(int y = 0; y < n; y++){
        int it = 0;
        for(int i = 0; i < n; i++){
            if(cur[i] == x[y]){
                it = i;
                break;
            }
        }
        for(int i = y; i <= it; i++){
            f(i, y, cur[i]);
        }
    }
    cout << ns.size() << "\n";
    for(int i = 0; i < ns.size(); i++){
        cout << ns[i].first << " " << ns[i].second << "\n";
    }
    return 0;
}

//5
//1 2 3
//2 3 1
//

Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> ns;
vector<int> cur;
vector<int> pos;
vector<int> x;
vector<vector<bool>> v;
bool d = false;

void f(int it, int y, int j){
    int co = 0;
    it--;
    if(it != - 1){
        while((cur[it] <= j && !v[cur[it]][cur[it+1]] && !v[cur[it+1]][cur[it]]) || (d && it >= y)){
            v[cur[it+1]][cur[it]] = true;
            ns.push_back({cur[it+1], cur[it]});
            swap(cur[it], cur[it+1]);
            it--;
            if(it == -1){
                break;
            }
        }
    }
    it++;
    while(it < y){
        v[cur[it+1]][cur[it]] = true;
        ns.push_back({cur[it+1], cur[it]});
        swap(cur[it], cur[it+1]);
        it++;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    v.resize(n+1, vector<bool>(n+1));
    x.resize(n);
    pos.resize(n+1);
    for(int i = 0; i < n; i++){
        cin >> x[i];
        pos[x[i]] = i;
    }
    cur.resize(n);
    for(int i = 0; i < n; i++){
        cur[i] = i+1;
    }
    for(int y = 0; y < n; y++){
        int it = 0;
        for(int i = 0; i < n; i++){
            if(cur[i] == x[y]){
                it = i;
                break;
            }
        }
        for(int i = y; i <= it; i++){
            d = false;
            if(i == it){
                d = true;
            }
            f(i, y, cur[i]);
        }
        cout << ns.size() << "\n";
        for(int i = 0; i < n; i++){
            cout << cur[i] << " ";
        }cout << "\n";
    }
    cout << ns.size() << "\n";
    for(int y = 0; y < ns.size(); y++){
        cout << ns[y].first << " " << ns[y].second << "\n";
    }
    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 7):
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> ns;
vector<int> cur;
vector<int> x;

void f(int it, int y, int j){
    int co = 0;
    it--;
    if(it != - 1){
        while(cur[it] <= j){
            ns.push_back({cur[it+1], cur[it]});
            swap(cur[it], cur[it+1]);
            it--;
            if(it == -1){
                break;
            }
        }
    }
    it++;
    while(it < y){
        ns.push_back({cur[it+1], cur[it]});
        swap(cur[it], cur[it+1]);
        it++;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    x.resize(n);
    for(int i = 0; i < n; i++){
        cin >> x[i];
    }
    cur.resize(n);
    for(int i = 0; i < n; i++){
        cur[i] = i+1;
    }
    for(int y = 0; y < n; y++){
        int it = 0;
        for(int i = 0; i < n; i++){
            if(cur[i] == x[y]){
                it = i;
                break;
            }
        }
        for(int i = y; i <= it; i++){
            f(i, y, cur[i]);
        }
    }
    cout << ns.size() << "\n";
    for(int i = 0; i < ns.size(); i++){
        cout << ns[i].first << " " << ns[i].second << "\n";
    }
    return 0;
}

//5
//1 2 3
//2 3 1
//
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define fi first
#define se second
#define sajz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll x, ll y) {return uniform_int_distribution<ll>(x, y)(rng);}

void solve(){
	int n; cin >> n;
	vector g(n+1, vector<int>()), dp(n+1, vector<int>(3));
	vector<int> deg(n+1);
	for (int i=0; i<n-1; i++){
		int x, y; cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
		deg[x]++;
		deg[y]++;
	}
	int res = 0;
	function<void(int,int)> dfs = [&](int v, int par){
		pair<int,int> mx[2] = {{0, 0}, {0, 0}};
		for (auto u : g[v]) if (u != par){
			dfs(u, v);
			res = max(res, dp[u][0] + deg[v] - 2);
			res = max(res, dp[u][1] + deg[v] - 1);
			dp[v][0] = max(dp[v][0], deg[v]);
			dp[v][1] = max(dp[v][1], dp[u][0]);
			dp[v][2] = max(dp[v][2], dp[u][1]);
			if (dp[u][0] >= mx[0].fi){
				mx[1] = mx[0];
				mx[0] = {dp[u][0], u};
			}else if (dp[u][0] > mx[1].fi) mx[1] = {dp[u][0], u};
		}
		res = max(res, mx[0].fi + mx[1].fi - 1);
		for (auto u : g[v]) if (u != par){
			if (u != mx[0].se) res = max(res, mx[0].fi + dp[u][1]);
			else res = max(res, mx[1].fi + dp[u][1]);
		}
	};
	dfs(1, 0);
	cout << res << '\n';
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int tc; cin >> tc;
	while (tc--) solve();
	return 0;
}
/*



*/


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define fi first
#define se second
#define sajz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll x, ll y) {return uniform_int_distribution<ll>(x, y)(rng);}

void solve(){
	int n; cin >> n;
	vector g(n+1, vector<int>()), dp(n+1, vector<int>(3));
	vector<int> deg(n+1);
	for (int i=0; i<n-1; i++){
		int x, y; cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
		deg[x]++;
		deg[y]++;
	}
	int res = 0;
	function<void(int,int)> dfs = [&](int v, int par){
		pair<int,int> mx[2] = {{0, 0}, {0, 0}};
		for (auto u : g[v]) if (u != par){
			dfs(u, v);
			res = max(res, dp[u][0] + deg[v] - 2);
			res = max(res, dp[u][1] + deg[v] - 1);
			dp[v][0] = max(dp[v][0], deg[v]);
			dp[v][1] = max(dp[v][1], dp[u][0]);
			dp[v][2] = max(dp[v][2], dp[u][1]);
			if (dp[u][0] >= mx[0].fi){
				mx[1] = mx[0];
				mx[0] = {dp[u][0], u};
			}else if (dp[u][0] > mx[1].fi) mx[1] = {dp[u][0], u};
		}
		res = max(res, mx[0].fi + mx[1].fi - 1);
		for (auto u : g[v]) if (u != par){
			if (u != mx[0].se) res = max(res, mx[0].fi + dp[u][1] - 1);
			else res = max(res, mx[1].fi + dp[u][1] - 1);
		}
	};
	dfs(1, 0);
	//for (int i=1; i<=n; i++) cout << i << ": " << dp[i][0] << ' ' << dp[i][1] << ' ' << dp[i][2] << '\n';
	cout << res << '\n';
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int tc; cin >> tc;
	while (tc--) solve();
	return 0;
}
/*

1
6
1 2
1 3
3 4
4 5
2 6

*/
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define fi first
#define se second
#define sajz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll x, ll y) {return uniform_int_distribution<ll>(x, y)(rng);}

void solve(){
	int n; cin >> n;
	vector g(n+1, vector<int>()), dp(n+1, vector<int>(3));
	vector<int> deg(n+1);
	for (int i=0; i<n-1; i++){
		int x, y; cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
		deg[x]++;
		deg[y]++;
	}
	int res = 0;
	function<void(int,int)> dfs = [&](int v, int par){
		int mx[2] = {0, 0};
		for (auto u : g[v]) if (u != par){
			dfs(u, v);
			res = max(res, dp[u][0] + deg[v] - 2);
			res = max(res, dp[u][1] + deg[v] - 1);
			dp[v][0] = max(dp[v][0], deg[v]);
			dp[v][1] = max(dp[v][1], dp[u][0]);
			dp[v][2] = max(dp[v][2], dp[u][1]);
			if (dp[u][0] >= mx[0]){
				mx[1] = mx[0];
				mx[0] = dp[u][0];
			}else if (dp[u][0] > mx[1]) mx[1] = dp[u][0];
		}
		res = max(res, mx[0] + mx[1] - 1);
	};
	dfs(1, 0);
	cout << res << '\n';
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int tc; cin >> tc;
	while (tc--) solve();
	return 0;
}
/*



*/


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define fi first
#define se second
#define sajz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll x, ll y) {return uniform_int_distribution<ll>(x, y)(rng);}

void solve(){
	int n; cin >> n;
	vector g(n+1, vector<int>()), dp(n+1, vector<int>(3));
	vector<int> deg(n+1);
	for (int i=0; i<n-1; i++){
		int x, y; cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
		deg[x]++;
		deg[y]++;
	}
	int res = 0;
	function<void(int,int)> dfs = [&](int v, int par){
		pair<int,int> mx[2] = {{0, 0}, {0, 0}};
		for (auto u : g[v]) if (u != par){
			dfs(u, v);
			res = max(res, dp[u][0] + deg[v] - 2);
			res = max(res, dp[u][1] + deg[v] - 1);
			dp[v][0] = max(dp[v][0], deg[v]);
			dp[v][1] = max(dp[v][1], dp[u][0]);
			dp[v][2] = max(dp[v][2], dp[u][1]);
			if (dp[u][0] >= mx[0].fi){
				mx[1] = mx[0];
				mx[0] = {dp[u][0], u};
			}else if (dp[u][0] > mx[1].fi) mx[1] = {dp[u][0], u};
		}
		res = max(res, mx[0].fi + mx[1].fi - 1);
		for (auto u : g[v]) if (u != par){
			if (u != mx[0].se) res = max(res, mx[0].fi + dp[u][1]);
			else res = max(res, mx[1].fi + dp[u][1]);
		}
	};
	dfs(1, 0);
	cout << res << '\n';
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int tc; cin >> tc;
	while (tc--) solve();
	return 0;
}
/*



*/
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define fi first
#define se second
#define sajz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll x, ll y) {return uniform_int_distribution<ll>(x, y)(rng);}

void solve(){
	int n; cin >> n;
	vector g(n+1, vector<int>()), dp(n+1, vector<int>(3));
	vector<int> deg(n+1);
	for (int i=0; i<n-1; i++){
		int x, y; cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
		deg[x]++;
		deg[y]++;
	}
	int res = 0;
	function<void(int,int)> dfs = [&](int v, int par){
		int mx[2] = {0, 0};
		for (auto u : g[v]) if (u != par){
			dfs(u, v);
			res = max(res, dp[u][0] + deg[v] - 2);
			res = max(res, dp[u][1] + deg[v] - 1);
			dp[v][0] = max(dp[v][0], deg[v]);
			dp[v][1] = max(dp[v][1], dp[u][0]);
			dp[v][2] = max(dp[v][2], dp[u][1]);
			if (dp[u][0] >= mx[0]){
				mx[1] = mx[0];
				mx[0] = dp[u][0];
			}else if (dp[u][0] > mx[1]) mx[1] = dp[u][0];
		}
		res = max(res, mx[0] + mx[1] - 1);
	};
	dfs(1, 0);
	cout << res << '\n';
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int tc; cin >> tc;
	while (tc--) solve();
	return 0;
}
/*



*/