bzoj1131: [POI2008]Sta
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                bzoj1131: [POI2008]Sta
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                二次掃描+換根
有點困思路不清晰啊
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; typedef long long LL;struct node {int x,y,next; }a[2100000];int len,last[1100000]; void ins(int x,int y) {len++;a[len].x=x;a[len].y=y;a[len].next=last[x];last[x]=len; } int fa[1100000],dep[1100000],tot[1100000]; LL sum[1100000]; void dfs(int x) {tot[x]=1;for(int k=last[x];k;k=a[k].next){if(a[k].y!=fa[x]){fa[a[k].y]=x;dep[a[k].y]=dep[x]+1;dfs(a[k].y);sum[x]+=sum[a[k].y]+tot[a[k].y];tot[x]+=tot[a[k].y];}} } LL mmax,now,cnt;int id; void changert(int x) {if(now+sum[x]>mmax)mmax=now+sum[x],id=x;else if(now+sum[x]==mmax)id=min(id,x);for(int k=last[x];k;k=a[k].next){if(a[k].y!=fa[x]){cnt+=tot[x]-tot[a[k].y];now+=(sum[x]-(sum[a[k].y]+tot[a[k].y]))+cnt;changert(a[k].y);now-=(sum[x]-(sum[a[k].y]+tot[a[k].y]))+cnt;cnt-=tot[x]-tot[a[k].y];}} } int main() {freopen("a.in","r",stdin);freopen("a.out","w",stdout);int n,x,y;scanf("%d",&n);len=0;memset(last,0,sizeof(last));for(int i=1;i<n;i++){scanf("%d%d",&x,&y);ins(x,y),ins(y,x);}fa[1]=0;dep[1]=1;dfs(1);mmax=0,id=(1<<30),now=0,cnt=0;changert(1);printf("%d\n",id);return 0; }?
轉載于:https://www.cnblogs.com/AKCqhzdy/p/9877434.html
總結
以上是生活随笔為你收集整理的bzoj1131: [POI2008]Sta的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: springboot 问题总结
- 下一篇: 24-Thief小偷-Crime犯罪
