poj 3107 Godfather 求树的所有重心 树形DP基础题

来源:互联网 发布:淘宝什么是淘气值 编辑:程序博客网 时间:2024/05/29 02:55
Godfather
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 5390 Accepted: 1880

Description

Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders.

Unfortunately, the structure of Chicago mafia is rather complicated. There are n persons known to be related to mafia. The police have traced their activity for some time, and know that some of them are communicating with each other. Based on the data collected, the chief of the police suggests that the mafia hierarchy can be represented as a tree. The head of the mafia, Godfather, is the root of the tree, and if some person is represented by a node in the tree, its direct subordinates are represented by the children of that node. For the purpose of conspiracy the gangsters only communicate with their direct subordinates and their direct master.

Unfortunately, though the police know gangsters’ communications, they do not know who is a master in any pair of communicating persons. Thus they only have an undirected tree of communications, and do not know who Godfather is.

Based on the idea that Godfather wants to have the most possible control over mafia, the chief of the police has made a suggestion that Godfather is such a person that after deleting it from the communications tree the size of the largest remaining connected component is as small as possible. Help the police to find all potential Godfathers and they will arrest them.

Input

The first line of the input file contains n — the number of persons suspected to belong to mafia (2 ≤ n ≤ 50 000). Let them be numbered from 1 to n.

The following n − 1 lines contain two integer numbers each. The pair aibi means that the gangster ai has communicated with the gangster bi. It is guaranteed that the gangsters’ communications form a tree.

Output

Print the numbers of all persons that are suspected to be Godfather. The numbers must be printed in the increasing order, separated by spaces.

Sample Input

61 22 32 53 43 6

Sample Output

2 3

Source

Northeastern Europe 2005, Northern Subregion



一开始用vector储存边,set储存答案,结果TLE

#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI 3.1415926535897932384626#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)#define  lson   num<<1,le,mid#define rson    num<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)using namespace std;const int INF =0x3f3f3f3f;const int maxn=  50000+10   ;//const int maxm=    ;//const int INF=    ;typedef long long ll;const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);//by yskysker123int fir[maxn];int nex[2*maxn];int u[2*maxn],v[2*maxn];//当形成树的时候,无向边拆成有向边,要*2int ans[maxn];int mini,n,cnt;int dp[maxn],sum[maxn];bool vis[maxn];int e_max;inline void add_son(int s,int t){    int e=e_max++;    u[e]=s;    v[e]=t;    nex[e]=fir[s];    fir[s]=e;}void dfs(int x){    sum[x]=1;    dp[x]=0;    for(int e=fir[x];~e;e=nex[e])    {        int y=v[e];        if(vis[y])  continue;        vis[y]=1;        dfs(y);        sum[x]+=sum[y];        dp[x]=max(dp[x],sum[y]);    }    dp[x]=max(dp[x],n-sum[x]);    if(dp[x]<mini)    {        mini=dp[x];        cnt=0;        ans[cnt++]=x;    }    else if(dp[x]==mini)    {        ans[cnt++]=x;    }}int main(){    int x,y;    while(~scanf("%d",&n))    {        memset(fir,-1,sizeof fir);        e_max=0;        for(int i=1;i<=n-1;i++)            {                scanf("%d%d",&x,&y);                add_son(x,y);                add_son(y,x);            }        memset(vis,0,sizeof vis);        mini=INF;        vis[1]=1;        cnt=0;        dfs(1);        sort(ans,ans+cnt);        for(int i=0;i<cnt;i++)        {            if(i==0)            printf("%d",ans[0]);            else            printf(" %d",ans[i]);        }        putchar('\n');    }    return 0;}

用vector储存边也会超时


#include<cstdio>#include<string>#include<cstring>#include<algorithm>#include<vector>using namespace std;typedef long long ll;const int INF =0x3f3f3f3f;const int maxn=50000    ;int n,cnt;int fir[maxn+3],nex[maxn*2+4],e_max,u[2*maxn+4],v[2*maxn+4];int dp[maxn+3],ans[maxn+3];int mini;void add_edge(int x,int y){    int e=e_max++;    nex[e]=fir[x];    fir[x]=e;    u[e]=x;    v[e]=y;}void print(int x){    if(cnt++)  putchar(' ');    printf("%d",x);}void init(){    e_max=0;    memset(fir,-1,sizeof fir);    cnt=0;    mini=INF;}void dfs(int x,int fa){    dp[x]=1;    ans[x]=0;    for(int e=fir[x];~e;e=nex[e])    {        int y=v[e];if(y==fa)  continue;        dfs(y,x);        dp[x]+=dp[y];        ans[x]=max(ans[x],dp[y]);    }    ans[x]=max(n-dp[x],ans[x]);    if(ans[x]<mini)    {        mini=ans[x];    }}int main(){    int x,y;    while(~scanf("%d",&n))    {        init();        for(int i=1;i<n;i++)        {            scanf("%d%d",&x,&y);            add_edge(x,y);            add_edge(y,x);        }            dfs(1,-1);                                    for(int i=1;i<=n;i++)            {                if(ans[i]==mini)  print(i);            }             putchar('\n');                 }   return 0;}


0 0
原创粉丝点击