HDU 4738 Caocao's Bridges(割边)

时间:2021-08-27 14:30:06

  乍一看一个模板题,仔细一看还是模板题,但是三个坑。1,不是连通图,放0个。2 守卫为0,放1个。 3注意重边。

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
#define maxn 1005
int head[maxn],dfn[maxn],low[maxn],guard[maxn][maxn],tot,n;
int vis[maxn],all,index,bri[maxn*maxn];
struct Edge
{
int to,nxt,g;
} edge[maxn*maxn*];
queue<int> que;
void bfs(int u)
{
vis[u] = ;
que.push(u);
while(!que.empty())
{
int now = que.front();
que.pop();
for(int i = head[now]; i != -; i = edge[i].nxt)
{
int v = edge[i].to;
if(!vis[v]) {que.push(v);vis[v] = ;}
}
}
}
bool judge()
{
memset(vis,,sizeof(vis));
int sum = ;
while(!que.empty()) que.pop();
for(int i = ; i <= n; i++)
{
if(!vis[i])
{
sum++;
bfs(i);
}
}
if(sum==) return true;
return false;
}
void addedge(int u,int v,int w)
{
edge[tot].to = v;
edge[tot].g = w;
edge[tot].nxt = head[u];
head[u] = tot++;
}
void init()
{
all = index = ;
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
}
void tarjan(int u,int fa)
{
low[u] = dfn[u] = ++index;
for(int i = head[u];i != -;i = edge[i].nxt){
int v = edge[i].to;
if(!dfn[v]){
tarjan(v,u);
low[u] = min(low[u],low[v]);
if(low[v] > dfn[u] && guard[u][v]!=-&&guard[v][u]!=-){
bri[all++] = edge[i].g;
}
}
else if(v != fa) low[u] = min(low[u],dfn[v]);
}
}
int main()
{
int m,a,b,w;
while(~scanf("%d%d",&n,&m))
{
if(n+m == ) break;
memset(head,-,sizeof(head));
tot = ;
memset(guard,-,sizeof(guard));
for(int i = ; i < m; i++)
{
scanf("%d%d%d",&a,&b,&w);
if(guard[a][b]==- && guard[b][a]==-)
{
addedge(a,b,w);
addedge(b,a,w);
guard[a][b] = guard[b][a] = w;
}
else {
guard[a][b] = guard[b][a] = -;
}
}
if(!judge())
{
puts("");
continue;
}
init();
tarjan(,-);
if(all == ){
puts("-1");
continue;
}
sort(bri,bri+all);
if(bri[] == ) puts("");
else printf("%d\n",bri[]);
}
}