poj2230 Watchcow(欧拉回路)

题    目    传    送    门    在    这



题目大意

有一个由n个点和m条边组成的无向图。

让你从节点1出发,每条边恰好从正反两个方向走一遍。

输出任意一种方案。

解题思路

那这题和欧拉回路对比一下,发现欧拉回路是每个边直走一次,所以有以下代码。

vis[e] = vis[e ^ 1] = 1;

其中标记了当的方向和反边。

而这题是从两个方向走,所以对每个边只要标记以下当前的方向,每个边就会遍历两遍。

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#define rep(x, l, r) for(int x = l; x <= r; x++)
#define repd(x, r, l) for(int x = r; x >= l; x--)
#define clr(x, y) memset(x, y, sizeof(x))
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define MAXN 100005
#define MAXM 1000005
#define fi first
#define se second
#define SZ(x) ((int)x.size())
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int p = 10000007;
int lowbit(int x){ return x & -x; }
int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p; } return x; }

stack<int> sta;
int n, cnt, tot;
int head[MAXN], nxt[MAXM], to[MAXM];
int ans[2 * MAXN];
bool vis[MAXM];

void init(){
    cnt = 0;
    clr(head, -1);
}

void add(int u, int v){
    nxt[cnt] = head[u];
    head[u] = cnt;
    to[cnt] = v;
    cnt++;
}

void euler(){
    sta.push(1);
    while(!sta.empty()){
        int u = sta.top(), e = head[u];
        while(e != -1&& vis[e]) e = nxt[e];
        if(e != -1){
            int v = to[e];
            sta.push(v);
            vis[e] = 1;
            head[u] = nxt[e];
        }
        else{
            sta.pop();
            ans[++tot] = u;
        }
    }
}

int main(){
    init();
    int m;
    scanf("%d%d", &n, &m);
    rep(i, 1, m){
        int u, v;
        scanf("%d%d", &u, &v);
        add(u, v), add(v, u);
    }
    euler();
    repd(i, tot, 1) printf("%d\n", ans[i]);
    return 0;
}

原文地址:https://www.cnblogs.com/SHANAchan/p/10349433.html

时间: 2024-08-07 16:17:35

poj2230 Watchcow(欧拉回路)的相关文章

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 欧拉回路题解

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

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 (欧拉回路)

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

POJ2230(打印欧拉回路)

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

欧拉路,欧拉回路小结(转)

欧拉路,欧拉回路小结 把欧拉路和欧拉回路做一个小总结,包含了一些题目,以后遇到新的我还会陆续加上. 定义: 给定无孤立结点图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在 一条回路经过G每条边有且仅有一次,称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 关于欧拉路和欧拉回路定义及存在证明请看这里. 这里给出欧拉路和欧拉回路存在条件的结论: 存在欧拉路的条件: 无向图:  图连通,所有点都是偶数度,或者只有两个点是奇数度.当所有点是偶数度时欧拉路起点可以是任意 点:当

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i