• CCF-CSP题解 201709-4 通信网络


    dfs

    #include <bits/stdc++.h>
    const int maxn = 1000;
    const int maxm = 10000;
    
    using namespace std;
    
    int to[maxm+10];
    int nex[maxm+10];
    int head[maxn+10], cnt = 0;
    
    int know[maxn+10][maxn+10];
    
    int vis[maxn+10];
    
    void dfs(int r, int x)
    {
        vis[x] = 1;
        know[r][x] = know[x][r] = 1;
        for (int i = head[x]; i != -1; i = nex[i])
        {
            int l = to[i];
            if (!vis[l])
            {
                dfs(r, l);
            }
        }
    }
    
    int main()
    {
        int n, m;
        scanf("%d%d", &n, &m);
    
        memset(head, -1, sizeof(head));
        for (int i = 1, a, b; i <= m; i++)
        {
            scanf("%d%d", &a, &b);
            to[cnt] = b; nex[cnt] = head[a];
            head[a] = cnt++;
        }
    
        memset(know, 0, sizeof(know));
    
        for (int i = 1; i <= n; i++)
        {
            memset(vis, 0, sizeof(vis));
            dfs(i, i);
        }
    
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            bool flag = true;
            for (int j = 1; j <= n; j++)
            {
                if (!know[i][j])
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                ans++;
            }
        }
    
        printf("%d
    ", ans);
    
        return 0;
    }
    
  • 相关阅读:
    学习JavaDay07
    学习JavaDay05
    学习JavaDay0402
    学习JavaDay04
    学习JavaDay03
    学习JavaDay02
    学习JavaDay01
    数据库的增删改查
    关于Spring
    关于多线程Thread的简单应用
  • 原文地址:https://www.cnblogs.com/acboyty/p/11368637.html
Copyright © 2020-2023  润新知