POJ 2762 going from U-V or from V to u? (Strong unicom, topological sort)

Source: Internet
Author: User

http://poj.org/problem?id=2762

Going from U-V or from V to u?
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14573 Accepted: 3849

Description

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 the rooms x and Y, and ask one of the 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 is all possible, but she actually doesn ' t Know how to decide if a task is possible. To make hers life easier, Jiajia decided to choose a cave in which every pair of rooms are a possible task. Given a cave, can you tell Jiajia whether wind can randomly choose both 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 is contains integers n, m (0 < N < 1001,m < 6000), the number of rooms and corridors In the cave. The next m lines each contains to integers u and V, indicating that there is a corridor connecting the class U and the Class V dire ctly.

Output

The output should contain T lines. Write ' Yes ' if the cave have the property stated above, or ' No ' otherwise.

Sample Input

13 31 22) 33 1

Sample Output

Yes

Source

POJ Monthly--2006.02.26,zgl & TWB


Test instructions

Given a direction graph, determine if any two-point u,v can be reached from U to V or from V to U.

Analysis:

Determine the single connectivity to the graph. First, to get a dag, if the DAG is a single chain, it is clear that it is possible. How can I tell if a dag is a single chain? Just determine if the topological order is unique.


/* * * author:fcbruce <[email protected]> * * time:tue 11:37:19 AM CST */#include <cstdio& GT, #include <iostream> #include <sstream> #include <cstdlib> #include <algorithm> #include < ctime> #include <cctype> #include <cmath> #include <string> #include <cstring> #include < stack> #include <queue> #include <list> #include <vector> #include <map> #include <set> #define SQR (x) ((x) * (x)) #define LL long long#define ITN int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#defi NE eps 1e-10#ifdef _win32 #define LLD "%i64d" #else #define LLD "%lld" #endif # define MAXM 8964#define MAXN 1007using names Pace std;int N,m;int fir[maxn];int u[maxm],v[maxm],nex[maxm];int e_max;int pre[maxn],low[maxn],sccno[maxn];int st[  Maxn],top;int scc_cnt,dfs_clock;int deg[maxn];inline void add_edge (int _u,int _v) {int e=e_max++;  U[e]=_u;v[e]=_v; Nex[e]=fir[u[e]];fir[u[e]]=e;} void Tarjan_dfs (int s) {pre[S]=low[s]=++dfs_clock;  St[++top]=s;    for (int e=fir[s];~e;e=nex[e]) {int t=v[e];      if (pre[t]==0) {Tarjan_dfs (t);    Low[s]=min (Low[s],low[t]);    } else {if (sccno[t]==0) low[s]=min (low[s],pre[t]);    }} if (Low[s]==pre[s]) {scc_cnt++;    for (;;)      {int x=st[top--];      sccno[x]=scc_cnt;    if (x==s) break;  }}}void FIND_SCC () {scc_cnt=dfs_clock=0;  Top=-1;  memset (sccno,0,sizeof sccno);  memset (pre,0,sizeof Pre); for (int i=1;i<=n;i++) if (pre[i]==0) Tarjan_dfs (i);}  BOOL Unique_toposort () {top=-1;  for (int i=1;i<=scc_cnt;i++) {if (deg[i]==0) st[++top]=i;    } for (int i=0;i<scc_cnt;i++) {if (Top==1 | | | top==-1) return FALSE;    int x=st[top--];      for (int e=fir[x];~e;e=nex[e]) {deg[v[e]]--;    if (deg[v[e]]==0) st[++top]=v[e]; }} return true;  int main () {#ifdef fcbruce freopen ("/home/fcbruce/code/t", "R", stdin), #endif//fcbruce int t_t;  scanf ("%d", &t_t); while (t_t--) {SCANF ("%d%d", &n,&m);    e_max=0;    memset (fir,-1,sizeof fir);      for (int i=0,u,v;i<m;i++) {scanf ("%d%d", &u,&v);    Add_edge (U,V);    } FIND_SCC ();    int Temp=e_max;    e_max=0;    memset (fir,-1,sizeof fir);    memset (deg,0,sizeof deg);      for (int e=0;e<temp;e++) {if (Sccno[u[e]]==sccno[v[e]]) continue;      deg[sccno[v[e]]]++;    Add_edge (Sccno[u[e]],sccno[v[e]);    } if (Unique_toposort ()) puts ("Yes");      Else puts ("No"); } return 0;}


POJ 2762 going from U-V or from V to u? (Strong unicom, topological sort)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.