zoukankan      html  css  js  c++  java
  • 拓扑排序bfs_dfs

    dfs
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    const int maxn = 1e5+50;
    struct Edge{
        int lst;
        int to;
    }edge[maxn*5];
    int head[maxn];
    
    int qsz;
    
    inline void add(int u, int v) {
        edge[qsz].lst = head[u];
        edge[qsz].to  = v;
        head[u] = qsz++;
    }
    
    int vis[maxn];
    int  ans[maxn];
    int qtot;
    bool dfs(int u) {
        int v, i;
        vis[u] = -1;
        for (i=head[u]; i; i=edge[i].lst) {
            v = edge[i].to;
            if (vis[v] == -1) return false;
            else if (!vis[v] && !dfs(v)) return false;
        }
        ans[qtot--] = u; 
        vis[u] = 1;    
        return true;
    }
    
    int main()
    {
        int t, n, m, i, j, u, v;
        scanf("%d", &t);
        while (t--) {
            // init;
            qsz = 1;
            memset(head, 0, sizeof(head));
            memset( vis, 0, sizeof( vis));
            scanf("%d%d", &n, &m);
            qtot = n;
            for (i=1; i<=m; ++i) {
                scanf("%d%d", &u, &v);
                add(v, u);
            }
            for (i=1; i<=n; ++i) 
                if (!vis[i]) 
                    if (!dfs(i)) 
                        break;
                        
            if (!qtot) printf("Correct
    ");
            else printf("Wrong
    ");
        }
        
        return 0;
    }
    
    
    

    bfs
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    const int maxn = 1e5+50;
    struct Edge{
        int lst;
        int to;
    }edge[maxn*5];
    int head[maxn];
    int qsz;
    
    
    int inq[maxn];
    int q[maxn];
    int qhead;
    inline void add(int u, int v) {
        edge[qsz].lst = head[u];
        edge[qsz].to  = v;
        head[u] = qsz++;
    }
    
    int main()
    {
        int t, n, m, i, j, u, v;
        scanf("%d", &t);
        while (t--) {
            // init;
            qsz = 1;
            memset(head, 0, sizeof(head));
            memset(inq,  0, sizeof( inq));
            qhead = 0;
            
            scanf("%d%d", &n, &m);
            for (i=1; i<=m; ++i) {
                scanf("%d%d", &u, &v);
                add(u, v);
                inq[v]++;
            }
            for (i=1; i<=n; ++i) 
                if (!inq[i]) 
                    q[qhead++] = i;
            for (i=0; i<qhead; ++i) {
                for (j=head[q[i]]; j; j=edge[j].lst) {
                    v = edge[j].to;
                    inq[v]--;
                    if (!inq[v]) q[qhead++] = v;
                }
            }
            if (qhead == n) printf("Correct
    ");
            else printf("Wrong
    ");
        }
        
        return 0;
    }
    
    
    
     
  • 相关阅读:
    对spring web启动时IOC源码研究
    对volatile关键字的理解
    [书籍分享]0-009.微信营销与运营解密:利用微信创造商业价值的奥秘
    [JavaWeb基础] 002.JSP和SERVLET初级入门
    [Objective-C] 005_Category(类别)
    [PHP学习教程
    读Pyqt4教程,带你入门Pyqt4 _008
    读Pyqt4教程,带你入门Pyqt4 _007
    宝宝巴士安卓框架介绍
    [安卓基础] 006.打开另一个Activity
  • 原文地址:https://www.cnblogs.com/cgjh/p/9753757.html
Copyright © 2011-2022 走看看