标签:des style blog http io color ar os sp
3 3 1 2 7 2 3 4 3 1 4 3 2 1 2 7 2 3 4 0 0
-1 4
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define INF 0x3f3f3f3f 15 #define pii pair<int,int> 16 using namespace std; 17 const int maxn = 1005; 18 struct arc { 19 int to,id,next; 20 arc(int x = 0,int y = 0,int z = -1) { 21 to = x; 22 id = y; 23 next = z; 24 } 25 }; 26 arc e[maxn*maxn*2]; 27 int head[maxn],dfn[maxn],low[maxn],uf[maxn]; 28 int tot,idx,ans,p[maxn*maxn*2],n,m; 29 int Find(int x){ 30 if(x == uf[x]) return uf[x]; 31 return uf[x] = Find(uf[x]); 32 } 33 void add(int u,int v,int id) { 34 e[tot] = arc(v,id,head[u]); 35 head[u] = tot++; 36 e[tot] = arc(u,id,head[v]); 37 head[v] = tot++; 38 } 39 void tarjan(int u,int fa) { 40 dfn[u] = low[u] = ++idx; 41 bool flag = true; 42 for(int i = head[u]; ~i; i = e[i].next) { 43 if(e[i].to == fa && flag) { 44 flag = false; 45 continue; 46 } 47 if(!dfn[e[i].to]) { 48 tarjan(e[i].to,u); 49 low[u] = min(low[u],low[e[i].to]); 50 if(low[e[i].to] > dfn[u] && p[e[i].id] < ans) 51 ans = p[e[i].id]; 52 } else low[u] = min(low[u],dfn[e[i].to]); 53 } 54 } 55 void init() { 56 for(int i = 0; i < maxn; ++i) { 57 head[i] = -1; 58 dfn[i] = low[i] = 0; 59 uf[i] = i; 60 } 61 idx = tot = 0; 62 ans = INF; 63 } 64 int main() { 65 int u,v; 66 while(scanf("%d %d",&n,&m),n||m) { 67 init(); 68 for(int i = 1; i <= m; ++i) { 69 scanf("%d %d %d",&u,&v,p+i); 70 add(u,v,i); 71 int tx = Find(u); 72 int ty = Find(v); 73 if(tx != ty) uf[tx] = ty; 74 } 75 int root = 0; 76 for(int i = 1; i <= n; ++i) if(uf[i] == i) root++; 77 if(root > 1){ 78 printf("%d\n",0); 79 continue; 80 } 81 for(int i = 1; i <= n; ++i) 82 if(!dfn[i]) tarjan(i,-1); 83 if(ans == INF) puts("-1"); 84 else if(ans) printf("%d\n",ans); 85 else puts("1"); 86 } 87 return 0; 88 }
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/crackpotisback/p/4106089.html