poj_2230 Watchcow

Watchcow

Time Limit: 3000MS

Memory Limit: 65536K

Total Submissions: 4550

Accepted: 1898

Special Judge

题目链接:http://poj.org/problem?id=2230

Description

Bessie's been appointed thenew watch-cow for the farm. Every night, it's her job to walk across the farmand 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'sseen everything she needs to see. But since she isn't, she wants to make sureshe walks down each trail exactly twice. It's also important that her two tripsalong each trail be in opposite directions, so that she doesn't miss the samething twice.

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

Input

* Line 1: Two integers, N andM.

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

Output

* Lines 1..2M+1: A list offields she passes through, one per line, beginning and ending with the barn atfield 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

1

2

3

4

2

1

4

3

2

4

1

Hint

OUTPUT DETAILS:

Bessie starts at 1 (barn), goes to 2, then 3, etc...

Source

USACO 2005 JanuarySilver

题意:

         给你一张图,让你按不同顺序访问该图的所有边刚好2次。

解题思路:

       这个题目用深搜就可以搞定,一般深搜是以点来判断是否已经搜索过,而这个是用边来判断,直到每条边都被搜索过,存储的时候应该是存储的无向图来当有向图,访问一条边就设置为访问过。

代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include <vector>
#define N 10003

using namespace std;

struct edge
{
    int end;
    int flag;
};
vector<edge> g[N];

int n ,m;

//递归深搜,遍历每一条边
void DFS(int s)
{

    for(int i=0;i<g[s].size();i++)
    {
        if(g[s][i].flag==1)
        {
            g[s][i].flag=0;
            DFS(g[s][i].end);
        }
    }
    printf("%d\n",s);
}
int main()
{
    scanf("%d%d",&n,&m);
    int i;
    int s,e;
    memset(g,0,sizeof(g));
    for(i=1;i<=m;i++)
    {
        scanf("%d%d",&s,&e);
        edge temp;
        temp.end=e;
        temp.flag=1;
        g[s].push_back(temp);
        temp.end=s;
        g[e].push_back(temp);
    }
    DFS(1);
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值