zoukankan      html  css  js  c++  java
  • hdu_1269, 强连通分量的学习

    http://acm.hdu.edu.cn/showproblem.php?pid=1269

    #include<cstdio>
    #include<cstring>
    
    const int maxn = 10000 + 10;
    struct Edge{
        int to, next;
    }e[maxn*10];
    int head[maxn], index;
    int dfn[maxn], low[maxn];
    int belong[maxn], vis[maxn];
    int step, color;
    
    void add_edge(int u, int v){
        e[index].to = v, e[index].next = head[u];
        head[u] = index ++;
    }
    void init_edge(){
        memset(head, -1, sizeof head);
        index= 0;
    }
    int n, m;
    int stack[maxn], top;
    void targan(int u){
        dfn[u] = low[u] = ++step;
        stack[top++] = u;
        vis[u] = 1;
        for(int i = head[u]; i != -1; i = e[i].next){
            int v = e[i].to;
            if(!dfn[v]){
                targan(v);
                if(low[u] > low[v])
                  low[u] = low[v];
            }else
              if(vis[v] && low[u] > dfn[v])
                low[u] = dfn[v];
        }
        if(dfn[u] == low[u]){
            color ++;
            int s;
            do{
                s = stack[top--];
                vis[s] = 0;
                belong[s] = color;
            }while(s != u);
        }
    }
    void solve(){
        memset(vis, 0, sizeof vis);
        memset(dfn, 0, sizeof dfn);
        step = color = top = 0;
        for(int i = 1; i <= n; i ++)
          if(!dfn[i])
            targan(i);
    }
    int main(){
        while(scanf("%d%d", &n, &m), n||m){
            init_edge();
            for(int i = 0; i < m; i ++){
                int u, v;
                scanf("%d%d", &u, &v);
                add_edge(u, v);
            }
            solve();
            if(color == 1)
              puts("Yes");
            else
              puts("No");
        }
        return 0;
    }

    这个也当作模板吧

  • 相关阅读:
    OpenFlow 消息
    Revolving Digits
    Tavas and Malekas
    Prefixes and Suffixes
    快速构造后缀自动机
    REBXOR
    BZOJ3689 异或之
    BZOJ3689 异或之
    最长路径异或和
    BZOJ 1355 [Baltic2009] Bazinga
  • 原文地址:https://www.cnblogs.com/louzhang/p/2635440.html
Copyright © 2011-2022 走看看