Link:
http://acm.hdu.edu.cn/showproblem.php?pid=4738
Main topic:
There are a lot of islands, and then there is a need to build some bridges to link all the islands, Zhou Yu to do is to not allow all the islands to connect, each bridge is guarded, Zhou Yu can only blow a bridge, and he sent people to blow up the bridge can only send the number must be greater than the number of guards Bridge.
Output the smallest number of bridges, and if there is no answer, output-1
Code:
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<cstdlib>#include<algorithm>#include<vector>using namespacestd;#defineINF 0X7FFFFFF#defineN 100005typedefLong LongLL;structnode{intE, W; Node (intE=0,intw=0): E (E), W (w) {}};intLow[n], dfn[n], time, N, M;intF[n];intBri[n];vector< vectors <node> >G;voidInit () {memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); Memset (F,0,sizeof(f)); memset (BRI,-1,sizeof(BRI)); time=0; G.clear (); G.resize (n+2);}voidTarjan (intUintFA) {Low[u]= Dfn[u] = + +Time ; F[u]=FA; intLen = G[u].size (), k =0; Node V; for(intI=0; i<len; i++) {v=G[u][i]; if(!k && v.e==a) {k++; Continue; } if(!LOW[V.E]) {Tarjan (V.E, u); Low[u]=min (Low[u], low[v.e]); } ElseLow[u]=min (Low[u], dfn[v.e]); if(Dfn[u] <LOW[V.E]) BRI[V.E]=V.W; if(k==2&& bri[v.e]!=-1) BRI[V.E]= -1; }}voidSlove () {intAns =INF, I; Tarjan (1,1); for(i=2; i<=n; i++) { if(!Low[i]) Break; } if(i!=n+1) {puts ("0"); return ; } for(i=1; i<=n; i++) { if(bri[i]!=-1) ans=min (ans, bri[i]); } if(ans==0) ans++; if(ans==INF) ans= -1; printf ("%d\n", ans);}intMain () { while(SCANF ("%d%d", &n, &m), m+N) {Init (); for(intI=0; i<m; i++) { intA, B, C; scanf ("%d%d%d", &a, &b, &c); G[a].push_back (Node (b,c)); G[b].push_back (Node (a,c)); } slove (); } return 0;}
(connected figure Tarjan) Caocao ' s Bridges--hdu--4738