`
huobengluantiao8
  • 浏览: 1029830 次
文章分类
社区版块
存档分类
最新评论

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;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics