51nod 1076 2 Non-intersecting path non-intersect component Trajan algorithm

Source: Internet
Author: User

1076 2 Disjoint path base time limit: 1 seconds space limit: 131072 KB Score: 40 Difficulty: 4-level algorithm topic collection focus on the vertex V and Edge e of the graph G. Q Query, query from one vertex of G v[s] to another vertex v[t], whether there are 2 disjoint paths. (Two paths do not go through the same edge) (note that there is no heavy edge in the graph, that is, the start and end points are determined, there is a maximum of 1 paths between them) Input
Line 1th: 2 number M N, separated by a space, M is the number of vertices, N is the number of edges. (2 <= M <= 25000, 1 <= N <= 50000) 2-n + 1 lines, 2 numbers per line, separated by spaces, respectively, the starting and ending numbers of the N edges. For example, 2 4 means that the starting point is 2, the end point is 4, and since it is a non-direction graph, it is a feasible path from 4 to 2. The Nth + 2 line, a number Q, indicates that a Q query will be followed. (1 <= Q <= 50000) nth + 3-n + 2 + Q lines, 2 numbers per line s, T, in the middle separated by spaces, indicating the starting and ending points of the query.
Output
If there are 2 disjoint paths from S to T, the output is yes, otherwise the output is no.
Input example
4 41 22 31 31 451 22 33 12 41 4
Output example
Yesyesyesnono


The template of the direct composition set of the strong Unicom component and then see if the two points asked are connected.
The beginning of the array is small and then the last 3 answers have been WA looking for a long time simply cancer ah ...
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < queue> #include <vector> #include <iomanip> #include <math.h> #include <map>using namespace STD; #define FIN freopen ("Input.txt", "R", stdin), #define FOUT freopen ("Output.txt", "w", stdout); #define INF 0x3f3f 3f3f#define infll 0x3f3f3f3f3f3f3f#define Lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef Long Long LL ; typedef pair<int, int> pii;using namespace std;const int MX = 1e5 + 10;struct Edge {int u, V, NXT;}    E[mx];int head[mx], erear;void edge_init () {erear = 0; Memset (Head,-1, sizeof (head));}    void Edge_add (int u, int v) {e[erear].u = u;    E[EREAR].V = v;    E[EREAR].NXT = Head[u]; Head[u] = erear++;} int n, M, in[mx], cnt[mx], Val[mx];int Bsz, SSZ, Dsz;int low[mx], dfn[mx];void init_tarjan (int n) {Bsz = SSZ = DSZ = 0    ; for (int i = 1; I <= n; ++i) low[i] = Dfn[i] = 0;} int STACK[MX], instack[mx], Belong[mx];void Trajan (int u, int e) {Instack[u] = 1;    STACK[++SSZ] = u;    Dfn[u] = low[u] = ++DSZ;        for (int i = head[u]; ~i; i = e[i].nxt) {int v = E[I].V;        if ((i ^ 1) = = e) continue; if (!            Dfn[v]) {Trajan (V, i);        Low[u] = min (Low[u], low[v]);        } else if (Instack[v]) {Low[u] = min (Low[u], low[v]);        }} if (dfn[u] = = Low[u]) {bsz++; int v;            do {v = stack[ssz--];            INSTACK[V] = 0;        BELONG[V] = Bsz;    } while (Ssz && v! = u);    }}void tarjan_solve (int n) {dsz = Bsz = SSZ = 0;    memset (DFN, 0, sizeof (DFN)); for (int i = 1; I <= n; i++) {if (!    Dfn[i]) Trajan (I,-1);    }/* Indent point */Edge_init ();        for (int i = 0; i < 2 * m; i + = 2) {int u = e[i].u, v = e[i].v; U = belong[u];        v = belong[v];        if (U = = v) continue;        Edge_add (U, v);    Edge_add (V, u);    }}int Main () {//fin int m, n; while (~SCANF ("%d%d", &m, &n)) {edge_init ();            for (int i = 1; I <= n; i++) {int u, v;            scanf ("%d%d", &u, &v);            Edge_add (U, v);        Edge_add (V, u);        } tarjan_solve (n);        int Q;        scanf ("%d", &q);            for (int i = 1; I <= Q; i++) {int u, v;            scanf ("%d%d", &u, &v);            if (belong[u] = = Belong[v]) printf ("yes\n");        else printf ("no\n"); }} return 0;}

  



51nod 1076 2 Non-intersecting path non-intersect component Trajan algorithm

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.