Codeforces 405e DFS

Source: Internet
Author: User

This topic requires that all the edges in a undirected connected graph be divided into two pairs, which can only appear once, and one side must be connected together. vertices can be reused but edges cannot be reused.

The solvable condition is very easy, because the graph is connected, as long as the number of edges is an even number.

In the beginning, I directly searched for DFS brute-force attacks by using the Euler loop method, marking the path, and outputting the unlabeled two consecutive edges.

However, it turns out that this algorithm is wrong.

Violent search can be established only when the edge on the graph can exist in many edge pairs, but some images certainly do not meet this condition.

In fact, the solution is to consider the special side based on DFS.

That is, each time a vertex is located, the subnode is DFS. If a single edge is found under the subnode, the current edge and the subnode are merged and output.

Otherwise, the current edge will be stored in the queue that exclusive to the point. After all is saved, the current edge will be output in pairs. If there is a single side, return it to the father and tell him that there is a single side here.

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <queue>using namespace std;const int N=100000+10;int u[N<<1],v[N<<1],nt[N<<1],ft[N];int n,m,cnt;int vis[N<<1];void add(int a,int b){    u[cnt]=a;    v[cnt]=b;    nt[cnt]=ft[a];    ft[a]=cnt++;}int dfs(int x,int f){    queue<int> vec;    for (int i=ft[x];i!=-1;i=nt[i]){        int nx=v[i];        if (vis[i] || nx==f) continue;        vis[i]=vis[i^1]=1;        int r=dfs(nx,x);        if (r){            printf("%d %d %d\n",x,nx,r);        }        else{            vec.push(nx);        }    }    while (vec.size()>=2){        int a=vec.front();        vec.pop();        int b=vec.front();        vec.pop();        printf("%d %d %d\n",a,x,b);    }    if (!vec.empty()){        int a=vec.front();        vec.pop();        return  a;    }    return 0;}int main(){    while (scanf("%d%d",&n,&m)!=EOF)    {        cnt=0;        memset(ft,-1,sizeof ft);        memset(vis,0,sizeof vis);        int a,b;        for (int i=0;i<m;i++){            scanf("%d%d",&a,&b);            add(a,b);            add(b,a);        }        //cout<<m<<" "<<(m&1)<<endl;        if (m&1){            puts("No solution");            continue;        }        dfs(1,-1);    }    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.