Hawk-and-Chicken (hdu 3639 强连通缩点+反向建图DFS)
来源:互联网 发布:淘宝子账号权重值500 编辑:程序博客网 时间:2024/06/07 17:59
Hawk-and-Chicken
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2261 Accepted Submission(s): 663
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
Author
Dragon
Source
2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU
Recommend
lcy | We have carefully selected several similar problems for you: 3634 3635 3633 3636 3637
题意:n个人投票,告诉投票关系问哪些人的票数最多,票可以传递,比如A投给B,B得一票,B又投给C,那么C得两票。
思路:先强连通缩点,在同一个连通分量里的传递出去的票数为连通分量内点的个数-1(自己不算),缩点后重新建图,这个时候需要反向建图,这样入度为零的点票数才可能最大,可以dfs解决,求最大值。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b) for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n) scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf printf#define DBG pf("Hi\n")typedef long long ll;using namespace std;#define INF 0x3f3f3f3f#define mod 1000000009const int maxn = 1005;const int MAXN = 5050;const int MAXM = 60600;struct Edge{ int to,next;}edge[MAXM],e[MAXM];int head[MAXN],tot;int DFN[MAXN],Low[MAXN],Stack[MAXN],Belong[MAXN],num[MAXN];bool Instack[MAXN],vis[MAXN];int scc,Index,top;int all[MAXN];int hed[MAXN],cnt;int n,m;int indegree[MAXN];void init(){ tot=cnt=0; memset(head,-1,sizeof(head)); memset(hed,-1,sizeof(hed));}void add(int u,int v){ e[cnt].to=v; e[cnt].next=hed[u]; hed[u]=cnt++;}void addedge(int u,int v){ edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++;}void Tarjan(int u){ int v; DFN[u]=Low[u]=++Index; Stack[top++]=u; Instack[u]=true; for (int i=head[u];~i;i=edge[i].next) { v=edge[i].to; if (!DFN[v]) { Tarjan(v); if (Low[u]>Low[v]) Low[u]=Low[v]; } else if (Instack[v]&&Low[u]>DFN[v]) Low[u]=DFN[v]; } if (Low[u]==DFN[u]) { ++scc; do{ v=Stack[--top]; Instack[v]=false; Belong[v]=scc; num[scc]++; }while (v!=u); } return ;}int dfs(int u){ vis[u]=true; int sum=num[u]; for (int i=hed[u];~i;i=e[i].next) { int v=e[i].to; if (!vis[v]) { sum+=dfs(v); } }// vis[u]=false; //开始写成回溯的了,WA了好多次=-= return sum;}void solve(int N){ memset(num,0,sizeof(num)); memset(indegree,0,sizeof(indegree)); memset(DFN,0,sizeof(DFN)); top=Index=scc=0; memset(Instack,false,sizeof(Instack)); for (int i=0;i<N;i++) if (!DFN[i]) Tarjan(i);// printf("s==%d\n",scc);// for (int i=0;i<N;i++)// printf(" %d",Belong[i]);// printf("\n"); for (int u=0;u<N;u++) { for (int i=head[u];~i;i=edge[i].next) { int v=edge[i].to; if (Belong[u]==Belong[v]) continue; add(Belong[v],Belong[u]); //反向建图 indegree[Belong[u]]++; } } int Max=-1; memset(all,-1,sizeof(all)); for (int i=1;i<=scc;i++) { if (indegree[i]==0) { memset(vis,false,sizeof(vis)); all[i]=dfs(i);// printf("**%d\n",all[i]); Max=max(Max,all[i]); } } printf("%d\n",Max-1); int flag=1; for (int i=0;i<N;i++) { if (all[Belong[i]]==Max) { if (flag){ printf("%d",i); flag=0; } else printf(" %d",i); } } printf("\n");}int main(){#ifndef ONLINE_JUDGE freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);#endif int i,j,t,u,v,cas=0; scanf("%d",&t); while (t--) { scanf("%d%d",&n,&m); init(); for (i=0;i<m;i++) { scanf("%d%d",&u,&v); addedge(u,v); } printf("Case %d: ",++cas); solve(n); } return 0;}
1 0
- Hawk-and-Chicken (hdu 3639 强连通缩点+反向建图DFS)
- 【HDU】3639 Hawk-and-Chicken 强连通缩点+DFS
- HDU 3639 Hawk-and-Chicken 强连通缩点+搜索
- hdu 3639 Hawk-and-Chicken 【强连通分量+反向建图dfs】
- HDU 3639 Hawk-and-Chicken(Tarjan缩点+反向DFS)
- HDU 3639 Hawk-and-Chicken 强联通缩点 + DFS
- HDU - 3639 Hawk-and-Chicken(dfs+强连通分量)
- HDU 3639Hawk-and-Chicken 强连通分量分解 + dfs
- hdu 3639 Hawk-and-Chicken【强联通Tarjan+Dfs】
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
- HDU3639 Hawk-and-Chicken 强连通+缩点+建反向图
- HDU 3639 Hawk-and-Chicken tarjan缩点+dfs
- HDU3639Hawk-and-Chicken (好题,强连通缩点,建图,DFS回溯)
- HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 && 求传递的最大值】
- HDU3639--Hawk-and-Chicken(强连通+缩点+反向图)
- HDU 3639 Hawk-and-Chicken(强连通)
- HDU 4597 Play Game
- oracle安装报错:Could not retrieve local nodename.
- MBR与GPT分区格式(实例-创建大于2TB的分区)
- test
- java获取当前路径
- Hawk-and-Chicken (hdu 3639 强连通缩点+反向建图DFS)
- iOS 【transform属性】-通过tramsform熟悉个可以修改对象的平移、缩放比例和旋转角度
- HDU 1241 Oil Deposits【dfs最大联通块(洪水算法)】
- android应用生命周期
- HDU5348 MZL's endless loop (搜索)
- 不解压直接读取压缩包中的文件
- 菜鸟学Android(四十四):jsp标签技术简介及jsp标签的开发详解
- HTTPS、SPDY和HTTP/2的性能比较
- Python 爬虫