zoukankan      html  css  js  c++  java
  • lightoj 1026 无向图 求桥

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1026

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<vector>
    using namespace std;
    const int maxn = 10050;
    const int INF = 0x3f3f3f;
    
    vector<int> G[maxn];
    int pre[maxn],dfs_clock,low[maxn];
    
    struct ANS{
        int l,r;
        bool operator < (const ANS& a) const{
             return l < a.l || (l == a.l && r < a.r);    //排序被坑了,WA了好几次啊,没想后面的情况。
        }
    }ans[maxn];
    int cnt;
    int n;
    
    void tarjan(int u,int fa){
        low[u] = pre[u] = dfs_clock++;
        for(int i=0;i<G[u].size();i++){
            int v = G[u][i];
            if(v == fa)  continue;
            if(!pre[v]){
                tarjan(v,u);
                low[u] = min(low[u],low[v]);
                if(low[v] > pre[u]){
                   ans[cnt].l = min(u,v);
                   ans[cnt].r = max(u,v);
                   cnt++;
                }
            }
            else
                low[u] = min(low[u],pre[v]);  
        }
    }
    
    void init(){
        cnt = 0;
        dfs_clock = 1;
        memset(pre,0,sizeof(pre));
        for(int i=0;i<n;i++)  G[i].clear();
    }
    int main()
    {
        freopen("E:\acm\input.txt","r",stdin);
        int T;
        cin>>T;
        for(int t=1;t<=T;t++){
            scanf("%d",&n);
            init();
            for(int i=0;i<n;i++){
                int u,m;
                scanf("%d (%d)",&u,&m);
                for(int j=1;j<=m;j++){
                    int v;
                    scanf("%d",&v);
                    G[u].push_back(v);
                }
            }
            for(int i=0;i<n;i++){
                if(!pre[i]){
                    tarjan(i,-1);
                }
            }
            printf("Case %d:
    ",t);
            printf("%d critical links
    ",cnt);
            sort(ans,ans+cnt);
            for(int i=0;i<cnt;i++){
                printf("%d - %d
    ",ans[i].l,ans[i].r);
            }
        }
    }
    View Code
  • 相关阅读:
    .net开发环境的选择
    html头部的一些信息
    SQLHelper类
    C#实现文件下载
    js类
    Winform小知识点
    emacs 代码缩进
    自己喜欢的shell终端配置
    time_wait过多的优化
    Emacs 电子邮件组件RMAIL
  • 原文地址:https://www.cnblogs.com/acmdeweilai/p/3265278.html
Copyright © 2011-2022 走看看