Watchcow(poj 2230)

Watchcow

求无向图每条边恰好经过两次,在回到原点,输出经过的顶点。容易转化为有向图欧拉回路每条边经过一次。

代码:

#include <stdio.h>
#include <stack>
#include <vector>
#include <iostream>
using namespace std;

#define M 100005

struct edge{
	int to,flag;
	edge(int to,int flag):to(to),flag(flag){}
};

int vis[M] = {0};
vector<edge> G[M];

stack <int> s;

void dfs(int u)
{
	for(int i=0;i<G[u].size();i++)
	{
		if(!G[u][i].flag)
		{
			G[u][i].flag=1;
			dfs(G[u][i].to);
			s.push(G[u][i].to);
		}
	}
}

int main()
{
    int n, m, a, b;

    scanf("%d%d", &n, &m);
    
    for (int i = 1; i <= m; ++i)
    {
        scanf("%d%d", &a, &b);
        G[a].push_back(edge(b,0));
        G[b].push_back(edge(a,0));
    }

    dfs(1);

    printf("1\n");
    while (s.size())
    {
        printf("%d\n", s.top());
        s.pop();
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37846371/article/details/80330176