HDU6165 Tarjan reduced topological sorting point +

Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=6165

Question is intended: to give a non-self-loop, without heavy side of FIG determines whether any two points can reach (as long as one can reach to the other)

This is makes the first topological sort of topic, explain visible: https://blog.csdn.net/qq_41713256/article/details/80805338

Analysis: Because the only two requirements that can reach any other point, which is in communication only requires weak FIG. Internal First strongly connected components is certainly may be any reachable, we first use Tarjan is reduced in spot forming a directed acyclic graph (the DAG), on this basis we topological sort, as long as there exists a path that connects all points, then certainly it is reached, based on this when we topological sorting, if each layer only one point in-degree 0, you can get a unique path connecting all the points, that this time we can arrived.

#include<bits/stdc++.h>
#define maxn 1011
using namespace std;
vector<int>G[maxn],new_G[maxn];
stack<int>s;
int n,m;
int dfn[maxn],vis[maxn],low[maxn],color[maxn],colornum,cnt,in[maxn];
//in数组记录入度
bool toposort(int n){
    queue<int>q;
    int res=0;
    for(int i=1;i<=n;i++)  //The total number of nodes n 
        IF ( in [I] == 0 ) q.push (I), RES ++;   // to point 0 of the queue into 
    IF (RES> . 1 ) return  0 ; 
    Vector < int > ANS;    / / ANS topological sequence, where the subject is useless to 
    the while (! q.empty ()) 
    {     // COUT 233 << << endl; 
        int P = q.front (); q.pop (); // selected from a 0-degree point, a queue
         // COUT P << << endl; 
        ans.push_back (P); 
        RES = 0 ;
         for ( int I = 0;i<new_G[p].size();i++)
        {    
            int y=new_G[p][i];
            //if(p==2)cout<<G[p][i]<<endl;
            in[y]--;
            if(in[y]==0)
                q.push(y),res++;
                //cout<<p<<" "<<y<<endl;  
        }
        if(res>1) return 0;
    }
    return 1;
}

void tarjan(int x)
{
    dfn[x]=low[x]=++cnt;
    s.push(x);
    vis[x]=true;
    for(int i=0;i<G[x].size();i++)
    {
        int q=G[x][i];
        if (!dfn[q])
        {
            tarjan(q);
            low[x]=min(low[x],low[q]);
        }
        else if (vis[q]) low[x]=min(low[x],dfn[q]);
    }
    if (low[x]==dfn[x])
    {
        colornum++;
        int t;
        do{
            t=s.top();s.pop();
            color[t]=colornum;
            vis[t]=false;
        }while(t!=x);
    }
}
void init(int n){
        colornum=0,cnt=0;
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(vis,0,sizeof(vis));
        memset(color,0,sizeof(color));
        memset(in,0,sizeof(in));
        for(int i=1;i<=n;i++) G[i].clear();
}
int main(){
    int T;scanf("%d",&T);
    while(T--){
        int n,m;scanf("%d%d",&n,&m);
        init(n);//初始化 
        for(int i=1;i<=m;i++){
            int x,y;scanf("%d%d",&x,&y);
            G[x].push_back(y);
        }
        for(int i=1;i<=n;i++){
            if(!dfn[i]) tarjan(i);
        }
        for(int i=1;i<=colornum;i++){//对新建的图初始化
            new_G[i].clear();
        }
        for(int i=1;i<=n;i++){
            for(int j=0;j<G[i].size();j++){
                if(color[i]!=color[G[i][j]]) in[color[G[i][j]]]++,new_G[color[i]].push_back(color[G[i][j]]);
            }
        }
        //cout<<endl<<colornum<<endl;
        if(toposort(colornum)){
            printf("I love you my love and our love save us!\n");
        }
        else printf("Light my fire!\n");
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/qingjiuling/p/11317454.html