Hawk-and-Chicken
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4170 Accepted Submission(s): 1301
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
Each test case start with two integer n, m in a line (2 <= n <= 5000, 0
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 total
supports 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
2 4 3 3 2 2 0 2 1 3 3 1 0 2 1 0 2
Sample Output
Case 1: 2 0 1 Case 2: 2 0 1 2
Author
Dragon
Source
2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU
分析:
题目意思:
给你一个有向图,a可以到b,b可以到c,那么c点就可以得到两分
问你得到分数最高的点是哪些点(升序输出),且最高的得分是多少
给你一个有向图,a可以到b,b可以到c,那么c点就可以得到两分
问你得到分数最高的点是哪些点(升序输出),且最高的得分是多少
分析:
对于某个点x
就是算有多少个点可以到x
直接dfs的话,不行,得反向建图,原因:
正向建图的话,对x点,有的点因为dfs顺序导致点标记的原因,导致某些点不到到达x
比如图:
1->x
2->x
3->1
3->2
如果是先dfs1,然后dfs2的话,1和2都被标记了,不能通过了,导致3不能到达x,实际上3能到x
反向建图的话,只要看x能到哪些点就可以了,可以直接dfs,不存在上述麻烦
对于某个点x
就是算有多少个点可以到x
直接dfs的话,不行,得反向建图,原因:
正向建图的话,对x点,有的点因为dfs顺序导致点标记的原因,导致某些点不到到达x
比如图:
1->x
2->x
3->1
3->2
如果是先dfs1,然后dfs2的话,1和2都被标记了,不能通过了,导致3不能到达x,实际上3能到x
反向建图的话,只要看x能到哪些点就可以了,可以直接dfs,不存在上述麻烦
只要反向建图,不论是否有强连通分量,都是可以直接dfs的!!!!
是对出度为0的点dfs
但是看看数据范围,直觉应该是会超时....
然后来了一发,果然超时
是对出度为0的点dfs
但是看看数据范围,直觉应该是会超时....
然后来了一发,果然超时
所以我们得进行优化
优化方法:
对强连通分量进行缩点处理
对某个强连通分量,假设该强连通分量有k个点,那么该强连通分量的每个点的得分都是k-1
所以我们缩点,然后重新建图,得到一个没有强连通分量的有向图
然后再跑dfs
所以我们用tarjan算法进行缩点重新建图处理
优化方法:
对强连通分量进行缩点处理
对某个强连通分量,假设该强连通分量有k个点,那么该强连通分量的每个点的得分都是k-1
所以我们缩点,然后重新建图,得到一个没有强连通分量的有向图
然后再跑dfs
所以我们用tarjan算法进行缩点重新建图处理
code:
#include#include #include #include<string.h> #include<set> #include