ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050D - Digital string maximization
Language: C++20 (GCC 13-64)


Submission (verdict: Time limit exceeded on test 8):
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(m) m.begin(),m.end()
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    string s;
    cin >> s;
    ll n = s.size();
    vector<bool> vis(n,false);
    string ans(n,'0');
    ll cnt = 0;
    ll i = 0;
    while(i<n && cnt<n){
        ll j = i;
        ll maxi_ind = i;
        ll maxi_val = s[i]-'0';
        ll jump = 10;
        while(j<n && jump > 0){
            if(vis[j]){
                j++;
                continue;
            }
            if(s[j]-'0'-(10-jump) > maxi_val){
                maxi_val = s[j]-'0'-(10-jump);
                maxi_ind = j;
            }
            jump--;
            j++;
        }
        vis[maxi_ind] = true;
        ans[cnt] = '0'+maxi_val;
        cnt++;
        if(i==maxi_ind){
            while(i<n && vis[i]){
                i++;
            }
        }
    }
    cout << ans << endl;
}
int main(){
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
}
Problem: 2050D - Digital string maximization
Language: C++20 (GCC 13-64)

Previous submission (verdict: Compilation error):
v#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(m) m.begin(),m.end()
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    string s;
    cin >> s;
    ll n = s.size();
    f(i,0,n-1){
        ll maxi_ind = i;
        ll maxi_val = s[i]-'0';
        f(j,i,min(10LL,n-1)){
            if(s[j]-'0'+i-j>maxi_val){
                maxi_val = s[j]-'0'+i-j;
                maxi_ind = j;
            }
        }
        while(maxi_ind > i){
            swap(s[maxi_ind],s[maxi_ind-1]);
            maxi_ind--;
        }
        s[i] = '0'+maxi_val;
    }
    cout << s << endl;
}
int main(){
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
}

Submission (verdict: Wrong answer on test 3):
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(m) m.begin(),m.end()
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    string s;
    cin >> s;
    ll n = s.size();
    f(i,0,n-1){
        ll maxi_ind = i;
        ll maxi_val = s[i]-'0';
        f(j,i,min(10LL,n-1)){
            if(s[j]-'0'+i-j>maxi_val){
                maxi_val = s[j]-'0'+i-j;
                maxi_ind = j;
            }
        }
        while(maxi_ind > i){
            swap(s[maxi_ind],s[maxi_ind-1]);
            maxi_ind--;
        }
        s[i] = '0'+maxi_val;
    }
    cout << s << endl;
}
int main(){
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
}
Problem: 2050D - Digital string maximization
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 3):
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(m) m.begin(),m.end()
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    string s;
    cin >> s;
    ll n = s.size();
    f(i,0,n-1){
        ll maxi_ind = i;
        ll maxi_val = s[i]-'0';
        ll j = i;
        f(j,i,min(10LL,n-1)){
            if(s[j]-'0'+i-j>maxi_val){
                maxi_val = s[j]-'0'+i-j;
                maxi_ind = j;
            }
        }
        s[maxi_ind] ='0'+ maxi_val;
        while(maxi_ind > i){
            swap(s[maxi_ind-1],s[maxi_ind]);
            maxi_ind--;
        }
    }
    cout << s << endl;
}
int main(){
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
}

Submission (verdict: Compilation error):
v#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(m) m.begin(),m.end()
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    string s;
    cin >> s;
    ll n = s.size();
    f(i,0,n-1){
        ll maxi_ind = i;
        ll maxi_val = s[i]-'0';
        f(j,i,min(10LL,n-1)){
            if(s[j]-'0'+i-j>maxi_val){
                maxi_val = s[j]-'0'+i-j;
                maxi_ind = j;
            }
        }
        while(maxi_ind > i){
            swap(s[maxi_ind],s[maxi_ind-1]);
            maxi_ind--;
        }
        s[i] = '0'+maxi_val;
    }
    cout << s << endl;
}
int main(){
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
}
Problem: 2050D - Digital string maximization
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 3):
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(m) m.begin(),m.end()
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    string s;
    cin >> s;
    ll n = s.size();
    f(i,0,n-1){
        ll maxi_ind = i;
        ll maxi_val = s[i]-'0';
        f(j,i,min(10LL,n-1)){
            if(s[j]-'0'+i-j>maxi_val){
                maxi_val = s[j]-'0'+i-j;
                maxi_ind = j;
            }
        }
        while(maxi_ind > i){
            swap(s[maxi_ind],s[maxi_ind-1]);
            maxi_ind--;
        }
        s[i] = '0'+maxi_val;
    }
    cout << s << endl;
}
int main(){
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(m) m.begin(),m.end()
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    string s;
    cin >> s;
    ll n = s.size();
    f(i,0,n-1){
        ll maxi_ind = i;
        ll maxi_val = s[i]-'0';
        f(j,i,min(i+10LL,n-1)){
            if(s[j]-'0'+i-j>maxi_val){
                maxi_val = s[j]-'0'+i-j;
                maxi_ind = j;
            }
        }
        while(maxi_ind > i){
            swap(s[maxi_ind],s[maxi_ind-1]);
            maxi_ind--;
        }
        s[i] = '0'+maxi_val;
    }
    cout << s << endl;
}
int main(){
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
}
Problem: 2050D - Digital string maximization
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 8):
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(m) m.begin(),m.end()
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    string s;
    cin >> s;
    ll n = s.size();
    vector<bool> vis(n,false);
    string ans(n,'0');
    ll cnt = 0;
    ll i = 0;
    while(i<n && cnt<n){
        ll j = i;
        ll maxi_ind = i;
        ll maxi_val = s[i]-'0';
        ll jump = 10;
        while(j<n && jump > 0){
            if(vis[j]){
                j++;
                continue;
            }
            if(s[j]-'0'-(10-jump) > maxi_val){
                maxi_val = s[j]-'0'-(10-jump);
                maxi_ind = j;
            }
            jump--;
            j++;
        }
        vis[maxi_ind] = true;
        ans[cnt] = '0'+maxi_val;
        cnt++;
        if(i==maxi_ind){
            while(i<n && vis[i]){
                i++;
            }
        }
    }
    cout << ans << endl;
}
int main(){
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
}

Submission (verdict: Wrong answer on test 3):
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(m) m.begin(),m.end()
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}
void solve(){
    string s;
    cin >> s;
    ll n = s.size();
    f(i,0,n-1){
        ll maxi_ind = i;
        ll maxi_val = s[i]-'0';
        ll j = i;
        f(j,i,min(10LL,n-1)){
            if(s[j]-'0'+i-j>maxi_val){
                maxi_val = s[j]-'0'+i-j;
                maxi_ind = j;
            }
        }
        s[maxi_ind] ='0'+ maxi_val;
        while(maxi_ind > i){
            swap(s[maxi_ind-1],s[maxi_ind]);
            maxi_ind--;
        }
    }
    cout << s << endl;
}
int main(){
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
}
Problem: 2057C - Trip to the Olympiad
Language: C++20 (GCC 13-64)


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

#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define int long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define vi vector<int>
#define inp for(int i=0;i<n;i++) cin>>arr[i]
#define vll vector<ll>
#define vvi vector<vector<int> >
#define vpii vector<pii >
#define mp make_pair
#define f first
#define s second

const int MOD = 1e9 + 7;
const int INF = 1e9;

// Function to print a vector
template <typename T>
void printVector(const vector<T>& v) {
    for (const auto& element : v) {
        cout << element << ' ';
    }
    cout << endl;
}

// Function to print a set
template <typename T>
void printSet(const set<T>& s) {
    for (const auto& element : s) {
        cout << element << ' ';
    }
    cout << endl;
}

// Function to print a map
template <typename K, typename V>
void printMap(const map<K, V>& m) {
    for (const auto& [key, value] : m) {
        cout << key << " -> " << value << endl;
    }
}
void func(int a,int b,int c){
    cout<<(a^b)+(b^c)+(c^a)<<endl;
}
void solve() {
    // Write your code here
    int l,r;
    cin>>l>>r;
    int msd = log2(r);
    vector<int> a1,a2;
    for(int i=msd ;i>=0;i--){
        if(l & (1LL << i)){
            a1.pb(1);
        }
        else a1.pb(0);
        if(r & (1LL << i)){
            a2.pb(1);
        }
        else a2.pb(0);
    }
    int num = 0;
    reverse(all(a1));
    reverse(all(a2));
    bool flag = true,flag2 = true;
    for(int i=msd;i>=0;i--){
        if(a1[i] == a2[i] && flag){
            num += (1LL << i)*a1[i];
        }
        else{
            if(flag){
                // num += (1LL << i);
                flag = false;
            }
            else{
                // if(a1[i] == 0 && flag2){
                //     num += (1LL << i);
                //     flag2 = false;
                //     continue;
                // }
                if(flag2){
                    num += (1LL << i);
                    if(a1[i] == 0) flag2 = false;
                }
                else{
                    if(a1[i] != a2[i] || a1[i] == 0 && a2[i] == 0) 
                    num += (1LL << i);
                }
               
            }
        }
        // if(i == msd && a1[i] == a2[i] && a1[i] == 1){
        //     num = l;
        //     break;
        // }
        // else if(a1[i] == a2[i] && a1[i] == 0) num += (1LL << i);
        // else if(a1[i] != a2[i]) num += (1LL << i);
        // cout<<i<<" - "<<num<<" ";
    }
    // cout<<endl;
    // printVector(a1);
    // printVector(a2);
    // cout<<msd<<"->";
    cout<<l<<" "<<r<<" "<<num<<endl;
    // func(0,8,7);
    // func(8,7,1);
}

signed main() {
    fastio;
    int t = 1;
    cin >> t; // Uncomment if multiple test cases
    while (t--) {
        solve();
    }
    return 0;
}
Problem: 2057C - Trip to the Olympiad
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 1):
    #include <bits/stdc++.h>
    using namespace std;
    using i64 = long long;
    using u64 = unsigned long long;
    using u32 = unsigned;
    using u128 = unsigned __int128;
    int binaryToDecimal(string n)
    {
        string num = n;
        int dec_value = 0;
 
    // Initializing base value to 1, i.e 2^0
        i64 base = 1;
 
        int len = num.length();
        for (int i = len - 1; i >= 0; i--) {
            if (num[i] == '1')
                dec_value += base;
                base = base * 2;
        }
 
        return dec_value;
    }
    string decToBinary(int n){
        string s="";
    // Size of an integer is assumed to be 32 bits
        for (int i = 31; i >= 0; i--) {
            int k = n >> i;
            if (k & 1)
                s.push_back('1');
            else
                s.push_back('0');
            }
        return s;
    }
    void solve() {
        int l, r;
        cin >> l >> r;
        string s1=decToBinary(l);
        string s2=decToBinary(r);
        //cout<<s1<<"\n"<<s2<<"\n";
        int ind=0;
        for(int i=0;i<32;i++){
            if(s1[i]!=s2[i]){
                ind=i;
                break;
            }
        }
        s1[ind]='1';
        s2[ind]='0';
        //cout<<s1<<"\n"<<s2<<"\n";
        for(int i=0;i<31;i++){
            if(i<=ind){
                continue;
            }
            s1[i]='0';
            s2[i]='1';
        }
        i64 x=binaryToDecimal(s1);
        i64 y=binaryToDecimal(s2);
        cout<<x<<" "<<y<<" "<<y+1<<"\n";
    }
     
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        
        int t;
        cin >> t;
        
        while (t--) {
            solve();
        }
        
        return 0;
    }
Problem: 2062C - Cirno and Operations
Language: C++23 (GCC 14-64, msys2)

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

int main() {
    int t; cin >> t;
    while (t--) {
        int n; c5in >> n;
        vector<int> a(n);
        long long sum = 0;
        for(int i = 0; i<n; i++){
            cin >> a[i];
            sum += a[i];
        }
        while(n>=2){
            long long maxi = 0;
            for(int i = 0; i<n-1; i++){
                long long diff = a[i+1] - a[i];
                a[i] = diff;
                maxi += diff;
            }
            sum = max(sum, abs(maxi));
            n--;
        }
        cout << sum << endl;
    }
    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<int> a(n);
        long long sum = 0;
        for(int i = 0; i<n; i++){
            cin >> a[i];
            sum += a[i];
        }
        while(n>=2){
            long long maxi = 0;
            for(int i = 0; i<n-1; i++){
                long long diff = a[i+1] - a[i];
                a[i] = diff;
                maxi += diff;
            }
            sum = max(sum, abs(maxi));
            n--;
        }
        cout << sum << endl;
    }
    return 0;
}
Problem: 2062C - Cirno and Operations
Language: C++23 (GCC 14-64, msys2)


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

int main() {
    int t; cin >> t;
    while (t--) {
        int n; c5in >> n;
        vector<int> a(n);
        long long sum = 0;
        for(int i = 0; i<n; i++){
            cin >> a[i];
            sum += a[i];
        }
        while(n>=2){
            long long maxi = 0;
            for(int i = 0; i<n-1; i++){
                long long diff = a[i+1] - a[i];
                a[i] = diff;
                maxi += diff;
            }
            sum = max(sum, abs(maxi));
            n--;
        }
        cout << sum << 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>
using namespace std;
 
int main() {
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        vector<int> a(n);
        long long sum = 0;
        for(int i = 0; i<n; i++){
            cin >> a[i];
            sum += a[i];
        }
        while(n>=2){
            long long maxi = 0;
            for(int i = 0; i<n-1; i++){
                long long diff = a[i+1] - a[i];
                a[i] = diff;
                maxi += diff;
            }
            sum = max(sum, abs(maxi));
            n--;
        }
        cout << sum << endl;
    }
    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<long long> a(n);
        long long sum = 0;
        for(int i = 0; i<n; i++){
            cin >> a[i];
            sum += a[i];
        }
        while(n>=2){
            long long maxi = 0;
            for(int i = 0; i<n-1; i++){
                long long diff = a[i+1] - a[i];
                a[i] = diff;
                maxi += diff;
            }
            sum = max(sum, abs(maxi));
            n--;
        }
        cout << sum << endl;
    }
    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>
 
#define int int64_t
using namespace std;

void solve() {
    int n;
    cin >> n;
    vector<int> deg(n + 1);
    vector<vector<int>> a(n + 1, vector<int>());
    
    for (int i{1}; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        ++deg[u];
        ++deg[v];
        a[u].push_back(v);
        a[v].push_back(u);
    }

    int max_deg{};
    vector<int> deg_cnt(n);
    for (int i{1}; i < n; ++i) {
        max_deg = max(max_deg, deg[i]);
        ++deg_cnt[deg[i]];
    }

    int ans{};

    for (int u{1}; u <= n; ++u) {
        if (deg[u] == max_deg && deg_cnt[max_deg] == 1) {
            continue;
        }

        for (int v : a[u]) {
            --deg_cnt[deg[v]];
        }

        ans = max(ans, deg[u] + max_deg - int(deg_cnt[max_deg] == 0) - 1);

        for (int v : a[u]) {
            ++deg_cnt[deg[v]];
        }
    }

    cout << ans << "\n";
}

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

    int t{1};
    cin >> t;
    while (t--) {
        solve();
    }
}


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

void solve() {
    int n;
    cin >> n;
    vector<int> deg(n + 1);
    vector<vector<int>> a(n + 1, vector<int>());
    
    for (int i{1}; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        ++deg[u];
        ++deg[v];
        a[u].push_back(v);
        a[v].push_back(u);
    }

    int max_deg{};
    vector<int> deg_cnt(n);
    for (int i{1}; i < n; ++i) {
        max_deg = max(max_deg, deg[i]);
        ++deg_cnt[deg[i]];
    }

    int ans{};

    for (int u{1}; u <= n; ++u) {
        if (deg[u] == max_deg && deg_cnt[max_deg] == 1) {
            continue;
        }

        for (int v : a[u]) {
            --deg_cnt[deg[v]];
        }
        --deg_cnt[deg[u]];

        ans = max(ans, deg[u] + max_deg - int(deg_cnt[max_deg] == 0) - 1);

        for (int v : a[u]) {
            ++deg_cnt[deg[v]];
        }
        ++deg_cnt[deg[u]];
    }

    cout << ans << "\n";
}

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

    int t{1};
    cin >> t;
    while (t--) {
        solve();
    }
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

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

void solve() {
    int n;
    cin >> n;
    vector<int> deg(n + 1);
    vector<vector<int>> a(n + 1, vector<int>());
    
    for (int i{1}; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        ++deg[u];
        ++deg[v];
        a[u].push_back(v);
        a[v].push_back(u);
    }

    int max_deg{};
    vector<int> deg_cnt(n);
    for (int i{1}; i < n; ++i) {
        max_deg = max(max_deg, deg[i]);
        ++deg_cnt[deg[i]];
    }

    int ans{};

    for (int u{1}; u <= n; ++u) {
        if (deg[u] == max_deg && deg_cnt[max_deg] == 1) {
            continue;
        }

        for (int v : a[u]) {
            --deg_cnt[deg[v]];
        }
        --deg_cnt[deg[u]];

        ans = max(ans, deg[u] + max_deg - int(deg_cnt[max_deg] == 0) - 1);

        for (int v : a[u]) {
            ++deg_cnt[deg[v]];
        }
        ++deg_cnt[deg[u]];
    }

    cout << ans << "\n";
}

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

    int t{1};
    cin >> t;
    while (t--) {
        solve();
    }
}


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

void solve() {
    int n;
    cin >> n;
    vector<int> deg(n + 1);
    vector<vector<int>> a(n + 1, vector<int>());
    
    for (int i{1}; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        ++deg[u];
        ++deg[v];
        a[u].push_back(v);
        a[v].push_back(u);
    }

    int max_deg{};
    vector<int> deg_cnt(n);
    for (int i{1}; i <= n; ++i) {
        max_deg = max(max_deg, deg[i]);
        ++deg_cnt[deg[i]];
    }

    int ans{};

    for (int u{1}; u <= n; ++u) {
        if (deg[u] == max_deg && deg_cnt[max_deg] == 1) {
            continue;
        }

        for (int v : a[u]) {
            --deg_cnt[deg[v]];
        }
        --deg_cnt[deg[u]];

        ans = max(ans, deg[u] + max_deg - int(deg_cnt[max_deg] == 0) - 1);

        for (int v : a[u]) {
            ++deg_cnt[deg[v]];
        }
        ++deg_cnt[deg[u]];
    }

    cout << ans << "\n";
}

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

    int t{1};
    cin >> t;
    while (t--) {
        solve();
    }
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)


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

void solve() {
    int n;
    cin >> n;
    vector<int> deg(n + 1);
    vector<vector<int>> a(n + 1, vector<int>());
    
    for (int i{1}; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        ++deg[u];
        ++deg[v];
        a[u].push_back(v);
        a[v].push_back(u);
    }

    int max_deg{};
    vector<int> deg_cnt(n);
    for (int i{1}; i < n; ++i) {
        max_deg = max(max_deg, deg[i]);
        ++deg_cnt[deg[i]];
    }

    int ans{};

    for (int u{1}; u <= n; ++u) {
        if (deg[u] == max_deg && deg_cnt[max_deg] == 1) {
            continue;
        }

        for (int v : a[u]) {
            --deg_cnt[deg[v]];
        }

        ans = max(ans, deg[u] + max_deg - int(deg_cnt[max_deg] == 0) - 1);

        for (int v : a[u]) {
            ++deg_cnt[deg[v]];
        }
    }

    cout << ans << "\n";
}

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

    int t{1};
    cin >> t;
    while (t--) {
        solve();
    }
}