Hawk-and-Chicken
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5031 Accepted Submission(s): 1530
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.
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.
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.
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
题目大意 : 输入一个有向图, 输出每个点可以被多少个点到达(不包括他自己)
思路 : 先缩点, 记录每个点集包含的点的个数, 然后遍历原图, 看两个点是否在同一个强连通分量当中, 如果不在, 反向建边, 最后遍历每个新点, 记录他能够到达多少个点, 再加上他自身点集包含的点的个数即可, 之所以要反向建边, 是因为你直接dp的话, 会把根节点重复算上,和一些反向建边的拓扑排序一个道理
Accepted code
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
vector <int> e[MAXN << 1];
vector <int> edge[MAXN << 1];
int dfn[MAXN], low[MAXN], n, m, ans, max_, T, X, C;
int dp[MAXN], suo[MAXN], pre[MAXN], tot, scnt;
stack <int> st;
bool vis[MAXN];
void init() {
for (int i = 0; i <= n; i++) e[i].clear(), edge[i].clear(), dfn[i] = low[i] = dp[i] = suo[i] = 0, vis[i] = false;
tot = scnt = max_ = C = 0;
}
void tarjan(int x, int fa) {
dfn[x] = low[x] = ++tot;
vis[x] = true; st.push(x);
for (int i = 0; i < SZ(e[x]); i++) {
int vi = e[x][i];
if (!dfn[vi]) {
tarjan(vi, x);
Min(low[x], low[vi]);
}
else if (vis[vi]) Min(low[x], dfn[vi]);
}
if (dfn[x] == low[x]) {
scnt++; int k;
do {
k = st.top(); st.pop();
dp[scnt]++, suo[k] = scnt;
vis[k] = false;
} while (k != x);
}
}
void dfs(int x) {
vis[x] = true;
for (int i = 0; i < SZ(edge[x]); i++) {
int vi = edge[x][i];
if (!vis[vi]) ans += dp[vi], dfs(vi); // 加上可以经过的点数
}
}
int main()
{
cin >> T;
while (T--) {
sc("%d %d", &n, &m); init();
for (int i = 0; i < m; i++) {
int ui, vi;
sc("%d %d", &ui, &vi);
e[ui].push_back(vi);
}
for (int i = 0; i < n; i++) {
if (!dfn[i]) tarjan(i, i);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < SZ(e[i]); j++) {
int ui = suo[i], vi = suo[e[i][j]];
if (ui != vi) edge[vi].push_back(ui); // 反向建边
}
}
if (scnt == 1) {
printf("Case %d: %d\n", ++X, n - 1);
for (int i = 0; i < n - 1; i++) printf("%d ", i);
printf("%d\n", n - 1); continue;
}
for (int i = 1; i <= scnt; i++) {
int res = dp[i]; dp[i] = 0; // 记录自身点数
MEM(vis, 0);
ans = 0; dfs(i);
dp[i] += res + ans;
Max(max_, dp[i]);
}
for (int i = 0; i < n; i++) {
if (dp[suo[i]] == max_) pre[++C] = i;
}
printf("Case %d: %d\n", ++X, max_ - 1);
for (int i = 1; i < C; i++) printf("%d ", pre[i]);
printf("%d\n", pre[C]);
}
return 0;
}