hdu5215 DFS Dyeing award odd ring + Edge double connected component award even ring (and check set)

Source: Internet
Author: User
Tags min cmath

Cycle Time limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 131 Accepted Submission (s): 39


Problem Description Ery is interested in graph theory, today he ask Brotherk a problem on It:given you a undirected gr APh with N vertexes and M edges, you can select a vertex as your starting point, then you need to walk in the graph Alon G edges. However, you can ' t pass a edge more than once, even opposite direction is forbidden. At the end, you should come back to the starting point. Assume you have passed X edges, there is, and questions:

Question 1:can X Be a odd number?

Question 2:can X Be a even number?

(Note:you must walk, so X can ' t be 0)
Input The first line contains a single integer T, indicating the number of test cases.

Each test case is begins with the integer N, M, indicating the number of vertexes and the number of edges. Following M lines, each line contains the integers UI, Vi, indicating there is a edge between vertex Ui and vertex Vi .

T is about 30

1≤n≤100000

0≤m≤300000

1≤ui,vi≤n

Ui won't equal to Vi

There is at the most one edge between any pair of vertex.
Output for each test, print, lines.

The first line contains ' YES ' or ' NO ' for question 1.

The second line contains "YES" or "NO" for question 2.
Sample Input

3 1 0 3 3 1 2 2 3 3 1 4 4 1 2 2 3 3 4 4 1
Sample Output
No no yes no no yes Hint If need a larger stack size, please use #pragma comment (linker, "/stack:102400000,102400000") and submit your solution using C + +.
Source Race Code "Bestcoder" Cup Chinese college students program Design Championship


Topic Connection http://acm.hdu.edu.cn/showproblem.php?pid=5215

Test instructions: Give you an no-map, start from a point and then return to that point, and each side can only walk once, ask whether there are odd ring and even ring.

Idea: To judge the odd ring, you can use DFS staining to determine whether it is a binary graph, there must be no singular ring in the binary map, and the non-binary map must have a singular ring.

Judging the coupling ring, we can find out whether the double connected component of the graph has only one singular ring, otherwise there must be even ring, because the double connected component consisting of multiple rings must exist even rings.

Code:

#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include < string> #include <vector> #include <queue> #include <cmath> #include <stack> #include <set > #include <map> #define INF 0xfffffff #define MAX 1000005 #define MOD 10000007 #define CLR (b) memset ((a), (b), si Zeof ((a))) #pragma comment (linker, "/stack:102400000,102400000") #define UL u<<1 #define UR (u<<1) | using NA
Mespace std;
typedef long Long LL;
struct Edge {int v,next;} e[max*4];
int pre[max],low[max],bcc_cnt=0,dfs_clock=0;
int Head[max],bridge[max*4],tot,vis[max],color[max];
    void Addedge (int u,int v) {e[tot].v=v;
    E[tot].next=head[u];
head[u]=tot++;
} int odd=0,even=0;
    void Dfs (int u) {if (odd) return;
        for (int i=head[u]; i!=-1; i=e[i].next) {int v=e[i].v;
            if (color[v]==-1) {color[v]= (color[u]+1)%2;
        DFS (v);
} else if (Color[v]==color[u]) {odd=1;            return;
    }}} void TDFs (int u,int fa) {pre[u]=low[u]=++dfs_clock;
        for (int i=head[u]; i!=-1; i=e[i].next) {int v=e[i].v;
            if (!pre[v]) {TDFs (v,u);
            Low[u]=min (Low[u],low[v]);
            if (Low[v]>pre[u]) {bridge[i]=bridge[i^1]=1;
    }} else if (PRE[V]&LT;PRE[U]&AMP;&AMP;V!=FA) low[u]=min (Low[u],pre[v]);
    }} void Find_bcc (int n) {CLR (pre,0);
    CLR (low,0);
        for (int i=1; i<=n; i++) {if (!pre[i]) {TDFs (i,-i);
}}} int v_cnt,e_cnt;
    void dfs2 (int u) {vis[u]=1;
    v_cnt++;
        for (int i=head[u]; i!=-1; i=e[i].next) {if (bridge[i]) continue;
        int v=e[i].v;
        e_cnt++;
        if (!vis[v]) {DFS2 (v);
    }}} void Init () {tot=0;
    CLR (head,-1);
    CLR (color,-1);
    CLR (vis,0);
CLR (bridge,0);
    } int main () {int t;
    scanf ("%d", &t);
        while (t--) {Init ();
        int n,m,u,v;
        scanf ("%d%d", &n,&m);
            for (int i=1; i<=m; i++) {scanf ("%d%d", &u,&v);
            Addedge (U,V);
        Addedge (V,u);
        } odd=even=0;
                for (int i=1; i<=n; i++) {if (color[i]==-1) {color[i]=0;
                DFS (i);
            if (odd) break;
        }} find_bcc (n);
            for (int i=1; i<=n; i++) {v_cnt=e_cnt=0;
            if (!vis[i]) {DFS2 (i);
            }else continue;
            if (v_cnt==1) continue;
            e_cnt/=2; if (! (
                E_cnt==v_cnt&&v_cnt%2==1)) {even=1;
            Break
        }} if (odd) printf ("yes\n");
        else printf ("no\n");
        if (even) printf ("yes\n");
    else printf ("no\n");
} return 0;
 }


And check the set:

U,v two points into a 2u,2u+1,v,2v+1, if the 2u and 2v+1 have been linked to the existence of even ring, otherwise 2u and 2v+1 connected, 2u+1 and 2v connected, each connection found 2u and 2u+1 connected to the existence of odd ring.

Proof: The point is split and then cross-linked, point u after odd number of times back to u, will lead to their two points back to a set, if after an even number of times back to u, which will find that the time to connect 2u, has been and 2v+1 in the same set.

#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include < string> #include <vector> #include <queue> #include <cmath> #include <stack> #include <set > #include <map> #define INF 0xfffffff #define MAX 1000005 #define MOD 10000007 #define CLR (b) memset ((a), (b), si Zeof ((a))) #pragma comment (linker, "/stack:102400000,102400000") #define UL u<<1 #define UR (u<<1) | using NA
Mespace std;
typedef long Long LL;
int Pre[2*max];
    int find (int r) {while (R!=pre[r]) {r=pre[r]=pre[pre[r]];
} return R;
} int even,odd;
    void Add (int u,int v) {int a=find (u);
    int B=find (v);
    if (a!=b) {pre[a]=b;
    }} int main () {int t;
    scanf ("%d", &t);
        while (t--) {int n,m,u,v;
        scanf ("%d%d", &n,&m);
        odd=0,even=0;
        for (int i=1;i<=2* (n+1); i++) pre[i]=i; for (int i=1; i<=m; i++) {scanf ("%d%d", &u,&v);
            if (Odd&&even) continue;
            if (Find (2*u) ==find (2*v+1)) even=1;
            Add (2*u,2*v+1);
            Add (2*U+1,2*V);
        if (Find (u*2) ==find (u*2+1)) odd=1;
        } if (odd) printf ("yes\n");
        else printf ("no\n");
        if (even) printf ("yes\n");
    else printf ("no\n");
} 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.