Going from u to v or from v to u? 【判定弱连通】=【tarjan求scc+ 缩点+topo】

来源:互联网 发布:淘宝上传生产许可证 编辑:程序博客网 时间:2024/05/16 05:30

In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the other. The son can either go from x to y, or from y to x. Wind promised that her tasks are all possible, but she actually doesn’t know how to decide if a task is possible. To make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. Given a cave, can you tell Jiajia whether Wind can randomly choose two rooms without worrying about anything?
Input
The first line contains a single integer T, the number of test cases. And followed T cases.

The first line for each case contains two integers n, m(0 < n < 1001,m < 6000), the number of rooms and corridors in the cave. The next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly.
Output
The output should contain T lines. Write ‘Yes’ if the cave has the property stated above, or ‘No’ otherwise.
Sample Input
1
3 3
1 2
2 3
3 1
Sample Output
Yes
题意 给一个有向图,问是否对于任意的两个点,都可以相互到达。
是不是和强连通很像,其实我少说了一个很重要的条件,就是这个任意的两个点:没有规定那个是起始点,那个是终点,都可以当起点或者终点。所以和强连通还是很不一样。 其实这样的图叫做弱连通图;
对于一个DAG(有向图求scc+缩点后的新图(有向无环图))图来说,其实这样题意的图(弱连通图),就是只要有一个起点,它可以走过所有的点,并到达终点。 这样的图符合题意(弱连通图);

链接
代码
没加输入挂,334ms 加了输入挂64ms 。。 再一次感叹输入挂的强大。其实仔细想一下这道题的输入量 ,m*t*2==2*6000*t ;也就至少到了1e5级了。一般到1e5级的输入量,用输入挂,效果就很显著。

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<iostream>#include<vector>using namespace std;const int MAXN= 1001+10;inline int read(){    int x=0,f=1;char ch=getchar();    while(ch>'9'||ch<'0'){ if(ch=='-') f=-1; ch=getchar();}    while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}    return x*f;}  //  输入挂/*------------------------------------*/struct Edge {    int from,to,next;}edge[6000+10];int head[MAXN],top;int n,m;void init(){    memset(head,-1,sizeof(head));    top=0;}void addedge(int a,int b){    Edge e={a,b,head[a]};    edge[top]=e;head[a]=top++;}void getmap(){    int a,b;  while(m--){    a=read();b=read();    addedge(a,b);  }}int low[MAXN],dfn[MAXN];int scc_cnt,sccno[MAXN];stack<int>S;int Instack[MAXN];vector<int>G[MAXN];int dfs_clock;void tarjan(int now,int par){    low[now]=dfn[now]=++dfs_clock;    S.push(now);Instack[now]=1;    for(int i=head[now];i!=-1;i=edge[i].next){        Edge e=edge[i];        if(!dfn[e.to]){            tarjan(e.to,now);            low[now]=min(low[now],low[e.to]);        }else if(Instack[e.to])        low[now]=min(low[now],dfn[e.to]);    }    if(low[now]==dfn[now]) {        scc_cnt++;        for(;;){            int nexts=S.top();S.pop();Instack[nexts]=0;            sccno[nexts]=scc_cnt;            if(nexts==now) break;        }    }}void find_cut(int le,int ri){    memset(low,0,sizeof(low));    memset(dfn,0,sizeof(dfn));    memset(Instack,0,sizeof(Instack));    memset(sccno,0,sizeof(sccno));    dfs_clock=scc_cnt=0;    for(int i=le;i<=ri;i++){        if(!dfn[i]) tarjan(i,-1);    }}int in[MAXN];void suodian(){    for(int i=1;i<=scc_cnt;i++) {            in[i]=0;G[i].clear();    }    for(int i=0;i<top;i++){        Edge e=edge[i];        int now=sccno[e.from];        int nexts=sccno[e.to];        if(now!=nexts){            G[now].push_back(nexts);            in[nexts]++;        }    }}queue<int>Q;bool topo(){    while(!Q.empty()) Q.pop();    int num=0;    for(int i=1;i<=scc_cnt;i++){        if(!in[i]) {            Q.push(i);            num++;            if(num>1) return false;        }    }    int k=0;    while(!Q.empty()){        int now=Q.front();Q.pop();num=0;k++;        for(int i=0;i<G[now].size();i++){            int v=G[now][i];            if(--in[v]==0){                Q.push(v);                num++;                if(num>1) return false;            }        }    }    return  k==scc_cnt;}void solve(){    find_cut(1,n);    suodian();    if(scc_cnt==1) puts("Yes");    else    puts(topo()?"Yes":"No");}int main(){    int t;t=read();while(t--){        n=read();m=read();        init();        getmap();        solve();    }    return 0;}
阅读全文
0 0