1 solutions

  • 0
    @ 2026-7-28 15:25:59
    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                               神兽保佑            永无BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
     */
    #include<bits/stdc++.h>
    using namespace std;
    #define MOD         998244353
    #define speMOD      2933256077ll
    #define int         long long
    #define pii         pair<int,int>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
    #define over(x)     {cout<<x<<endl;return;}
    #define lowbit(x)   ((x)&(-(x)))
    #define cntbit(x)   __builtin_popcount(x)
    #define rf(v)       shuffle(all(v),sd);
    mt19937 sd(std::random_device{}());
    int qpow (int a,int b,int m=MOD,int res=1){
    	a%=m;
    	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
    	return res;
    }
    string YN(bool x,string Y="Yes",string N="No") {
    	if(x){
    		return Y;
    	} else {
    		return N;
    	}
    }
    int fac[2000005],inv[2000005];
    void init(int n){
    	fac[0]=inv[0]=1;
    	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
    	inv[n]=qpow(fac[n],MOD-2);
    	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
    }
    int binom(int x,int y){
    	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
    }
    int read(){
    	char c=getchar();int res=0;
    	while(c<48||c>57)c=getchar();
    	do res=(res<<1)+(res<<3)+(c^48),c=getchar();while(c>=48&&c<=57);
    	return res;
    }
    int n;
    vector<int>v[1000005];
    int sz[1000005],dp[1000005],ans[1000005],fa[1000005];
    int ms,ps;
    void dfs(int x,int pre){
    	sz[x]=1;ans[x]=0;fa[x]=pre;
    	for(auto i:v[x])if(i!=pre)dfs(i,x),sz[x]+=sz[i],ans[x]+=ans[i]+dp[i];
    	vector<int>t;
    	for(auto i:v[x])if(i!=pre)t.pb(sz[i]);
    	sort(all(t));
    	int a=0,b=0;
    	for(auto i:t){
    		if(a>b)swap(a,b);
    		ans[x]+=a;a+=i;
    	}
    	dp[x]=min(a,b)+1;
    	int m=n-sz[x];if(t.size())m=max(m,t[t.size()-1]);
    	if(m<ms)ms=m,ps=x;
    }
    int res;
    vector<int>e;
    void Main() {
    	n=read();
    	REP(i,0,n)v[i].clear();
    	REP(i,1,n){
    		int x=read()-1,y;y=read()-1;
    		v[x].pb(y);v[y].pb(x);
    	}
    	ps=-1;ms=n;
    	dfs(0,-1);dfs(ps,-1);
    	printf("%lld\n",ans[ps]);
    }
    void TC() {
    	int tc=read();
    	while(tc--){
    		Main();
    		cout.flush();
    	}
    }
    signed main() {
    	freopen("permutation.in","r",stdin);
    	freopen("permutation.out","w",stdout);
    	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
    }
    /*
    1. CLEAR the arrays (ESPECIALLY multitests)
    2. DELETE useless output
     */
    
    • 1

    Information

    ID
    2916
    Time
    2000ms
    Memory
    512MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    2
    Accepted
    2
    Uploaded By