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

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

using namespace std;
using ll = long long int ;
using pii = pair<int,int> ;

void dfs(const vector<vector<int> > &adj , vector<int> &dp , vector<int> &prev , int v , int p = -1)
{
    prev[v] = p ;

    for(int to : adj[v])
    {
        if(to != p)
        {
            dfs(adj , dp , prev , to , v) ;
            dp[v] = max(dp[v] ,
                        dp[to] ) ;
        }
    }

    dp[v] = max(dp[v] ,
                int(adj[v].size()) ) ;
}

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

    vector<vector<int> > adj(n + 1) ;
    vector<int> dp(n + 1 , 0) ;
    vector<int> prev(n + 1) ;
    for(int i = 1 ; i < n ; ++i)
    {
        int u , v ;
        cin >> u >> v ;

        adj[u].push_back(v) ;
        adj[v].push_back(u) ;
    }

    dfs(adj , dp , prev , 1 , -1) ;

    int ans = 0 ;

    for(int i = 1 ; i <= n ; ++i)
    {
        for(int to : adj[i])
        {
            if(to == prev[i])
                continue ;

            ans = max(ans ,
                      int(adj[i].size()) + int(adj[to].size()) - 2) ;

            for(int to2 : adj[to])
            {
                if(to2 == i)
                    continue ;

                ans = max(int(adj[i].size()) + dp[to2] - 1 ,
                          ans ) ;


            }
        }
    }

    cout << ans << "\n" ;

}

int main()
{
    int t ;
    cin >> t ;

    while(t--)
        solve() ;

    return 0;
}


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

using namespace std;
using ll = long long int ;
using pii = pair<int,int> ;

void dfs(const vector<vector<int> > &adj , vector<int> &dp , vector<int> &prev , int v , int p = -1)
{
    prev[v] = p ;

    for(int to : adj[v])
    {
        if(to != p)
        {
            dfs(adj , dp , prev , to , v) ;
            dp[v] = max(dp[v] ,
                        dp[to] ) ;
        }
    }

    dp[v] = max(dp[v] ,
                int(adj[v].size()) ) ;
}

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

    vector<vector<int> > adj(n + 1) ;
    vector<int> dp(n + 1 , 0) ;
    vector<int> prev(n + 1) ;
    for(int i = 1 ; i < n ; ++i)
    {
        int u , v ;
        cin >> u >> v ;

        adj[u].push_back(v) ;
        adj[v].push_back(u) ;
    }

    dfs(adj , dp , prev , 1 , -1) ;

    int ans = 0 ;

    for(int i = 1 ; i <= n ; ++i)
    {
        for(int to : adj[i])
        {
            if(to == prev[i])
                continue ;

            ans = max(ans ,
                      int(adj[i].size()) + int(adj[to].size()) - 2) ;

            for(int to2 : adj[to])
            {
                if(to2 == i)
                    continue ;

                ans = max(int(adj[i].size()) + dp[to2] - 1 ,
                          ans ) ;


            }
        }
    }

    for(int i = 1 ; i <= n ; ++i)
    {
        pii k = {-100000 , -100000} ;
        for(int to : adj[i])
        {
            if(to == prev[i])
                continue ;


            if(dp[to] >= k.first)
            {
                k = {dp[to] , k.first} ;
            }
            else
            {
                if(dp[to] >= k.second)
                {
                    k = {k.first , dp[to]} ;
                }
            }
        }

        ans = max(ans ,
                  k.first + k.second  - 1) ;

    }

    cout << ans << "\n" ;

}

int main()
{
    int t ;
    cin >> t ;

    while(t--)
        solve() ;

    return 0;
}