Watchcow

传送门

题目大意:

给你一幅连通的图,要求从起点1开始走,要经过每条边刚好两次,并且最终回到1起点。

思路:将无向图转换成有向图求欧拉回路。

#include<cstdio>
#define N 11000
#define M 110000//由于是将无向图转换成有向图,所以边变为原来的两倍。
               //因此*2,这个地方调了好几天,满满的都是泪啊。
int n,m;
struct map
{
    int tot;
    int head[N],v[M],pre[M];
    /*
      pre[]里存的是a点连得另一个点的编号。
      v[]存的是当前边连得b点。
    */
    bool f[M];
    void addedge(int a,int b)
    {
        tot++;
        v[tot]=b;
        pre[tot]=head[a];
        head[a]=tot;
    }
}G;
void dfs(int now)
{
    for (int p=G.head[now];p;p=G.pre[p])
      {
           if (!G.f[p])
             {
              G.f[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;
}
时间: 2025-01-04 22:15:32

Watchcow的相关文章

[2016-01-27][POJ][2230][Watchcow]

H - 欧拉图 中级者向 Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2230 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 s

POJ 2230 Watchcow

Watchcow Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 223064-bit integer IO format: %lld      Java class name: Main Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk

POJ2230 Watchcow【欧拉回路】

Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6172Accepted: 2663 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

POJ 2230 Watchcow 欧拉回路题解

本题就是以每个节点和节点之间建路,而且说明是无向图,不过这里有个技巧,就是根据题意把它当成有向图来做,就成了直接查找有向图的欧拉回路就可以了.因为题意是需要每条边都走两遍的,而且每次走的方向相反. 观察出这点,那么这道题就好做啦,直接建图,Feury递归求解就可以了. 建图注意需要建邻接表,不要建矩阵,因为建成矩阵,那么会很大很大,而根据题意,建成邻接表最多只需要5倍的顶点数. 打印的顺序是逆过来打和顺着打都可以的,因为先走那边都可以. #include <stdio.h> #include

[欧拉回路] poj 2230 Watchcow

题目链接: http://poj.org/problem?id=2230 Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6055   Accepted: 2610   Special Judge Description Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to walk acr

【poj2230】Watchcow

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'

POJ2230 Watchcow 【欧拉回路】+【DFS】

Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5964   Accepted: 2561   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 ev

POJ 2230 Watchcow &amp;&amp; USACO Watchcow 2005 January Silver (欧拉回路)

题意: Bessie 最近做了农场看守,他每天晚上的工作就是巡视农场并且保证没有坏人破坏农场.从谷仓出发去巡视,并且最终回到谷仓. Bessie 视力不是很好,不能像其他农场的看守一样,对农场的每一条连接不同场地的路走一遍就可以发现是不是有异常情况,他需要每条路都走两遍,并且这两边必须是不同的方向,因为他觉得自己应该不会两次都忽略农场中的异常情况. 每块地之间一定会由至少一条路相连.现在的任务就是帮他制定巡视路线.前提假设一定存在满足题意的路径. 输入: 第一行输入两个数N(2 <= N <=

POJ 2230 Watchcow (欧拉回路)

题目地址:POJ 2230 最普通的欧拉回路.边不重复记录点.不多说. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.