poj 2230 第一道欧拉路题目

Watchcow
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 6457 Accepted: 2818 Special Judge

Description

Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk across the farm and make sure that no evildoers are doing any evil. She begins at the barn, makes her patrol, and then returns to the barn when she's done.

If she were a more observant cow, she might be able to just walk each of M (1 <= M <= 50,000) bidirectional trails numbered 1..M between N (2 <= N <= 10,000) fields numbered 1..N on the farm once and be confident that she's seen everything she needs to see. But since she isn't, she wants to make sure she walks down each trail exactly twice. It's also important that her two trips along each trail be in opposite directions, so that she doesn't miss the same thing twice.

A pair of fields might be connected by more than one trail. Find a path that Bessie can follow which will meet her requirements. Such a path is guaranteed to exist.

Input

* Line 1: Two integers, N and M.

* Lines 2..M+1: Two integers denoting a pair of fields connected by a path.

Output

* Lines 1..2M+1: A list of fields she passes through, one per line, beginning and ending with the barn at field 1. If more than one solution is possible, output any solution.

Sample Input

4 5
1 2
1 4
2 3
2 4
3 4

Sample Output

12342143241


题意:一个人需要经过每个地点两次,求出一条路径,因为题目保证存在,所以直接dfs就行了:

由于有向图一个点要走2次,需要判断这个点是否走过,以下用两种方式判断

法一:在存储表里设一个flag来判断,并作一个edge型vector数组

#include<iostream>
#include<string>
#include<vector>
using namespace std;
#define MAXN 10002
int n, m;
struct edge
{
	int v;
	bool flag;
};
vector<edge>pos[MAXN];
void dfs(int x)
{
	for (int i = 0; i < pos[x].size();i++)
	if (!pos[x][i].flag){
		pos[x][i].flag = 1;
		dfs(pos[x][i].v);
	}
	cout << x << endl;
}
int main()
{
	cin >> n >> m;
	int a, b;
	edge temp;
	for (int i = 0; i < m; i++){
		cin >> a >> b;
		temp.flag = 0;
		temp.v = a;
		pos[b].push_back(temp);
		temp.v = b;
		pos[a].push_back(temp);

	}
	dfs(1);
	return 0;
}

法二:用一般的邻接表来表示,属于模板型的,开2倍大的标记数组

#include <iostream>
#include <stdio.h>
using namespace std;
const int maxm = 2*50000 + 1;
const int maxv = 10000 + 5;
struct edge{
    int to;
    int next;
};
edge node[maxm]; /*邻接表*/
int adj[maxv];
bool used[maxm];/* 标记边是否访问过*/
void Euler(int vertix)
{
    for(int i = adj[vertix]; i != -1; i = node[i].next)
    {
        if(!used[i])
        {
            used[i] = 1;
            Euler(node[i].to);
        }
    }
    printf("%d\n", vertix);
}
int main()
{
    int n = 0;
    int m = 0;
    int i = 0;
    int u = 0;
    int v = 0;
    int cnt = 0;
    scanf("%d%d", &n, &m);
    for(i = 0; i <= n; ++i)
        adj[i] = -1;
    for(i = 0; i <= m*2; ++i)
        used[i] = 0;
    for(i = 0; i < m; ++i)
    {
        scanf("%d%d", &u, &v);
        //u->v
        node[cnt].to = v;
        node[cnt].next = adj[u];
        adj[u] = cnt++;
        //v->u
        node[cnt].to = u;
        node[cnt].next = adj[v];
        adj[v] = cnt++;
    }
    Euler(1);
    return 0;
}


在网上搜代码的时候还发现了这个,在结构体中存储数组,第一次见啊,关系有点乱,以后再看看吧

#include <cstdio>
#include <cstring>
#include <cstdlib>
#define MAXM 100002
#define MAXN 10002
int N,M;
struct eList
{
	int Head[MAXN],Pre[MAXM],V[MAXM],tot;
	bool Vis[MAXM];
	void AddEdge(int a,int b)
	{
		++tot;V[tot]=b;Pre[tot]=Head[a];Head[a]=tot;
	}
}G;
int Count=0;
int Ans[MAXM],AnsT;
void DFS(int now)
{
	for (int p=G.Head[now];p;p=G.Pre[p])
	{
		if (!G.Vis[p])
		{
			G.Vis[p]=1;
			DFS(G.V[p]);
		}
	}
	printf("%d\n",now);
}
int main()
{
	scanf("%d%d",&N,&M);
	for (int i=1;i<=M;++i)
	{
		int a,b;
		scanf("%d%d",&a,&b);
		G.AddEdge(a,b);
		G.AddEdge(b,a);
	}
	DFS(1);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值