HDU 3639 Hawk-and-Chicken,hawkandeagle

來源:互聯網
上載者:User

HDU 3639 Hawk-and-Chicken,hawkandeagle
Hawk-and-ChickenTime Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1979    Accepted Submission(s): 570


Problem DescriptionKids 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. 
InputThere 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. 
OutputFor 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
 
AuthorDragon 
Source2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU解題思路:首先是強連通分量縮點,然後重新構圖,對於同一個強連通分量的點的支援度一是強連通分量內的其他點,還有其他的強連通分量對他所在強連通分量的支援,為了找到這個我們之前構圖時反向建圖,然後bfs
#include<iostream>#include<cstdio>#include<cstring>#include<stack>#include<queue>using namespace std;#define Max 5005int head[Max],head2[Max],j,k,bcnt,dindex,dfn[Max],low[Max],belong[Max],sup[Max];bool visit[Max],vis[Max];;stack<int> s;struct{    int s;    int e;    int next;}edge[Max*6],edge2[Max*6];void add(int s,int e){    edge[k].s=s;    edge[k].e=e;    edge[k].next=head[s];    head[s]=k++;}void add2(int s,int e){    edge2[j].s=s;    edge2[j].e=e;    edge2[j].next=head2[s];    head2[s]=j++;}void tarjan(int i){    int ed;    dfn[i]=low[i]=++dindex;    visit[i]=true;    s.push(i);    for(int t=head[i];t!=-1;t=edge[t].next)    {        ed=edge[t].e;        if(!dfn[ed])        {            tarjan(ed);            if(low[i]>low[ed])                low[i]=low[ed];        }        else if(visit[ed]&&low[i]>dfn[ed])            low[i]=dfn[ed];    }    if(dfn[i]==low[i])    {        bcnt++;        do        {            ed=s.top();            s.pop();            visit[ed]=false;            belong[ed]=bcnt;        }while(i!=ed);    }}void solve(int n){    int i;    bcnt=dindex=0;    memset(visit,false,sizeof(visit));    memset(low,0,sizeof(low));    memset(belong,0,sizeof(belong));    memset(dfn,0,sizeof(dfn));    while(!s.empty())        s.pop();    for(i=0;i<n;i++)        if(!dfn[i])            tarjan(i);}int bfs(int rt){    int i,st,ed,sum=0;    vis[rt]=true;    queue<int> q;    q.push(rt);    while(!q.empty())    {        st=q.front();        q.pop();        for(i=head2[st];i!=-1;i=edge2[i].next)        {            ed=edge2[i].e;            if(!vis[ed])            {                sum+=sup[ed]+1;                q.push(ed);                vis[ed]=true;            }        }    }    return sum;}int main(){    int i,t,m,n,a,b,ans[Max],sum[Max],flag,cnt,p[Max],ncase=1;    scanf("%d",&t);    while(t--)    {        flag=-1;        j=k=cnt=0;        memset(p,0,sizeof(p));        memset(ans,0,sizeof(ans));        memset(sum,0,sizeof(sum));        memset(head,-1,sizeof(head));        memset(head2,-1,sizeof(head2));        memset(sup,0,sizeof(sup));        scanf("%d%d",&n,&m);        while(m--)        {            scanf("%d%d",&a,&b);            add(a,b);        }        solve(n);        for(i=0;i<k;i++)        {            int st=edge[i].s;            int ed=edge[i].e;            if(belong[st]!=belong[ed])                add2(belong[ed],belong[st]);        }        for(i=0;i<n;i++)            sup[belong[i]]++;        for(i=1;i<=bcnt;i++)            sup[i]--;        for(i=1;i<=bcnt;i++)        {            memset(vis,false,sizeof(vis));            sum[i]=bfs(i)+sup[i];        }        for(i=0;i<n;i++)        {            ans[i]=sum[belong[i]];            flag=max(flag,ans[i]);        }        for(i=0;i<n;i++)        {            if(ans[i]==flag)                p[cnt++]=i;            //cout<<ans[i]<<endl;        }        printf("Case %d: %d\n",ncase++,flag);        for(i=0;i<cnt;i++)            printf("%d%c",p[i],i==cnt-1?'\n':' ');    }    return 0;}


 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.