hdu 4738 Caocao's Bridges 图的割边

#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x7fffffff;

struct Edge
{
    int from,to,tag;
};

int n,m,dfn[1024],low[1024],vis[1024],dep,ans,mp[1024][1024];
vector<Edge>edges;
vector<int>G[1024];

void AddEdge(int from,int to)
{
    int i,flag=0;
    for(i=0;i<G[from].size();i++)
        if(edges[G[from][i]].to==to)
        {
            edges[G[from][i]].tag++;flag=1;break;
        }
    if(!flag)
    {
        Edge tp;
        tp.from=from;tp.to=to;tp.tag=0;
        edges.push_back(tp);
        G[from].push_back(edges.size()-1);
    }
}

void dfs(int u,int fa)
{
    dfn[u]=low[u]=++dep;
    for(int i=0;i<G[u].size();i++)
    {
        Edge t=edges[G[u][i]];
        if(!vis[t.to])
        {
            vis[t.to]=1;
            dfs(t.to,u);
            low[u]=min(low[u],low[t.to]);
            if(low[t.to]>dfn[u]&&!t.tag)
                ans=min(ans,mp[t.to][u]);
        }
        else if(t.to!=fa)
            low[u]=min(low[u],dfn[t.to]);
    }
}

int main()
{
    int _,i,j,u,v,val;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        edges.clear();
        for(i=0;i<=n;i++)
            G[i].clear();
        memset(mp,0xff,sizeof(mp));
        for(i=1;i<=m;i++)
        {
          scanf("%d%d%d",&u,&v,&val);
            AddEdge(u,v);
            AddEdge(v,u);
            if(mp[u][v]==-1)
                mp[u][v]=mp[v][u]=val;
            else
               mp[u][v]=mp[v][u]=min(mp[u][v],val);
        }
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                if(mp[i][j]==-1)
                    mp[i][j]=inf;
        memset(vis,0,sizeof(vis));
        vis[1]=1;
        dep=0;
        ans=inf;
        dfs(1,-1);
       if(ans==inf) ans=-1;
       if(ans==0) ans=1;
       for(i=1;i<=n;i++)
        if(vis[i]==0) ans=0;
       printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(hdu 4738 Caocao's Bridges 图的割边)