码迷,mamicode.com
首页 > 其他好文 > 详细

[Noip提高组]-Day1-T2 信息传递

时间:2017-03-27 00:42:05      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:printf   最小   span   clu   出栈   else   sed   cee   names   

算法:Tarjan

大意:给你一个n边的有n个节点的图,求图中最小环

思路:直接强连通上,这道题可以涨姿势

   脑子有坑!!!差点忘了出栈,第一次打强连通,感觉自己药丸

代码:

技术分享
 1 #include<bits/stdc++.h>
 2 #define maxn 200007
 3 using namespace std;
 4 int dfn[maxn],low[maxn];
 5 bool vis[maxn]; 
 6 vector<int> E[maxn];
 7 int n,x,ans,tot; 
 8 stack<int> S;
 9 void tarjan(int x){
10     dfn[x]=low[x]=++tot;
11     S.push(x);vis[x]=1;
12     for(int i=0;i<E[x].size();i++){
13         int v=E[x][i];
14             if(!dfn[v]){
15                 tarjan(v);low[x]=min(low[x],low[v]);
16             }else if(vis[v]){
17                 low[x]=min(low[x],dfn[v]);
18             }
19     }
20     if(low[x]==dfn[x]){
21         int cnt=0;
22         while(1){
23             int now=S.top();S.pop();
24             vis[now]=0;
25             cnt++;
26             if(now==x) break;
27         }
28         if(cnt>1) ans=min(ans,cnt);
29     }
30 }
31 int main(){
32     ans=1e+9;
33     scanf("%d",&n);
34     for(int i=1;i<=n;i++){
35         scanf("%d",&x);
36         E[i].push_back(x);
37     }
38     for(int i=1;i<=n;i++){
39         if(!dfn[i]) tarjan(i);
40     }
41     printf("%d",ans);
42 } 
View Code

 

[Noip提高组]-Day1-T2 信息传递

标签:printf   最小   span   clu   出栈   else   sed   cee   names   

原文地址:http://www.cnblogs.com/Jrzn/p/6624747.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!