problem
- 给定一张n个点,n条边的有向图
- 求图的最小环,输出大小
solution
kosaraju暴力求出所有强连通分量,取最小值即可
codes
//kosaraju
#include<iostream>
#include<algorithm>
#include<vector>
#define maxn 200010
using namespace std;
vector<int>G[maxn], rG[maxn];
vector<int>vs, cmp[maxn];
int vis[maxn], book[maxn], cnt;
void dfs(int u){
if(vis[u])return ;
vis[u] = 1;
for(int i = 0; i < G[u].size(); i++)dfs(G[u][i]);
//for(int i = 0; i < G[u].size(); i++)if(!vis[G[u][i]])dfs(G[u][i]);
vs.push_back(u);
}
void rdfs(int u){
//if(book[u])return ;
book[u] = cnt;
cmp[cnt].push_back(u);
for(int i = 0; i < rG[u].size(); i++)if(!book[rG[u][i]])rdfs(rG[u][i]);
//for(int i = 0; i < rG[u].size(); i++)rdfs(rG[u][i]);
}
int main(){
int n; cin>>n;
for(int i = 1; i <= n; i++){
int x; cin>>x;
G[i].push_back(x);
rG[x].push_back(i);
}
for(int i = 1; i <= n; i++)dfs(i);
for(int i = n-1; i >= 0; i--){
if(!book[vs[i]]){
++cnt;
rdfs(vs[i]);
}
}
int ans = 0xffffff;
for(int i = 1; i <= cnt; i++){
int si = cmp[i].size();
if(si != 0 && si != 1){
ans = min(ans, si);
}
}
cout<<ans<<"\n";
return 0;
}
标签:NOIP2015,有向图,Vijos1979,vis,int,++,rG,maxn,book
From: https://blog.51cto.com/gwj1314/6043779