• 求点双联通分量(HihoCoder


    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<stack>
    using namespace std;

    struct my{
      int v;
      int next;
      int id;
    };

    my bian[2000000+10];
    int adj[200000+10];
    int maxn=(1<<29);
    int sccno[200000+10];
    int belong[200000+10];
    int pre[200000+10];
    int bccnum;
    int dfsnum;
    int fa1;
    int n,m;
    stack<int>q;

    void myinsert(int u,int v,int id){
         bian[++fa1].v=v;
         bian[fa1].next=adj[u];
         adj[u]=fa1;
         bian[fa1].id=id;
    }

    int tarjan(int u,int fa){
         int lowu=pre[u]=++dfsnum;
         for (int i=adj[u];i!=-1;i=bian[i].next){
            int v=bian[i].v;
            int e=bian[i].id;
            if(belong[e]) continue;
            if(!pre[v]){
                q.push(e);
                int lowv=tarjan(v,u);
                lowu=min(lowv,lowu);
                if(lowv>=pre[u]){
                  bccnum++;
                  int no=maxn;
                  for (;;){
                    int e2=q.top();
                    q.pop();
                    belong[e2]=bccnum;
                    no=min(no,e2);
                    if(e2==e||q.empty()) break;
                  }
                  sccno[bccnum]=no;
                  }
                } else if(v!=fa&&pre[v]<pre[u]){
                    q.push(e);
                    lowu=min(lowu,pre[v]);
            }
         }
         return lowu;
    }
    int main(){
        memset(bian,-1,sizeof(bian));
        memset(adj,-1,sizeof(adj));
        scanf("%d%d",&n,&m);
        int u,v;
        for (int i=1;i<=m;i++){
            scanf("%d%d",&u,&v);
            myinsert(u,v,i);
            myinsert(v,u,i);
        }
        /*for (int i=1;i<=n;i++){
            printf("%d ",i);
            for (int j=adj[i];j!=-1;j=bian[j].next){
                printf("%d ",bian[j].v);
            }
            printf(" ");
        }*/
        tarjan(1,-1);
        printf("%d ",bccnum);
        for (int i=1;i<=m;i++){
                int id=belong[i];
            printf("%d ",sccno[id]);
        }
       printf(" ");
    return 0;
    }

  • 相关阅读:
    ORA-01940: cannot drop a user that is currently connected
    三分钟入门VyOS网络操作系统
    金笛短信猫发短信一段时间后,停止发送
    ORA-01940 无法删除当前已连接的用户之解决方案
    Table is marked as crashed and should be repaire (
    DirectConnect API
    使用 ElasticSearch Aggregations 进行统计分析
    Cocos2d-x学习笔记(四) 布景层的加入移除
    Spark源代码阅读笔记之DiskStore
    一个搜索迷宫出路的程序
  • 原文地址:https://www.cnblogs.com/lmjer/p/8206465.html
Copyright © 2020-2023  润新知