• How Many Paths (强连通+dfs)


    题目:

    swjtu—春季集训第三场 - Virtual Judge (vjudge.net)

    You are given a directed graph GG which can contain loops (edges from a vertex to itself). Multi-edges are absent in GG which means that for all ordered pairs (u, v)(u,v) exists at most one edge from uu to vv. Vertices are numbered from 11 to nn.
    
    A path from uu to vv is a sequence of edges such that:
    
    vertex uu is the start of the first edge in the path;
    vertex vv is the end of the last edge in the path;
    for all pairs of adjacent edges next edge starts at the vertex that the previous edge ends on.
    We will assume that the empty sequence of edges is a path from uu to uu.
    
    For each vertex vv output one of four values:
    
    00, if there are no paths from 11 to vv;
    11, if there is only one path from 11 to vv;
    22, if there is more than one path from 11 to vv and the number of paths is finite;
    -11, if the number of paths from 11 to vv is infinite.
    Let's look at the example shown in the figure.
    
    
    Then:
    
    the answer for vertex 11 is 11: there is only one path from 11 to 11 (path with length 00);
    the answer for vertex 22 is 00: there are no paths from 11 to 22;
    the answer for vertex 33 is 11: there is only one path from 11 to 33 (it is the edge (1, 3)(1,3));
    the answer for vertex 44 is 22: there are more than one paths from 11 to 44 and the number of paths are finite (two paths: [(1, 3), (3, 4)][(1,3),(3,4)] and [(1, 4)][(1,4)]);
    the answer for vertex 55 is -11: the number of paths from 11 to 55 is infinite (the loop can be used in a path many times);
    the answer for vertex 66 is -11: the number of paths from 11 to 66 is infinite (the loop can be used in a path many times).
    Input
    The first contains an integer tt (1 \le t \le 10^41≤t≤10 
    4
     ) — the number of test cases in the input. Then tt test cases follow. Before each test case, there is an empty line.
    
    The first line of the test case contains two integers nn and mm (1 \le n \le 4 \cdot 10^5, 0 \le m \le 4 \cdot 10^51≤n≤410 
    5
     ,0≤m≤410 
    5
     ) — numbers of vertices and edges in graph respectively. The next mm lines contain edges descriptions. Each line contains two integers a_ia 
    i
    ​
     , b_ib 
    i
    ​
      (1 \le a_i, b_i \le n1≤a 
    i
    ​
     ,b 
    i
    ​
     ≤n) — the start and the end of the ii-th edge. The vertices of the graph are numbered from 11 to nn. The given graph can contain loops (it is possible that a_i = b_ia 
    i
    ​
     =b 
    i
    ​
     ), but cannot contain multi-edges (it is not possible that a_i = a_ja 
    i
    ​
     =a 
    j
    ​
      and b_i = b_jb 
    i
    ​
     =b 
    j
    ​
      for i \ne ji
    
    =j).
    
    The sum of nn over all test cases does not exceed 4 \cdot 10^5410 
    5
     . Similarly, the sum of mm over all test cases does not exceed 4 \cdot 10^5410 
    5
     .
    
    Output
    Output tt lines. The ii-th line should contain an answer for the ii-th test case: a sequence of nn integers from -11 to 22.
    
    Sample 1
    Inputcopy    Outputcopy
    5
    
    6 7
    1 4
    1 3
    3 4
    4 5
    2 1
    5 5
    5 6
    
    1 0
    
    3 3
    1 2
    2 3
    3 1
    
    5 0
    
    4 4
    1 2
    2 3
    1 4
    4 3
    View problem
    #include <bits/stdc++.h>
    using namespace std;
    #define ri register int
    #define M 400005
    
    template <class G > void read(G &x)
    {
        x=0;int f=0;char ch=getchar();
        while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
        x=f?-x:x;
        return ;
    }
    
    int T;
    int n,m;
    int head[M],nxt[M],to[M],cent=1;
    int vis[M],dfn[M],low[M],mb,l,q[M],tot,id[M],val[M];
    int in[M];
    int flag [M];
    void tj(int a)
    {
        vis[a]=1;
        q[++l]=a;
        dfn[a]=low[a]=++mb;
        for(ri p=head[a];p;p=nxt[p])
        {
            int b=to[p];
            if(!dfn[b])
            {
                tj(b);
                low[a]=min(low[a],low[b]);
            }
            else if(vis[b]) low[a]=min(low[a],dfn[b]);
        }
        
        if(low[a]==dfn[a])
        {
            tot++;int tmp=0;
            while(l>=1)
            {
                int b=q[l];l--;
                id[b]=tot;tmp++;
                vis[b]=0;
                if(b==a) break;
            }
            val[tot]=tmp;
        }
        
        
    }
    long long   num[M];
    void bfs()
    {
        queue<int> q;
        q.push(1);num[1]=1;
        
        while(!q.empty())
        {
            int a=q.front();q.pop();
            if(flag[a]==-1||flag[a]==-2) num[a]=-1;
            for(ri p=head[a];p;p=nxt[p])
            {
                int b=to[p];
                in[b]--;
                if(flag[b]==-1)
                {
                    if(vis[b]==0)
                    {
                        vis[b]=1;q.push(b);
                    }
                    continue;
                }
                if(in[b]==0) q.push(b);
                if(num[b]==-1)
                {
                    continue;
                }
                if(num[a]==-1)
                {
                    num[b]=-1;
                }else num[b]+=num[a];
            }
        }
    }
    void dfs(int a)
    {
        vis[a]=1;
        for(ri p=head[a];p;p=nxt[p])
        {
            int b=to[p];
            if(vis[b]) continue;
            dfs(b);
        }
    }
    void dfs1(int a,int k)
    {
        vis[a]=1;
        num[a]=-1;
        for(ri p=head[a];p;p=nxt[p])
        {
            int b=to[p];
            if(!vis[b])
            {
                dfs1(b,-1);
            }
        }
    }
    void dfs2(int a)
    {
        vis[a]=1;
        num[a]=1;
        for(ri p=head[a];p;p=nxt[p])
        {
            int b=to[p];
            if(!vis[b])
            {
                dfs2(b);
            }else
            {
                if(num[b]==1) num[b]=2;
            }
        }
    }
    void dfs3(int a)
    {
        num[a]=2;
        for(ri p=head[a];p;p=nxt[p])
        {
            int b=to[p];
            if(num[b]==1)
            {
                dfs3(b);
            }
        }
    }
    int main()
    {
        read(T);
        while(T--)
        {
            read(n);read(m);
            for(ri i=1;i<=m;i++)
            {
                int a,b;
                read(a);read(b);
                if(a==b) 
                {
                    flag[a]=-2;
                    continue;
                }
                in[b]++;
                to[++cent]=b;
                nxt[cent]=head[a];
                head[a]=cent;
            }
        
            tj(1);    
            for(ri i=1;i<=n;i++)
            {
                if(val[id[i]]>1) flag[i]=-1;
                if(val[id[i]]>=1&&flag[i]==-2) flag[i]=-1;
             } 
            for(ri i=1;i<=n;i++) vis[i]=0;
            
            for(ri i=1;i<=n;i++)
            {
                if(flag[i]==-1&&!vis[i])
                {
                    dfs1(i,-1);
                }
            }
            if(!vis[1])
            dfs2(1);
            for(ri i=1;i<=n;i++)
            {
                if(num[i]==2) dfs3(i);
            }
            for(ri i=1;i<=n;i++)
            {
                
                 printf("%lld ",num[i]);
            }
            cout<<endl;
            cent=1;
            for(ri i=1;i<=n;i++)
            {
                head[i]=0;in[i]=0;
                flag[i]=0;num[i]=0; vis[i]=dfn[i]=low[i]=mb=l=q[i]=0;tot=0,id[i]=0,val[i]=0;
            }
            
        }
        
        
        
        
    }
    View Code

    反思: 

    • 当提交的代码,错误百分之几时,自己题目理解出错,自己想的解决方法太片面,没有考虑完全
    • 或者 自己想解决方案的时候,有没有更简单易写的,思维难度,实现难度更简单的。
  • 相关阅读:
    BZOJ 3236: [Ahoi2013]作业
    BZOJ 3234: [Ahoi2013]立方体
    BZOJ 3235: [Ahoi2013]好方的蛇
    Hadoop 系列HDFS的Java API( Java API介绍)
    Hadoop 系列 HDFS 的JavaAPI Windows+IDEA+HDFS快速入门
    Hadoop 系列 HDFS:分布式文件系统(HDFS参数解读)
    Hadoop 系列 HDFS:分布式文件系统(HDFS集群模式)
    Hadoop 系列 HDFS:分布式文件系统(HDFS文件读写)
    Hadoop 系列 HDFS:分布式文件系统( HDFS概述)
    Hadoop中DataNode没有启动解决办法
  • 原文地址:https://www.cnblogs.com/Lamboofhome/p/15990218.html
Copyright © 2020-2023  润新知