标签:std += read == const define signed str ons
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define int long long
const int N=4e6+10;
int e[N],ne[N],h[N],idx;
int depth[N];
int ans[N];
int size[N];
int n;
char buf[1<<15],*fs,*ft;
inline char getc()
{
return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
}
inline int read()
{
int x=0,f=1;
char ch=getc();
while(ch<‘0‘||ch>‘9‘)
{
if(ch==‘-‘)f=-1;
ch=getc();
}
while(ch>=‘0‘&&ch<=‘9‘)
{
x=x*10+ch-‘0‘;
ch=getc();
}
return x*f;
}
void add(int a,int b)
{
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
void dfs1(int u,int fa)
{
depth[u]=depth[fa]+1;
size[u]=1;
for(int i=h[u];i!=-1;i=ne[i])
{
int j=e[i];
if(j==fa)
continue;
dfs1(j,u);
size[u]+=size[j];
}
}
void dfs2(int u,int fa)
{
for(int i=h[u];i!=-1;i=ne[i])
{
int j=e[i];
if(j==fa)
continue;
ans[j]=ans[u]-size[j]+n-size[j];
dfs2(j,u);
}
}
signed main()
{
n=read();
for(int i=0;i<=n;i++)
h[i]=-1;
for(int i=1;i<n;i++)
{
int a=read(),b=read();
add(a,b);
add(b,a);
}
dfs1(1,0);
for(int i=1;i<=n;i++)
ans[1]+=depth[i];
dfs2(1,0);
int maxv=0;
int poj=0;
for(int i=1;i<=n;i++)
if(ans[i]>maxv)
{
maxv=ans[i];
poj=i;
}
cout<<poj<<endl;
}
luogu P3478 [POI2008]STA-Station 换根dp
标签:std += read == const define signed str ons
原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12790081.html