POJ 2762 infers single unicom (support points and even deflation + topological sequencing)

Source: Internet
Author: User

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

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

[Submit]   [Go back]   [Status] [Discuss]


Test instructions: For random two node u,v. Assuming it can be from u to V or V to u, then output yes, otherwise output No.

train of thought: first strong connection contraction point, at this time must not contain the ring, see can find a longest road including all the contraction point can (actually find inference single chain), with topological sort OK, just assume there is only a strong connected component then must be yes, otherwise topo sort inference, code such as the following :

#include <iostream> #include <algorithm> #include <cstdio> #include <queue> #include < Cstring> #include <cmath> using namespace std;typedef long long ll;const int MAXN =1e4+10;const ll mod=20140413; Vector<int>g1[maxn],g2[maxn],g[maxn];int Sccno[maxn],vis[maxn],scc_cnt;//sccno Strong connected number, SCC_CNT represents the number of strongly connected components int Ind[maxn];//ind represents the in degree void init_g (int n)//Initialize the indent graph {memset (ind,0,sizeof ind); for (int i=1;i<=n;i++) g[sccno[i]].clear () ; for (int i=1;i<=n;i++) {for (int j=0;j<g1[i].size (); ++j) {int &v =g1[i][j];if (Sccno[i]!=sccno[v]) {g[sccno[i ]].push_back (Sccno[v]); ind[sccno[v]]++;}}} BOOL Toposort (int n)//topo sort {init_g (n); int u,cnt=0;queue<int>q;for (int i=1;i<=n;i++) {if (!ind[sccno[i]]) { if (!q.empty ()) return False;q.push (Sccno[i]); cnt++;}} while (!q.empty ()) {U=q.front (); Q.pop (); ind[u]=-1;for (int i=0;i<g[u].size (); i++) {int &v=g[u][i];ind[v]--;if (ind[v]==0) {if (!q.empty ()) return False;q.push (v); cnt++;}}} return cnt==scc_cnt;} Vector<int>s;void INIT (int n) {memset (vis,0,sizeof vis); Memset (sccno,0,sizeof sccno); S.clear (); scc_cnt=0;for (int i=1;i<=n;i++) {g1[i].clear (); G2[i].clear (); G[i].clear ();}} void Addedge (int u,int v) {g1[u].push_back (v); G2[v].push_back (u);} void dfs1 (int u) {if (Vis[u]) return, vis[u]=1;for (int i=0;i<g1[u].size (); i++) DFS1 (G1[u][i]); S.push_back (u);} void dfs2 (int u) {if (Sccno[u]) return, sccno[u]=scc_cnt;for (int i=0;i<g2[u].size (); ++i) DFS2 (G2[u][i]);} BOOL Is_semiconnect (int n)///calculates the strongly connected component, tentatively infers {for (int i=1;i<=n;i++) DFS1 (i), the for (int i=s.size (); i>=1;i--) {if (! Sccno[s[i-1]] {scc_cnt++;d fs2 (s[i-1]);}} return scc_cnt<=1;}    int main () {int t,n,m;   scanf ("%d", &t);   while (t--) {scanf ("%d%d", &n,&m);   int u,v;   Init (n);   while (m--) {scanf ("%d%d", &u,&v);   Addedge (U,V);   } if (Is_semiconnect (n)) puts ("Yes");   else{if (Toposort (n)) puts ("Yes");   Else puts ("No");  }} return 0; }

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

POJ 2762 infers single unicom (support points and even deflation + topological sequencing)

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.