ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2057C - Trip to the Olympiad
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
#define ll long long
#define vint vector<int>
#define df double
#define pb push_back
#define fs first
#define sc second
#define pii pair<int,int>
#define vll vector<long long>
#define fr(k,i,n) for(int i=k;i<n;i++)
#define f0(i,n) for(int i=0;i<n;i++)
#define M 1000000007
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int _;
    cin>>_;
    f0(__,_){
        int32_t l,r,m=0,h;
        cin>>l>>r;
        for(int i=31;i>=0;i--){
            if((r>>i)&1){h=i;break;}
        }
        f0(i,h+1){
            bool a=((l>>i)&1),b=((r>>i)&1);
            if(!(a&b)){m=m|(1<<i);}
        }
        cout<<m<<'\n';
    }
    return 0;
}

Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
#define ll long long
#define vint vector<int>
#define df double
#define pb push_back
#define fs first
#define sc second
#define pii pair<int,int>
#define vll vector<long long>
#define fr(k,i,n) for(int i=k;i<n;i++)
#define f0(i,n) for(int i=0;i<n;i++)
#define M 1000000007
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int _;
    cin>>_;
    f0(__,_){
        int32_t l,r,m=0,h;
        cin>>l>>r;
        for(int i=31;i>=0;i--){
            if((r>>i)&1){h=i;break;}
        }
        f0(i,h+1){
            bool a=((l>>i)&1),b=((r>>i)&1);
            if(!(a&b)){m=m|(1<<i);}
        }
        if(!l){m=r-1;}
        cout<<l<<' '<<r<<' '<<m<<'\n';
    }
    return 0;
}
Problem: 2057C - Trip to the Olympiad
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int l,r;
cin>>l>>r;
int temp=r;
int cnt=0;
while(temp!=0){temp>>=1;cnt++;}
cnt--;
int a=1ll<<cnt;
int b=a-1;
int c=(a==l?r:l); //since l-r>1
cout<<a<<" "<<b<<" "<<c<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
}
Problem: 2057C - Trip to the Olympiad
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int l,r;
cin>>l>>r;
int temp=r;
int cnt=0;
while(temp!=0){temp>>=1;cnt++;}
cnt--;
int a=(1ll<<cnt);
int b,c;
if(a+1<=r){b=a+1;c=(b==r?l:r);}
else{ b=a-1; c=(a==r?l:r);}
cout<<a<<" "<<b<<" "<<c<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int l,r;
cin>>l>>r;
int temp=l^r;
int cnt=0;
while(temp!=0){temp>>=1;cnt++;}
cnt--;
//most significant differing in l,r bit is off 
int a=((1ll<<cnt)-1)|l; // xor with l to make it atleast l to keep "a" within range
// most significant differing bit in l,r is on
int b=a+1; // since r-l>1 b is always valid
int c=(a==l?r:l); // if a is l then b=l+1 so c can be from l+2 as r-l>1 
cout<<a<<" "<<b<<" "<<c<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
}
Problem: 2057C - Trip to the Olympiad
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int l,r;
cin>>l>>r;
int temp=r;
int cnt=0;
while(temp!=0){temp>>=1;cnt++;}
cnt--;
int a=1ll<<cnt;
int b=a-1;
int c=(a==l?r:l); //since l-r>1
cout<<a<<" "<<b<<" "<<c<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
}

Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int l,r;
cin>>l>>r;
int temp=r;
int cnt=0;
while(temp!=0){temp>>=1;cnt++;}
cnt--;
int a=1ll<<cnt;
int b=a-1;
int c=(a==r?l:r); //since l-r>1
cout<<a<<" "<<b<<" "<<c<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
}
Problem: 2057C - Trip to the Olympiad
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int l,r;
cin>>l>>r;
int temp=r;
int cnt=0;
while(temp!=0){temp>>=1;cnt++;}
cnt--;
int a=(1ll<<cnt)|l;
int b=a-1;
int c=(a==r?l:r); //since l-r>1
cout<<a<<" "<<b<<" "<<c<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
}

Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int l,r;
cin>>l>>r;
int temp=r;
int cnt=0;
while(temp!=0){temp>>=1;cnt++;}
cnt--;
int a=(1ll<<cnt);
int b,c;
if(a+1<=r){b=a+1;c=(b==r?l:r);}
else{ b=a-1; c=(a==r?l:r);}
cout<<a<<" "<<b<<" "<<c<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
}
Problem: 2057C - Trip to the Olympiad
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int l,r;
cin>>l>>r;
int temp=r;
int cnt=0;
while(temp!=0){temp>>=1;cnt++;}
cnt--;
int a=1ll<<cnt;
int b=a-1;
int c=(a==r?l:r); //since l-r>1
cout<<a<<" "<<b<<" "<<c<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
}

Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int l,r;
cin>>l>>r;
int temp=r;
int cnt=0;
while(temp!=0){temp>>=1;cnt++;}
cnt--;
int a=(1ll<<cnt)|l;
int b=a-1;
int c=(a==r?l:r); //since l-r>1
cout<<a<<" "<<b<<" "<<c<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define endl '\n';
#define __builtin_popcount(x) __builtin_popcountll(x)
using namespace std;

#ifdef LOCAL
#include "debuglib.h"
#else
#endif

const int INF=1e17;
const int M=1e9+7;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};

int Remove(vector<vector<int>>& edges, set<int> removed){
	int n=edges.size()+1;
	vector<int> deg(n);
	for(int i=0;i<n-1;i++){
		if(removed.count(edges[i][0]) || removed.count(edges[i][1])){
			continue;
		}
		
		++deg[edges[i][0]];
		++deg[edges[i][1]];
	}
	
	int to_rem=max_element(deg.begin(), deg.end())-deg.begin();
	if(removed.count(to_rem)){
		if(to_rem == 0){
			to_rem += 1;
		}
		else{
			to_rem -= 1;
		}
	}
	
	return to_rem;
}

void test_cases(){
	int n;
	cin >> n;
	vector<vector<int>> edges;
	for(int i=0;i<n-1;i++){
		int u, v;
		cin >> u >> v;
		--u, --v;
		edges.push_back({u, v});
	}
	
	if(n == 2){
		cout << 0 << endl;
		return;
	}
	
	set<int> removed;
	removed.insert(Remove(edges, removed));
	removed.insert(Remove(edges, removed));
	vector<vector<int>> adj(n);
	for(auto& x: edges){
		if(removed.count(x[0]) || removed.count(x[1])){
			continue;
		}
		
		adj[x[0]].push_back(x[1]);
		adj[x[1]].push_back(x[0]);
	}
	
	vector<int> vis(n);
	function<void(int)> dfs = [&](int u){
		vis[u]=1;
		for(int x: adj[u]){
			if(!vis[x]){
				dfs(x);
			}
		}
	};
	int cnt=0;
	for(int i=0;i<n;i++){
		if(!vis[i] && (removed.count(i) == 0)){
			dfs(i);
			cnt += 1;
		}
	}
	
	cout << cnt << endl;
}

int32_t main(){
	fastio;
	int t=1;
	cin >> t;
	while(t--){
		test_cases();
	}
}

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define endl '\n';
#define __builtin_popcount(x) __builtin_popcountll(x)
using namespace std;

#ifdef LOCAL
#include "debuglib.h"
#else
#endif

const int INF=1e17;
const int M=1e9+7;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};

void test_cases(){
	int n;
	cin >> n;
	vector<vector<int>> adj(n);
	vector<int> deg(n);
	for(int i=0;i<n-1;i++){
		int u, v;
		cin >> u >> v;
		--u, --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
		++deg[u], ++deg[v];
	}
	
	if(n == 2){
		cout << 0 << endl;
		return;
	}
	
	multiset<pair<int, int>> degs;
	for(int i=0;i<n;i++){
		degs.insert(make_pair(deg[i], i));
	}
	
	int ans=0;
	for(int i=0;i<n;i++){
		int first=deg[i];
		for(int x: adj[i]){
			degs.erase(make_pair(deg[x], x));
		}
		
		int second=0;
		if(!degs.empty()){
			second=(*degs.rbegin()).first;
		}
		
		ans=max(ans, first+second-1);
	}
	
	for(int i=0;i<n;i++){
		for(int x: adj[i]){
			ans=max(ans, deg[i]+deg[x]-2);
		}
	}
	
	cout << ans << endl;
}

int32_t main(){
	fastio;
	int t=1;
	cin >> t;
	while(t--){
		test_cases();
	}
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define endl '\n';
#define __builtin_popcount(x) __builtin_popcountll(x)
using namespace std;

#ifdef LOCAL
#include "debuglib.h"
#else
#endif

const int INF=1e17;
const int M=1e9+7;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};

void test_cases(){
	int n;
	cin >> n;
	vector<vector<int>> adj(n);
	vector<int> deg(n);
	for(int i=0;i<n-1;i++){
		int u, v;
		cin >> u >> v;
		--u, --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
		++deg[u], ++deg[v];
	}
	
	if(n == 2){
		cout << 0 << endl;
		return;
	}
	
	multiset<pair<int, int>> degs;
	for(int i=0;i<n;i++){
		degs.insert(make_pair(deg[i], i));
	}
	
	int ans=0;
	for(int i=0;i<n;i++){
		int first=deg[i];
		for(int x: adj[i]){
			degs.erase(make_pair(deg[x], x));
		}
		
		int second=0;
		if(!degs.empty()){
			second=(*degs.rbegin()).first;
		}
		
		ans=max(ans, first+second-1);
	}
	
	for(int i=0;i<n;i++){
		for(int x: adj[i]){
			ans=max(ans, deg[i]+deg[x]-2);
		}
	}
	
	cout << ans << endl;
}

int32_t main(){
	fastio;
	int t=1;
	cin >> t;
	while(t--){
		test_cases();
	}
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define endl '\n';
#define __builtin_popcount(x) __builtin_popcountll(x)
using namespace std;

#ifdef LOCAL
#include "debuglib.h"
#else
#endif

const int INF=1e17;
const int M=1e9+7;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};

void test_cases(){
	int n;
	cin >> n;
	vector<vector<int>> adj(n);
	vector<int> deg(n);
	for(int i=0;i<n-1;i++){
		int u, v;
		cin >> u >> v;
		--u, --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
		++deg[u], ++deg[v];
	}
	
	if(n == 2){
		cout << 0 << endl;
		return;
	}
	
	multiset<pair<int, int>> degs;
	for(int i=0;i<n;i++){
		degs.insert(make_pair(deg[i], i));
	}
	
	int ans=0;
	for(int i=0;i<n;i++){
		int first=deg[i];
		degs.erase(make_pair(deg[i], i));
		for(int x: adj[i]){
			degs.erase(make_pair(deg[x], x));
		}
		
		int second=0;
		if(!degs.empty()){
			second=(*degs.rbegin()).first;
		}
		
		degs.insert(make_pair(deg[i], i));
		for(int x: adj[i]){
			degs.insert(make_pair(deg[x], x));
		}
		
		ans=max(ans, first+second-1);
	}
	
	for(int i=0;i<n;i++){
		for(int x: adj[i]){
			ans=max(ans, deg[i]+deg[x]-2);
		}
	}
	
	cout << ans << endl;
}

int32_t main(){
	fastio;
	int t=1;
	cin >> t;
	while(t--){
		test_cases();
	}
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define endl '\n';
#define __builtin_popcount(x) __builtin_popcountll(x)
using namespace std;

#ifdef LOCAL
#include "debuglib.h"
#else
#endif

const int INF=1e17;
const int M=1e9+7;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};

int Remove(vector<vector<int>>& edges, set<int> removed){
	int n=edges.size()+1;
	vector<int> deg(n);
	for(int i=0;i<n-1;i++){
		if(removed.count(edges[i][0]) || removed.count(edges[i][1])){
			continue;
		}
		
		++deg[edges[i][0]];
		++deg[edges[i][1]];
	}
	
	int to_rem=max_element(deg.begin(), deg.end())-deg.begin();
	return to_rem;
}

void test_cases(){
	int n;
	cin >> n;
	vector<vector<int>> edges;
	for(int i=0;i<n-1;i++){
		int u, v;
		cin >> u >> v;
		--u, --v;
		edges.push_back({u, v});
	}
	
	if(n == 2){
		cout << 0 << endl;
		return;
	}
	
	set<int> removed;
	removed.insert(Remove(edges, removed));
	removed.insert(Remove(edges, removed));
	vector<vector<int>> adj(n);
	for(auto& x: edges){
		if(removed.count(x[0]) || removed.count(x[1])){
			continue;
		}
		
		adj[x[0]].push_back(x[1]);
		adj[x[1]].push_back(x[0]);
	}
	
	vector<int> vis(n);
	function<void(int)> dfs = [&](int u){
		vis[u]=1;
		for(int x: adj[u]){
			if(!vis[x]){
				dfs(x);
			}
		}
	};
	int cnt=0;
	for(int i=0;i<n;i++){
		if(!vis[i] && (removed.count(i) == 0)){
			dfs(i);
			cnt += 1;
		}
	}
	
	cout << cnt << endl;
}

int32_t main(){
	fastio;
	int t=1;
	cin >> t;
	while(t--){
		test_cases();
	}
}
Problem: 2052A - Adrenaline Rush
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;
 
void solve(){
    int n; cin >> n;
    vector<int> vt(n);
    for (int &i : vt) cin >> i;
    set<pair<int, int>> ans;
    vector<pair<int, int>> vtans;
    // 1, 2, 3 -> 2, 1, 3
    vector<int> form(n);
    for (int i = 0; i < n; i++)
        form[i] = i + 1;
    for (int i = 0; i < n; i++){
        int idx = i;
        for (; idx < n; idx++)
            if (form[idx] == vt[i])
                break;
        if (idx + 1 < n){
            if (!ans.count({form[idx + 1], form[idx]})){
                vtans.push_back({form[idx + 1], form[idx]});
                ans.insert({form[idx + 1], form[idx]});
            }
            if (!ans.count({form[idx], form[idx + 1]})){
                vtans.push_back({form[idx], form[idx + 1]});
                ans.insert({form[idx], form[idx + 1]});
            }
            
        }
        for (int j = idx; j > i; j--){
            if (!ans.count({form[j], form[j - 1]})){
                vtans.push_back({form[j], form[j - 1]});
                ans.insert({form[j], form[j - 1]});
            }
            swap(form[j], form[j - 1]);
        }
    }
    cout << ans.size() << '\n';
    for (auto &p : vtans)
        cout << p.first << " " << p.second << '\n';
}
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
 
void solve(){
    int n; cin >> n;
    vector<int> vt(n);
    for (int &i : vt) cin >> i;
    vector<pair<int, int>> ans;
    for (int i = 1; i < n; i++)
        for (int j = i + 1; j <= n; j++)
            ans.push_back({j, i});
    vector<bool> b(n + 1, false);
    for (int i = n - 1; ~i; i--){
        for (int j = vt[i] - 1; j >= 1; j--){
            if (!b[j])
                ans.push_back({j, vt[i]});
        }
        b[vt[i]] = true;
    }
    cout << ans.size() << '\n';
    for (auto &[x, y] : ans)
        cout << x << ' ' << y << '\n';
}
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++23 (GCC 14-64, msys2)


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

void solve(){
    int n; cin >> n;
    vector<int> vt(n);
    for (int &i : vt) cin >> i;
    set<pair<int, int>> ans;
    // 1, 2, 3 -> 2, 1, 3
    vector<int> form(n);
    for (int i = 0; i < n; i++)
        form[i] = i + 1;
    for (int i = 0; i < n; i++){
        int idx = i;
        for (; idx < n; idx++)
            if (form[idx] == vt[i])
                break;
        if (idx + 1 < n){
            ans.insert({form[idx], form[idx + 1]});
            ans.insert({form[idx + 1], form[idx]});
        }
        for (int j = idx; j > i; j--){
            ans.insert({form[j], form[j - 1]});
            swap(form[j], form[j - 1]);
        }
    }
    cout << ans.size() << '\n';
    for (auto &p : ans)
        cout << p.first << " " << p.second << '\n';
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++23 (GCC 14-64, msys2)

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

void solve(){
    int n; cin >> n;
    vector<int> vt(n);
    for (int &i : vt) cin >> i;
    set<pair<int, int>> ans;
    // 1, 2, 3 -> 2, 1, 3
    vector<int> form(n);
    for (int i = 0; i < n; i++)
        form[i] = i + 1;
    for (int i = 0; i < n; i++){
        int idx = i;
        for (; idx < n; idx++)
            if (form[idx] == vt[i])
                break;
        if (idx + 1 < n){
            ans.insert({form[idx], form[idx + 1]});
            ans.insert({form[idx + 1], form[idx]});
        }
        for (int j = idx; j > i; j--){
            ans.insert({form[j], form[j - 1]});
            swap(form[j], form[j - 1]);
        }
    }
    cout << ans.size() << '\n';
    for (auto &p : ans)
        cout << p.first << " " << p.second << '\n';
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;
}

Submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;
 
void solve(){
    int n; cin >> n;
    vector<int> vt(n);
    for (int &i : vt) cin >> i;
    set<pair<int, int>> ans;
    vector<pair<int, int>> vtans;
    // 1, 2, 3 -> 2, 1, 3
    vector<int> form(n);
    for (int i = 0; i < n; i++)
        form[i] = i + 1;
    for (int i = 0; i < n; i++){
        int idx = i;
        for (; idx < n; idx++)
            if (form[idx] == vt[i])
                break;
        if (idx + 1 < n){
            if (!ans.count({form[idx + 1], form[idx]})){
                vtans.push_back({form[idx + 1], form[idx]});
                ans.insert({form[idx + 1], form[idx]});
            }
            if (!ans.count({form[idx], form[idx + 1]})){
                vtans.push_back({form[idx], form[idx + 1]});
                ans.insert({form[idx], form[idx + 1]});
            }
            
        }
        for (int j = idx; j > i; j--){
            if (!ans.count({form[j], form[j - 1]})){
                vtans.push_back({form[j], form[j - 1]});
                ans.insert({form[j], form[j - 1]});
            }
            swap(form[j], form[j - 1]);
        }
    }
    cout << ans.size() << '\n';
    for (auto &p : vtans)
        cout << p.first << " " << p.second << '\n';
}
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

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

void solve(){
    int n; cin >> n;
    vector<int> deg(n + 1);
    vector<vector<int>> adj(n + 1);
    for (int i = 0; i < n - 1; i++){
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        deg[x]++;
        deg[y]++;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    vector<int> sdeg = deg;
    sort(sdeg.begin(), sdeg.end());
    int ans = 1, mans = 0;
    for (int i = 0; i < n; i++){
        ans = deg[i];
        vector<int> ideg;

        for (auto &j : adj[i])
            ideg.push_back(deg[j]);
        ideg.push_back(deg[i]);
        sort(ideg.begin(), ideg.end(), greater<int>());

        vector<int> rem;     
        for (int &j : ideg){
            if (!sdeg.empty() && sdeg.back() == j){
                sdeg.pop_back();
                rem.push_back(j);
            }
        }
        reverse(rem.begin(), rem.end());

        int mx = -1;
        for (int &j : adj[i])
            mx = max(mx, deg[j] - 1);
        if (!sdeg.empty()) mx = sdeg.back();
        for (int &j : rem){
            sdeg.push_back(j);
        }
        mans = max(mans, ans + mx - 1);
    }
    cout << mans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) solve();

    return 0;
}


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

void solve(){
    int n; cin >> n;
    vector<int> deg(n, 0);
    vector<vector<int>> adj(n);
    for (int i = 0; i < n - 1; i++){
        int x, y;
        cin >> x >> y;
        x--; y--;
        deg[x]++;
        deg[y]++;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    vector<int> sdeg = deg;
    sort(sdeg.begin(), sdeg.end());
    int ans = 1, mans = 0;
    for (int i = 0; i < n; i++){
        ans = deg[i];
        vector<int> ideg;

        for (auto &j : adj[i])
            ideg.push_back(deg[j]);
        ideg.push_back(deg[i]);
        sort(ideg.begin(), ideg.end(), greater<int>());

        vector<int> rem;     
        for (int &j : ideg){
            if (!sdeg.empty() && sdeg.back() == j){
                sdeg.pop_back();
                rem.push_back(j);
            }
        }
        reverse(rem.begin(), rem.end());

        int mx = -1;
        if (!sdeg.empty()) mx = sdeg.back();
        for (int &j : adj[i])
            mx = max(mx, deg[j] - 1);
        for (int &j : rem){
            sdeg.push_back(j);
        }
        mans = max(mans, ans + mx - 1);
    }
    cout << mans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) solve();

    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)


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

void solve(){
    int n; cin >> n;
    vector<array<int, 2>> vt(n - 1);
    for (int i = 0; i < n - 1; i++) cin >> vt[i][0] >> vt[i][1];
    vector<vector<int>> edges(n + 1);
    for (auto &a : vt){
        edges[a[0]].push_back(a[1]);
        edges[a[1]].push_back(a[0]);
    }
    int mx, mx1;
    mx = mx1 = 0;
    int d, d1;
    for (int i = 1; i <= n; i++){
        if (mx < edges[i].size()){
            mx1 = mx;
            mx = edges[i].size();
            d1 = d;
            d = i;
        }
        else if (mx1 < edges[i].size()){
            mx1 = edges[i].size();
            d1 = i;
        }
    }
    for (int &e : edges[d]){
        if (e == d1){
            cout << mx + mx1 - 2 << '\n';
            return;
        }
    }
    cout << mx + mx1 - 1 << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) solve();

    return 0;
}