Links: Click to open the link
The vertex v and Edge E of a non-direction graph G are given. Q Query, query from a vertex of G v[s] to another vertex v[t], whether there are 2 disjoint paths. (Two paths do not pass the same edge)
(Note, there is no heavy edge in the graph, that is, to determine the starting point and end point, there are at most 1 routes between them)
Code:
#include <queue> #include <vector> #include <stdio.h> #include <stdlib.h> #include <string.h
> #include <iostream> #include <algorithm> using namespace std;
const int siz=50005;
struct node{int to,id;
Vector<node> G[siz];
int Low[siz],dfn[siz],sig[siz],vis[siz];
void Dfs (int s,int fa,int ID) {int i,tmp;
Vis[s]=1;
dfn[s]=low[s]=id++;
For (I=0;i<g[s].size (); i++) {tmp=g[s][i].to;
if (vis[tmp]==0) {DFS (tmp,s,id);
Low[s]=min (Low[s],low[tmp]);
if (Low[tmp]>dfn[s]) sig[g[s][i].id]=1;
else if (TMP!=FA) low[s]=min (low[s],dfn[tmp]);
} void cal (int s,int id) {int i,tmp;
Vis[s]=id;
For (I=0;i<g[s].size (); i++) {tmp=g[s][i].to; if (vis[tmp]| |
Sig[g[s][i].id]) continue;
Cal (Tmp,id); }///Run all the bridges first, then DFS tag, implement int main () { Edge Double unicom component int n,m,i,j,u,v,q,id;
while (scanf ("%d%d", &n,&m)!=eof) {for (i=1;i<=n;i++) g[i].clear ();
for (i=1;i<=m;i++) {scanf ("%d%d", &u,&v);
G[u].push_back ((node) {v,i});
G[v].push_back ((node) {u,i});
} memset (Vis) (vis,0,sizeof);
Memset (SIG) (sig,0,sizeof);
id=1;
for (i=1;i<=n;i++) {//Note that the initial diagram may not be connected to the IF (vis[i]==0) DFS (I,-1,ID);
} id=1;
memset (Vis) (vis,0,sizeof);
for (i=1;i<=n;i++) {if (vis[i]==0) {cal (I,id);
id++;
} scanf ("%d", &q);
while (q--) {scanf ("%d%d", &u,&v);
if (Vis[u]==vis[v]) puts ("Yes");
Else puts ("No");
} return 0;
}