poj_2230 Watchcow

news/2024/7/10 4:57:56 标签: output, each, pair, path, 存储, input

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


 


http://www.niftyadmin.cn/n/862609.html

相关文章

2021.11.20【读书笔记】|差异可变剪接事件及DTU分析

一、可变剪接(Alternative Splicing) 定义&#xff1a; 同一前体mRNA分子&#xff0c;可以在不同的剪接位点发生剪接反应&#xff0c;生成不同的mRNA分子&#xff0c;最终产生不同的蛋白质分子的一种RNA剪切方式。意义&#xff1a; 1. AS是形成生物多样性的重要原因之一2. AS是…

PHP使用数据库永久连接方式操作MySQL的是与非

PHP程序员应该都知道连接MySQL数据库可以使用mysql_pconnect&#xff08;永久连接&#xff09;函数&#xff0c;使用数据库永久连接可以提高效率&#xff0c;但是实际应用中数据库永久连接往往会导致出现一些问题&#xff0c;通常的表现就是在大访问量的网站上时常发生断断续续…

Web2.0网站性能调优实践

当前web2.0革命风起云涌&#xff0c;web2.0强调服务&#xff0c;而服务最基本的要求是速度快和稳定&#xff0c;离开这两个谈功能强大和易用性都没有任何意义。本文介绍一些关于笔者运营一个web2.0网站的优化心得和经验&#xff0c;希望能够和大家共同探讨。 Web2.0网站不同于以…

poj_1258 Agri-Net

Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28121 Accepted: 11149 题目链接&#xff1a;http://poj.org/problem?id1258 Description Farmer John has been electedmayor of his town! One of his campaign promises was to bring internet…

poj_1679 The Unique MST

The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14582 Accepted: 5035 题目连接&#xff1a;http://poj.org/problem?id1679 Description Given a connected undirectedgraph, tell if its minimum spanning tree is unique. Definition …

PHP开发规范!

一、规范前言篇 标准化不是特殊的个人风格&#xff0c;它让程序员可以了解任何代码&#xff0c;弄清程序的状况&#xff1b;新人可以很快的适应环境&#xff1b;防止新接触php的人一次次的犯同样的错误&#xff1b;在一致的开发环境下&#xff0c;可以减少人们犯错的机会。本规…

poj_1789 Truck History

Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12456 Accepted: 4713 题目链接&#xff1a;http://poj.org/problem?id1789 Description Advanced Cargo Movement, Ltd.uses trucks of different types. Some trucks are used for vegetab…

poj_2485 Highways

Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16037 Accepted: 7460 题目链接&#xff1a;http://poj.org/problem?id2485 Description The island nation of Flatopiais perfectly flat. Unfortunately, Flatopia has no public highways. So…