HDU 3639 Hawk-and-Chicken(Tarjan缩点+反向DFS)
来源:互联网 发布:香港科技大学gpa 知乎 编辑:程序博客网 时间:2024/06/05 07:50
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.
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.
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.
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=>A->C的,最后谁得到的多就选谁。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=5000+100;const int maxm=100000+10;struct node{ int u,v; int next;}e[maxm],e1[maxn];int head[maxn],cntE,cntF;int DFN[maxn],low[maxn],h[maxn];int s[maxm],top,index,cnt;int belong[maxn],instack[maxn];int dp[maxn],in[maxn],vis[maxn];int num[maxn];int n,m;void init(){ top=cntE=cntF=0; index=cnt=0; CLEAR(DFN,0); CLEAR(head,-1); CLEAR(instack,0);}void addedge(int u,int v){ e[cntE].u=u;e[cntE].v=v; e[cntE].next=head[u]; head[u]=cntE++;}void Tarjan(int u){ DFN[u]=low[u]=++index; instack[u]=1; s[top++]=u; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].v; if(!DFN[v]) { Tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]) low[u]=min(low[u],DFN[v]); } int v; if(DFN[u]==low[u]) { cnt++; do{ v=s[--top]; belong[v]=cnt; instack[v]=0; }while(u!=v); }}int dfs(int x){ int ans=num[x]; for(int i=h[x];i!=-1;i=e1[i].next) { int v=e1[i].v; if(!vis[v]) { vis[v]=1; ans+=dfs(v); } } return ans;}void work(){ REP(i,n) if(!DFN[i]) Tarjan(i); if(cnt==1) { printf("%d\n",n-1); REP(i,n) printf(i==n-1?"%d\n":"%d ",i); return ; } CLEAR(num,0); CLEAR(dp,0); CLEAR(in,0); CLEAR(h,-1); REP(i,n)//马丹,这里卡了我两天 num[belong[i]]++; REP(k,n) { for(int i=head[k];i!=-1;i=e[i].next) { int v=e[i].v; if(belong[k]!=belong[v])//反向建边dfs. {// cout<<"666 "<<endl; e1[cntF].u=belong[v]; e1[cntF].v=belong[k]; e1[cntF].next=h[belong[v]]; h[belong[v]]=cntF++; in[belong[k]]++; } } } REPF(i,1,cnt) { if(!in[i]) { CLEAR(vis,0); dp[i]=dfs(i)-1; } } int ans=0; REPF(i,1,cnt) ans=max(ans,dp[i]); printf("%d\n",ans); int flag=0; REP(i,n) { if(dp[belong[i]]==ans) { if(!flag) printf("%d",i),flag=1; else printf(" %d",i); } } printf("\n");}int main(){ int t,u,v; int cas=1; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); init(); for(int i=0;i<m;i++) { scanf("%d%d",&u,&v); addedge(u,v); } printf("Case %d: ",cas++); work(); } return 0;}
0 0
- HDU 3639 Hawk-and-Chicken(Tarjan缩点+反向DFS)
- HDU 3639 Hawk-and-Chicken tarjan缩点+dfs
- HDU 3639 Hawk-and-Chicken 强联通缩点 + DFS
- [HDU3639]Hawk-and-Chicken(Tarjan缩点+dfs)
- HDU3639 Hawk-and-Chicken Tarjan缩点 +dfs+贪心
- hdu 3639 Hawk-and-Chicken【强联通Tarjan+Dfs】
- Hawk-and-Chicken (hdu 3639 强连通缩点+反向建图DFS)
- [tarjan] hdu 3639 Hawk-and-Chicken
- 【HDU】3639 Hawk-and-Chicken 强连通缩点+DFS
- hdu 3639 Hawk-and-Chicken(缩点+树dp)
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
- hdu 3639 Hawk-and-Chicken 【强连通分量+反向建图dfs】
- HDU 3639 Hawk-and-Chicken
- HDU - 3639 Hawk-and-Chicken(dfs+强连通分量)
- HDU 3639Hawk-and-Chicken 强连通分量分解 + dfs
- HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 && 求传递的最大值】
- java 10.18.2
- Android Widget支持的布局和控件以及所占单元格计算
- Android WifiDisplay分析二:Wifi display连接过程
- 双系统重新安装后修复引导xp、win7、ubuntu
- CentOS 6.3(x86_64)下安装Oracle 10g R2
- HDU 3639 Hawk-and-Chicken(Tarjan缩点+反向DFS)
- BestCoder Round #14
- 学生课堂经验值管理系统
- booth乘法器原理
- bzoj 1768: [Ceoi2009]logs
- Eclipse如何自动生成注释
- java ajax请求示例
- C语言知识查漏补缺与心得体会(3)
- Linux系统启动那些事—基于Linux 3.10内核