大学班级选班长,N 个同学均可以发表意见
若意见为 A B 则表示 A 认为 B 合适,意见具有传递性,即 A 认为 B 合适,B 认为 C 合适,则 A 也认为 C 合适
勤劳的 TT 收集了M条意见,想要知道最高票数,并给出一份候选人名单,即所有得票最多的同学,你能帮帮他吗?
Input
本题有多组数据。第一行 T 表示数据组数。每组数据开始有两个整数 N 和 M (2 <= n <= 5000, 0 <m <= 30000),接下来有 M 行包含两个整数 A 和 B(A != B) 表示 A 认为 B 合适。
Output对于每组数据,第一行输出 “Case x: ”,x 表示数据的编号,从1开始,紧跟着是最高的票数。
接下来一行输出得票最多的同学的编号,用空格隔开,不忽略行末空格!
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
思路
有向图一般的思路就是先用Kahn算法求出强连通分量SCC,然后缩点,也就是将互相可到达和单向可到达分开。
缩点后,不难发现对于属于第i个SCC的点来说,答案分为
两部分,令SCC[i]表示第i个SCC中点的个数
当前SCC中的点,ans+=SCC[i]-1(去除自己)
其它SCC中的点
SUM(SCC[j]),其中j可到达I
稍加思考,可以发现最后答案一定出现在出度为0的SCC
中,
可以用反证法证明
因此我们将边反向,对每个入度为0的点进行dfs,计算其
能到达的点的SUM(SCC[j]),即可得到答案。
Kosaraju算法的步骤:
算法步骤
第一-遍dfs确定原图的逆后序序列
第二遍dfs在反图中按照逆后序序列进行遍历
反图即将原图中的有向边反向
每次由起点遍历到的点即构成一个SCC
(现在做题越来越吃力了,感觉自己好菜,刚开始的算法还能一边理解一边证明一边记忆,现在算法就是直接按照步骤和ppt上代码来,证明什么的根本不敢想)
代码
#include<bits/stdc++.h>
#define mn 5010
#define mm 30010
using namespace std;
int N,M;
int tot1=0,tot2=0,tot3=0,dcnt,scnt,ans,num=1;
int head1[mn],head2[mn],head3[mn],in_deg[mn],dfn[mn],scc[mn],aans[mn],vis1[mn],vis2[mn],vis3[mn],mscc[mn];
struct edge{
int v,next;
};
void init(){
tot1=0,tot2=0,tot3=0;
dcnt=0,scnt=0,ans=0;
for(int i=0;i<mn;i++){
head1[i]=-1;
head2[i]=-1;
head3[i]=