• POJ 1904 King's Quest


    强连通分量

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    
    const int maxn=5000;
    vector<int>G[maxn];
    vector<int>FG[maxn];
    vector<int>Edge[maxn];
    int flag[maxn],dfn[maxn],Belong[maxn];
    int N,t,to,Time,Block;
    struct Point
    {
        int id, dfn;
    } point[maxn];
    
    
    bool cmp(const Point&a, const Point&b)
    {
        return a.dfn>b.dfn;
    }
    
    void init()
    {
        for(int i=0; i<maxn; i++) G[i].clear();
        for(int i=0; i<maxn; i++) FG[i].clear();
        for(int i=0; i<maxn; i++) Edge[i].clear();
    
        memset(flag,0,sizeof(flag));
        memset(dfn,0,sizeof(dfn));
        memset(Belong,0,sizeof(Belong));
        Time=0,Block=0;
    }
    
    void dfs(int now)
    {
        flag[now]=1;
        for(int i=0; i<G[now].size(); i++)
            if(!flag[G[now][i]])
                dfs(G[now][i]);
        Time++;
        dfn[now]=Time;
    }
    
    void Dfs(int now)
    {
        Belong[now] = Block;
        for (int i = 0; i<FG[now].size(); i++)
            if (!Belong[FG[now][i]])
                Dfs(FG[now][i]);
    }
    
    int main()
    {
        while(~scanf("%d",&N))
        {
            init();
            for(int i=1; i<=N; i++)
            {
                scanf("%d",&t);
                while(t--)
                {
                    scanf("%d",&to);
                    G[i].push_back(to+N);
                    FG[to+N].push_back(i);
                    Edge[i].push_back(to);
                }
            }
            for(int i=1; i<=N; i++)
            {
                scanf("%d",&t);
                G[t+N].push_back(i);
                FG[i].push_back(t+N);
            }
            for (int i=1; i<=2*N; i++) if(!dfn[i]) dfs(i);
            for (int i=0; i<2*N; i++) point[i].id =i+1,point[i].dfn=dfn[i+1];
            sort(point, point + N, cmp);
            for (int i = 0; i<2*N; i++)
                if (!Belong[point[i].id])
                    Block++, Dfs(point[i].id);
            int TOT;
            int Cun[maxn];
            for(int i=1; i<=N; i++)
            {
                TOT=0;
                sort(Edge[i].begin(),Edge[i].end());
                for(int j=0; j<Edge[i].size(); j++)
                {
                    if(Belong[i]==Belong[Edge[i][j]+N])
                    {
                        Cun[TOT]=Edge[i][j];
                        TOT++;
                    }
                }
                printf("%d",TOT);
                for(int x=0; x<TOT; x++) printf(" %d",Cun[x]);
                printf("
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    python继承__init__函数
    Oracle 用户(user)和模式(schema)的区别【转】
    url增加签名验证防窜改
    Codeforces 每日一练 706C+575H+15C
    Codeforces每日一练 1194D+552C+1117D
    每日一练周赛#2 题解
    AtCoder Beginner Contest 160 题解(F待填坑)
    Codeforces每日一练 1030D+1154E+540D
    Codeforcs 每日一练 678C+527C+1012C
    Codeforces 每日一练922C+725D+1152D
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4775222.html
Copyright © 2020-2023  润新知