n个人相互投票,这投票关系是可以传递的。A投B,B投C,那么C就获得了两票。
同一个强连通分量的人的票数等于点数减一。
现在就缩点,然后逆向建图,从入度为0的点开始搜索。
题目链接
/***************************************** Author :Crazy_AC(JamesQi) Time :2016/5/25 File Name :scc + 逆向建图搜索 _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \| |// `. / \||| : |||// \ / _||||| -:- |||||- \ | | \\ - /// | | | \_| ''\---/'' | | \ .-\__ `-` ___/-. / ___`. .' /--.--\ `. . __ ."" '< `.___\_<|>_/___.' >'"". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `-. \_ __\ /__ _/ .-` / / ======`-.____`-.___\_____/___.-`____.-'====== `=---=' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 佛祖保佑 永无BUG *****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
#define lson rt << 1
#define rson rt << 1 | 1
#define bug cout << "BUG HERE\n"
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-8;
const double pi = 4 * atan(1);
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int nCase = 0;
int dcmp(double x){//精度正负、0的判断
if (fabs(x) < eps) return 0;
return x < 0?-1:1;
}
inline int read(){
char c = getchar();
while (!isdigit(c)) c = getchar();
int x = 0;
while (isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
const int maxn = 5010;
const int maxm = 30010;
int head[maxn], pnt[maxm], nxt[maxm], ecnt;
int n, m;
int dfn[maxn], low[maxn], in[maxn], depth;
int belong[maxn], block;
int cnt1[maxn],cnt2[maxn], cnt3[maxn];
stack<int> st;
void dfs(int u) {
dfn[u] = low[u] = ++depth;
in[u] = 1;
st.push(u);
for (int i = head[u]; ~i; i = nxt[i]) {
int v = pnt[i];
if (dfn[v] == -1) {
dfs(v);
low[u] = min(low[u], low[v]);
}else if (in[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
block++;
while(true) {
int x = st.top();
st.pop();
in[x] = 0;
belong[x] = block;
cnt1[block]++;
if (x == u) break;
// cnt1[block]++;
}
}
}
void find_scc() {
memset(dfn, -1,sizeof dfn);
memset(in, 0,sizeof in);
memset(cnt1, 0,sizeof cnt1);//scc
depth = block = 0;
for (int i = 0;i < n;++i)
if (dfn[i] == -1) dfs(i);
}
vector<int> G[maxn];
int mark[maxn];
int search(int u) {
int sum = 0;
for (int i = 0;i < G[u].size();++i) {
int v = G[u][i];
if (mark[v]) continue;
mark[v] = 1;
sum += cnt1[v];
sum += search(v);
}
return sum;
}
void solve() {
for (int i = 1;i <= block;++i)
G[i].clear();
//rebuild new graph
for (int u = 0;u < n;++u) {
for (int i = head[u]; ~i;i = nxt[i]) {
int v = pnt[i];
if (belong[u] != belong[v]) {
G[belong[v]].push_back(belong[u]);
in[belong[u]]++;
}
}
}
memset(cnt2, 0,sizeof cnt2);
for (int i = 1;i <= block;++i) {
if (in[i] == 0) {
memset(mark, 0,sizeof mark);
cnt2[i] = search(i);
}
}
int Max = 0;
for (int i = 0;i < n;++i) {
cnt3[i] = cnt1[belong[i]] + cnt2[belong[i]] - 1;
// cout << "cnt3 = " << cnt3[i] << endl;
Max = max(Max, cnt3[i]);
}
printf("Case %d: %d\n", ++nCase, Max);
int first = 1;
for (int i = 0;i < n;++i) {
if (cnt3[i] == Max) {
if (first) first = 0;
else printf(" ");
printf("%d", i);
}
}
printf("\n");
}
int main(int argc, const char * argv[])
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
// clock_t _ = clock();
int t;
cin >> t;
while(t--) {
scanf("%d %d", &n, &m);
memset(head, -1,sizeof head), ecnt = 0;
int u, v;
for (int i = 1;i <= m;++i) {
scanf("%d %d", &u, &v);
pnt[ecnt] = v, nxt[ecnt] = head[u], head[u] = ecnt++;
}
find_scc();
// cout << "block = " << block << endl;
// for (int i = 1;i <= block;++i)
// cout << cnt1[i] << ' ';
// cout << endl;
solve();
}
// printf("\nTime cost: %.2fs\n", 1.0 * (clock() - _) / CLOCKS_PER_SEC);
return 0;
}