HDU-4738 Caocao's Bridges 边联通分量

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738

  题意:在有重边的无向图中,求权值最小的桥。

  注意trick就好了,ans为0时输出1,总要有一个人去丢炸弹吧。。。

 //STATUS:C++_AC_62MS_8144KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e60;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End /* Edge-Biconnected Component(可以处理重边)
iscut[]为割边集
bccno[]为双连通点集,保存为编号 */
struct Edge{
int u,v,w;
}e[*N*N];
bool iscut[*N*N];
int first[N],next[*N*N],pre[N],low[N],bccno[N];
int n,m,mt,bcnt,dfs_clock;
stack<int> s; void adde(int a,int b,int c)
{
e[mt].u=a;e[mt].v=b;e[mt].w=c;
next[mt]=first[a];first[a]=mt++;
e[mt].u=b;e[mt].v=a;e[mt].w=c;
next[mt]=first[b];first[b]=mt++;
} void dfs(int u,int fa)
{
int i,v;
pre[u]=low[u]=++dfs_clock;
s.push(u);
int cnt=;
for(i=first[u];i!=-;i=next[i]){
v=e[i].v;
if(!pre[v]){
dfs(v,u);
low[u]=Min(low[u],low[v]);
if(low[v]>pre[u])iscut[i]=true; //存在割边
}
else if(fa==v){ //反向边更新
if(cnt)low[u]=Min(low[u],pre[v]);
cnt++;
}
else low[u]=Min(low[u],pre[v]);
}
if(low[u]==pre[u]){ //充分必要条件
int x=-;
bcnt++;
while(x!=u){
x=s.top();s.pop();
bccno[x]=bcnt;
}
}
} int find_bcc()
{
int i,cnt=;
bcnt=dfs_clock=;
mem(pre,);mem(bccno,);mem(iscut,);
for(i=;i<=n;i++){
if(!pre[i]){cnt++;dfs(i,-);}
}
return cnt;
} int main(){
// freopen("in.txt","r",stdin);
int i,j,a,b,c,ans,t;
while(~scanf("%d%d",&n,&m) && (n||m))
{
mem(first,-);mt=;
for(i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
adde(a,b,c);
} t=find_bcc();
if(t>){
printf("0\n");
continue;
} ans=INF;
for(i=;i<mt;i++){
if(iscut[i])ans=Min(ans,e[i].w);
}
printf("%d\n",ans==INF?-:(ans?ans:));
}
return ;
}
上一篇:Swift 学习笔记 (二)


下一篇:千万数量级分页存储过程 +AspNetPager现实分页