hdu 3639 Hawk-and-Chicken(缩点+树dp)

来源:互联网 发布:mac pro2012装win7 编辑:程序博客网 时间:2024/06/05 00:49

Hawk-and-Chicken

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1418    Accepted Submission(s): 395


Problem Description
Kids in kindergarten enjoy playing a game called Hawk-and-Chicken. But there always exists a big problem: every kid in this game want to play the role of Hawk. 
So the teacher came up with an idea: Vote. Every child have some nice handkerchiefs, and if he/she think someone is suitable for the role of Hawk, he/she gives a handkerchief to this kid, which means this kid who is given the handkerchief win the support. Note the support can be transmitted. Kids who get the most supports win in the vote and able to play the role of Hawk.(A note:if A can win
support from B(A != B) A can win only one support from B in any case the number of the supports transmitted from B to A are many. And A can't win the support from himself in any case.
If two or more kids own the same number of support from others, we treat all of them as winner.
Here's a sample: 3 kids A, B and C, A gives a handkerchief to B, B gives a handkerchief to C, so C wins 2 supports and he is choosen to be the Hawk.
 

Input
There are several test cases. First is a integer T(T <= 50), means the number of test cases.
Each test case start with two integer n, m in a line (2 <= n <= 5000, 0 <m <= 30000). n means there are n children(numbered from 0 to n - 1). Each of the following m lines contains two integers A and B(A != B) denoting that the child numbered A give a handkerchief to B.
 

Output
For each test case, the output should first contain one line with "Case x:", here x means the case number start from 1. Followed by one number which is the totalsupports the winner(s) get. 
Then follow a line contain all the Hawks' number. The numbers must be listed in increasing order and separated by single spaces.
 

Sample Input
24 33 22 02 13 31 02 10 2
 

Sample Output
Case 1: 20 1Case 2: 20 1 2
题意:要进行一场投票,统计谁得到的票数最多。投票具有传递性,而且自己不能投票给自己,如果A投票给B,B投票给C,那么B得1票,C得两票。
思路:根据传递性,进行强连通缩点,同一个连通分量中所有人获得票数相同。缩点后,对于新的图建立反图,在反图中找入度为0的点,可以证明,入度为0的点得到的票数肯定比其他点多。对入度为0的点进行dfs,累加子节点的权值,就得到该点所获得的总票数。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <cstdlib>#include <map>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define ll long longusing namespace std;const int maxn=5005;const int maxm=30005;const int INF=1000000000;struct node{    int v,next;}edge[maxm*2];int G[maxn],RG[maxn],in[maxn],dp[maxn],vote[maxn];int low[maxn],dfn[maxn],stack[maxn],scc[maxn];bool ins[maxn],vis[maxn];int n,num,m,snum,top,cnt;void init(){    memset(G,-1,sizeof(G));    memset(RG,-1,sizeof(RG));    num=0;}void add(int *head,int u,int v){    edge[num].v=v;    edge[num].next=head[u]++;    head[u]=num++;}void input(){    int a,b;    scanf("%d%d",&n,&m);    while(m--)    {        scanf("%d%d",&a,&b);        add(G,a,b);    }}void dfs(int u){    int x;    dfn[u]=low[u]=++cnt;    stack[top++]=u;    ins[u]=true;    for(int i=G[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        if(!dfn[v])        {            dfs(v);            low[u]=min(low[u],low[v]);        }        else if(ins[v])        low[u]=min(low[u],dfn[v]);    }    if(low[u]==dfn[u])    {        snum++;        do{            x=stack[--top];            ins[x]=false;            scc[x]=snum;            dp[snum]++;        }while(x!=u);    }}void tarjan(){    memset(dfn,0,sizeof(dfn));    memset(ins,false,sizeof(ins));    memset(dp,0,sizeof(dp));    cnt=top=snum=0;    for(int i=0;i<n;i++)    if(!dfn[i]) dfs(i);}int dfs1(int u){    int sum=dp[u];    vis[u]=true;    for(int i=RG[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        if(!vis[v]) sum+=dfs1(v);    }    return sum;}void solve(){    int ans=0,j;    memset(in,0,sizeof(in));   for(int u=0;u<n;u++)   for(int i=G[u];i!=-1;i=edge[i].next)   {       int v=edge[i].v;       if(scc[u]!=scc[v])       {           add(RG,scc[v],scc[u]);           in[scc[u]]++;       }   }   memset(vote,0,sizeof(vote));   for(int i=1;i<=snum;i++)   if(!in[i])   {       memset(vis,false,sizeof(vis));       vote[i]=dfs1(i)-1;       if(vote[i]>ans)       ans=vote[i];   }   printf("%d\n",ans);   for(j=0;j<n;j++)   if(vote[scc[j]]==ans)   {       printf("%d",j);       break;   }   for(++j;j<n;j++)   if(vote[scc[j]]==ans)       printf(" %d",j);    printf("\n");}int main(){    int t,c=0;    scanf("%d",&t);   while(t--)   {       init();       input();       tarjan();       printf("Case %d: ",++c);       solve();   }   return 0;}


原创粉丝点击