51nod1076 (side double Unicom component) __ graph theory

Source: Internet
Author: User

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;
 }

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.