Poj 2762 going from u to V or from V to u?
Link: http://poj.org/problem? Id = 2762
Question:To make their son more brave, Jiajia and wind took them to a big cave. There are n rooms in the cave, and some one-way channels connect some rooms. Each time, wind chooses two rooms X and Y and asks one of their sons to go from one room to another. The son can go from X to Y or from Y to X. Wind ensures that the tasks she has assigned can be completed, but she does not know how to determine whether a task can be completed. In order to make it easier for wind to issue tasks, Jiajia decided to find such a cave. every pair of rooms (set to X and Y) were connected (from X to Y, or you can go from Y to X. Given a cave, can you tell Jiajia whether wind can choose either of the two rooms without worrying that the two rooms may not communicate with each other?
Idea: the essence of this question is to find the single connectivity of the directed graph.Single connectivity: For any two-point U, V in the directed graph. There must be a path from u to V or from V to u. 1. Calculate a strongly connected component for the graph, and then scale the vertex in each strongly connected component into a vertex. A DAG (directed acyclic graph) is formed after the scale-in ). 2. In a dag, if the tree has forks, the 2.1 of the forks cannot reach each other. Therefore, the image must be a single link. 3. topology Sorting is used to determine whether a single link is used. Each time, a vertex with an inbound degree of 0 is added to the queue. If the inbound degree of more than two vertices is 0 at the same time, then these two points must not arrive at each other, that is, they are not single-connected.
Code:
/* ID: [email protected] prog: Lang: c ++ */# include <map> # include <set> # include <queue> # include <stack> # include <cmath> # include <cstdio> # include <vector> # include <string> # include <fstream> # include <cstring> # include <ctype. h> # include <iostream> # include <algorithm> using namespace STD; # define Inf (1 <30) # define PI ACOs (-1.0) # define MEM (, b) memset (a, B, sizeof (A) # define rep (I, A, n) for (INT I = A; I <n; I ++) # Defin E per (I, A, n) for (INT I = n-1; I> = A; I --) # define EPS 1e-6 # define debug puts ("================ ") # define Pb push_back # define mkp make_pair # define all (x ). begin (), (x ). end () # define Fi first # define se second # define SZ (x) (INT) (x ). size () # define posin (x, y) (0 <= (x) & (x) <n & 0 <= (y) & (y) <m) typedef long ll; typedef unsigned long ull; const int maxn = 1100; vector <int> G [maxn]; In T dfn [maxn], low [maxn], sccno [maxn], dfs_clock, scc_cnt; stack <int> S; void DFS (int u) {dfn [u] = low [u] = ++ dfs_clock; S. push (U); For (INT I = 0; I <G [u]. size (); I ++) {int v = G [u] [I]; If (! Dfn [v]) {DFS (V); low [u] = min (low [u], low [v]); // update the low value} else if (! Sccno [v]) low [u] = min (low [u], dfn [v]); // update the low value through the back edge (and the point accessed by the back edge must not be in the partitioned strongly connected component)} If (low [u] = dfn [u]) {// obtain the strongly connected component scc_cnt ++; while (1) {int x = S. top (); S. pop (); sccno [x] = scc_cnt; If (x = u) Break ;}} void find_scc (int n) {dfs_clock = scc_cnt = 0; memset (sccno, 0, sizeof (sccno); memset (dfn, 0, sizeof (dfn); While (! S. Empty () S. Pop (); For (INT I = 1; I <= N; I ++) if (! Dfn [I]) DFS (I);} const int maxm = 6100; int MP [maxm] [2]; vector <int> newg [maxn]; int degree [maxn]; void build_new_map (INT m) {for (INT I = 1; I <= scc_cnt; I ++) newg [I]. clear (), degree [I] = 0; For (INT I = 0; I <m; I ++) {int u = sccno [MP [I] [0], V = sccno [MP [I] [1]; If (u! = V) {degree [v] ++; newg [u]. pb (v) ;}} bool toposort () {queue <int> q; For (INT I = 1; I <= scc_cnt; I ++) if (! Degree [I]) Q. Push (I); If (Q. Size ()> 1) return 0; int tot = 0; while (! Q. empty () {int u = Q. front (); q. pop (); For (INT I = 0; I <newg [u]. size (); I ++) {int v = newg [u] [I]; degree [v] --; If (! Degree [v]) Q. push (V);} If (Q. size ()> 1) return 0;} return 1;} int main () {int n, m; int t; scanf ("% d", & T ); while (t --) {scanf ("% d", & N, & M); For (INT I = 0; I <= N; I ++) G [I]. clear (); int U, V; rep (I, 0, m) {scanf ("% d", MP [I], MP [I] + 1 ); G [MP [I] [0]. pb (MP [I] [1]);} find_scc (n); build_new_map (m); If (toposort () puts ("yes "); else puts ("no");} return 0 ;}
Poj 2762 going from u to V or from V to u? (Directed graph for single connectivity)