HDU3639 Hawk-and-Chicken 强连通+缩点+建反向图
来源:互联网 发布:骑马与砍杀哈劳斯数据 编辑:程序博客网 时间:2024/05/22 04:37
Hawk-and-Chicken
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2033 Accepted Submission(s): 588
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/*HDU 3639 强连通+缩点+建反向图不会,查看别人;把杂乱的有向图通过缩点变成有向无环图,然后建反向图,并标记每个点的入度(最大值一定在反向图的入度为的点中)然后dfs,最后就是在原图中找等于MAX的点就可以了。 */#include<iostream>#include<stdio.h> #include<vector>#include<stack>const int MAXN=5000+10;using namespace std;vector<int>mp1[MAXN];//原图vector<int>mp2[MAXN];//反向图stack<int>S;bool mark[MAXN];//标记元素是否在栈中int color[MAXN];//缩点,染色int to[MAXN];//入度int n,m,cnt,_count;int dfn[MAXN],low[MAXN];int num[MAXN];//用来保存所有的最大点int dp[MAXN];//用于保存每个强连通分量的点数//求强连通分量Tarjan算法void Tarjan(int u){ dfn[u]=low[u]=++cnt; mark[u]=true; S.push(u); for(int i=0;i<mp1[u].size();i++){ int v=mp1[u][i]; if(dfn[v]==0){ Tarjan(v); low[u]=min(low[u],low[v]); }else if(mark[v]){ low[u]=min(low[u],dfn[v]); } } if(low[u]==dfn[u]){ int v; do{ v=S.top(); S.pop(); mark[v]=false; color[v]=_count;//缩点,染色 dp[_count]++;//保存该强联通分量的点数 }while(u!=v); _count++; }}//计算反向图中每一个入度为0的点的最大值int dfs(int u){ mark[u]=true; cnt+=dp[u]; for(int i=0;i<mp2[u].size();i++){ int v=mp2[u][i]; if(!mark[v])dfs(v); } return cnt;}int main(){ int T,t=1,x,y; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ mp1[i].clear(); mp2[i].clear(); } for(int i=1;i<=m;i++){ scanf("%d%d",&x,&y); mp1[x].push_back(y);//建图 x->y } memset(mark,false,sizeof(mark)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(color,0,sizeof(color)); memset(dp,0,sizeof(dp)); memset(to,0,sizeof(to)); memset(num,0,sizeof(num)); _count=0; cnt=0; for(int i=0;i<n;i++){ if(dfn[i]==0) Tarjan(i); } //重新构图,建反向图 for(int i=0;i<n;i++){ for(int j=0;j<mp1[i].size();j++){ //不在同一个连通分量的点 if(color[i]!=color[mp1[i][j]]){ mp2[color[mp1[i][j]]].push_back(color[i]); to[color[i]]++;//入度 } } } printf("Case %d: ",t++); int MAX=-1,tag=0; for(int i=0;i<_count;i++){ //最大值一定在反向图中入度为0的点中 cnt=0; if(to[i]==0){ memset(mark,false,sizeof(mark)); int cnt=dfs(i); num[i]=cnt;//保存每一个入度0的最大值 MAX=max(MAX,cnt); } } printf("%d\n",MAX-1); //在原图中找最大的点 for(int i=0;i<n;i++){ if(num[color[i]]==MAX){ if(!tag){ printf("%d",i); tag=1; }else{ printf(" %d",i); } } } printf("\n"); } return 0;}
0 0
- HDU3639 Hawk-and-Chicken 强连通+缩点+建反向图
- HDU3639--Hawk-and-Chicken(强连通+缩点+反向图)
- hdu3639 Hawk-and-Chicken【强连通】
- 连通图之HDU3639 Hawk-and-Chicken
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
- HDU 3639 Hawk-and-Chicken(强连通分量+缩点)
- hdu 3639 Hawk-and-Chicken 【强连通分量+反向建图dfs】
- [HDU3639]Hawk-and-Chicken(Tarjan缩点+dfs)
- HDU3639 Hawk-and-Chicken Tarjan缩点 +dfs+贪心
- hdu3639 Hawk-and-Chicken
- hdu3639 Hawk-and-Chicken
- HDU3639 Hawk-and-Chicken
- Hawk-and-Chicken (hdu 3639 强连通缩点+反向建图DFS)
- hdu3639 Hawk-and-Chicken(Tarjan缩点+反图搜索)
- hdu3639 Hawk-and-Chicken (Tarjan)
- HDOJ 3639.Hawk-and-Chicken(Tarjan强连通分量+缩点)
- HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 && 求传递的最大值】
- 解决500 OOPS: vsftpd: cannot locate user specified in 'ftp_username':ftp的问题
- Java SE 第十讲(面向对象之封装) 续
- 读书笔记——程序员成长的烦恼(吴亮等)
- Eclipse调试Hbase程序“Failed to detect a valid hadoop home directory java.id.IOException”问题
- 再探Objective-C.4
- HDU3639 Hawk-and-Chicken 强连通+缩点+建反向图
- 解决linux 下php中xdebug的报错 :Xdebug MUST be loaded as a Zend extension in Unknown on line 0
- 编译内核使用make menuconfig时出错, 是一个目录 停止
- Arduino学习笔记3--用模拟IO口实现呼吸LED灯
- first time picture
- quartz定时任务
- java 线程组简介
- Android本地文档打开慢的解决方法
- Mac使用