Going from U-V or from V to u?
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 15755 |
|
Accepted: 4172 |
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
Test instructions: is to give some points, give the n point and M Edge, and then give the directly connected edge (note that there is a forward edge), to solve any x, y two points between the existence of an X. y or y can be
At x, if any x and y satisfy such conditions, the output is "Yes", otherwise the output "No".
Note that here is x reaching Y or y reaching X, yes or No and!!!
If "and", it is clear that the whole graph is a strong connected component (Title HDU 1296, problem resolution), but this is not the case.
The subject should be judged whether the whole graph is a weakly connected component.
Correct thinking: The strong connected component of the forward graph is solved first, and then the contraction point is re-built according to the strong connected components solved.
The problem is converted to solve whether there is a path that can go through all the vertices in the new diagram , then the new graph after the indentation can be sorted to see if the topological sort can be done successfully.
topological ordering follows conditions
A: The new figure cannot have more than 1 points with an entry degree of 0, which ensures that each point has an edge attached to it.
Second: In the process of topology sequencing traversal point u, if you remove the side associated with U with more than 1 of the points in the 0, indicating that these points can only be reached by u, and there is no reachable path between them. At this time do not meet the weak connectivity, jump out.
#include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm > #define MAXN 10000 + 100#define MAXM 100000 + 1000using namespace std;int N, m;struct node {int u, V, next;}; Node Edge[maxm];int HEAD[MAXN], Cnt;int LOW[MAXN], dfn[maxn];int dfs_clock;int stack[maxn];bool Instack[maxn];int top; int BELONG[MAXN], scc_clock;int in[maxn];vector<int>map[maxn];void init () {cnt = 0;memset (head,-1, sizeof (head)) ;} void Addedge (int u, int v) {edge[cnt] = {u, V, head[u]};head[u] = cnt++;} void Getmap () {scanf ("%d%d", &n, &m); while (m--) {int A, B; scanf ("%d%d", &a, &b); Addedge (A, b); }}void Tarjan (int u, int per) {int V; Low[u] = dfn[u] = ++dfs_clock; stack[top++] = u; Instack[u] = true; for (int i = head[u]; I! =-1; i = edge[i].next) {v = edge[i].v; if (!dfn[v]) {Tarjan (V, u); Low[u] = min (Low[v], low[u]); } else if (Instack[v]) { Low[u] = min (Low[u], dfn[v]); }} if (dfn[u] = = Low[u]) {scc_clock++; do{v = stack[--top]; INSTACK[V] = false; BELONG[V] = Scc_clock; }while (U! = v); }}void find () {memset (low, 0, sizeof (low)); memset (DFN, 0, sizeof (DFN)); Memset (Instack, False, sizeof (Instack)); memset (Belong, 0, sizeof (Belong)); Dfs_clock = Scc_clock = top = 0; for (int i = 1; I <= n; ++i) {if (!dfn[i]) Tarjan (i, I); }}void Suodian () {for (int i = 1; I <= scc_clock; ++i) {map[i].clear (); In[i] = 0; } for (int i = 0; i < cnt; ++i) {int u = belong[edge[i].u]; int v = belong[edge[i].v]; if (U = v) {map[u].push_back (v); in[v]++; }}}void Solve () {queue<int>q; int num = 0; for (int i = 1; I <= scc_clock; ++i) {if (!in[i]) {num++; Q.push (i); } if (num > 1) {printf ("no\n"); return; }} while (!q.empty ()) {int u = q.front (); Q.pop (); num = 0; for (int i = 0; i < map[u].size (); ++i) {int v = map[u][i]; in[v]--; if (!in[v]) {num++; There are two or more two branches, not weakly connected if (num > 1) {printf ("no\n"); return; } q.push (v); }}} printf ("yes\n");} int main () {int T; scanf ("%d", &t), while (t--) {init (); Getmap (); Find (); Suodian (); Solve ();} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2762--going from U to V or from V to u? "SCC indent new Diagram && determine if it is a weakly connected graph"