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

Sightseeing tour
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:10837   Accepted: 4560

Description

The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. They want to construct the tour so that every street in the city is visited exactly once. The bus should also start and end at the same junction. As in any city, the streets are either one-way or two-way, traffic rules that must be obeyed by the tour bus. Help the executive board and determine if it's possible to construct a sightseeing tour under these constraints.

Input

On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two positive integers m and s, 1 <= m <= 200,1 <= s <= 1000 being the number of junctions and streets, respectively. The following s lines contain the streets. Each street is described with three integers, xi, yi, and di, 1 <= xi,yi <= m, 0 <= di <= 1, where xi and yi are the junctions connected by a street. If di=1, then the street is a one-way street (going from xi to yi), otherwise it's a two-way street. You may assume that there exists a junction from where all other junctions can be reached.

Output

For each scenario, output one line containing the text "possible" or "impossible", whether or not it's possible to construct a sightseeing tour.
题目大意:给出一张混合图(有双向边以及单向边),求是否存在欧拉路径
解题思路:
2.说说自己对混合图欧拉回路的理解。因为有向边的存在,所以一定是需要满足每个点的入度等于出度,但是无向边,我们可以去进行两种选择,来达到寻找欧拉回路的目的。所以首先我们要对无向边进行定向处理,即随意的给无向边一个方向。这样转化成了有向图。
3.转化成有向图之后我们要计算每个点的出入以及出度,若其奇偶性不同,很明显无法通过自调整来实现入度与出度的相等,这幅图就一定不存在欧拉回路。
4.自调整的过程就是跑最大流的过程。对于建图:无向边定向并且容量为1,有向边不可以加入建图中,因为有向边是不可以进行自调整的。对于每个出度大于入度的点,我们从源点向该点连边,边权为出度与入度之差的一半,对于每个入度大于出入的点,从该点向汇点连边,边权为入度与出度之差的一半。从源点到汇点跑一遍最大流。
5.若最大流等于从源点出发的边权之和,则存在欧拉回路。
代码如下:
 #include<stdio.h>
#include<string.h>
#include<queue>
#define mem(a, b) memset(a, b, sizeof(a))
const int MAXN = ;
const int MAXM = ;
const int inf = 0x3f3f3f3f;
using namespace std; int n, m, st, ed, tot; //n个点 m条边(有向 + 无向)
int out[MAXN], in[MAXN], head[MAXN], cnt;
queue<int> Q;
int dep[MAXN]; struct Edge
{
int to, next, flow;
}edge[ * MAXM]; void add(int a, int b, int c)
{
cnt ++;
edge[cnt].to = b;
edge[cnt].next = head[a];
edge[cnt].flow = c;
head[a] = cnt;
} int build()
{
for(int i = ; i <= n; i ++)
{
if((out[i] + in[i]) % ) //如果存在有点的出入跟入度的奇偶性不同 那么无论如何调节都无法做到入度和出度相等
return ;
if(out[i] > in[i])
{
int x = (out[i] - in[i]) / ;
tot += x;
add(st, i, x);
add(i, st, );
}
else if(in[i] > out[i])//入度与出度相等的情况 加不加边无影响
{
int x = (in[i] - out[i]) / ;
add(i, ed, x);
add(ed, i, );
}
}
return ;
} int bfs()
{
if(st == ed)
return ;
mem(dep, -);
dep[st] = ;
Q.push(st);
while(!Q.empty())
{
int index = Q.front();
Q.pop();
for(int i = head[index]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if(edge[i].flow > && dep[to] == -)
{
dep[to] = dep[index] + ;
Q.push(to);
}
}
}
return dep[ed] != -;
} int dfs(int now, int zx)
{
if(now == ed)
return zx;
for(int i = head[now]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if(dep[to] == dep[now] + && edge[i].flow > )
{
int flow = dfs(to, min(zx, edge[i].flow));
if(flow > )
{
edge[i].flow -= flow;
edge[i ^ ].flow += flow;
return flow;
}
}
}
return -;
} int dinic()
{
int ans = ;
while(bfs())
{
while()
{
int inc = dfs(st, inf);
if(inc == -)
break;
ans += inc;
}
}
return ans;
} int main()
{
int T;
scanf("%d", &T);
while(T --)
{
mem(out, ), mem(in, ), mem(head, -);
cnt = -, tot = ;
scanf("%d%d", &n, &m);
st = , ed = n + ;
for(int i = ; i <= m; i ++)
{
int a, b, op;
scanf("%d%d%d", &a, &b, &op);
out[a] ++ ,in[b] ++;
if(op == ) //只有无向边才能自调节
{
add(a, b, );
add(b, a, );
}
}
if(build())
{
int maxflow = dinic();
if(maxflow == tot) //最大流等于源点出去的边满流
printf("possible\n");
else
printf("impossible\n");
}
else
printf("impossible\n");
}
return ;
}

Sightseeing tour 【混合图欧拉回路】的更多相关文章

  1. POJ 1637 Sightseeing tour ★混合图欧拉回路

    [题目大意]混合图欧拉回路(1 <= N <= 200, 1 <= M <= 1000) [建模方法] 把该图的无向边随便定向,计算每个点的入度和出度.如果有某个点出入度之差为 ...

  2. poj1637 Sightseeing tour(混合图欧拉回路)

    题目链接 题意 给出一个混合图(有无向边,也有有向边),问能否通过确定无向边的方向,使得该图形成欧拉回路. 思路 这是一道混合图欧拉回路的模板题. 一张图要满足有欧拉回路,必须满足每个点的度数为偶数. ...

  3. POJ1637 Sightseeing tour (混合图欧拉回路)(网络流)

                                                                Sightseeing tour Time Limit: 1000MS   Me ...

  4. poj 1637 Sightseeing tour 混合图欧拉回路 最大流 建图

    题目链接 题意 给定一个混合图,里面既有有向边也有无向边.问该图中是否存在一条路径,经过每条边恰好一次. 思路 从欧拉回路说起 首先回顾有向图欧拉回路的充要条件:\(\forall v\in G, d ...

  5. POJ 1637 Sightseeing tour (混合图欧拉回路)

    Sightseeing tour   Description The city executive board in Lund wants to construct a sightseeing tou ...

  6. poj1637 Sightseeing tour 混合图欧拉回路判定

    传送门 第一次做这种题, 尽管ac了但是完全不知道为什么这么做. 题目就是给一些边, 有向边与无向边混合, 问你是否存在欧拉回路. 做法是先对每个点求入度和出度, 如果一条边是无向边, 就随便指定一个 ...

  7. poj1637Sightseeing tour(混合图欧拉回路)

    题目请戳这里 题目大意:求混合图欧拉回路. 题目分析:最大流.竟然用网络流求混合图的欧拉回路,涨姿势了啊啊.. 其实仔细一想也是那么回事.欧拉回路是遍历所有边一次又回到起点的回路.双向图只要每个点度数 ...

  8. POJ1637:Sightseeing tour(混合图的欧拉回路)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10581   Accepted: 4466 ...

  9. POJ 1637 Sightseeing tour(混合图的欧拉回路)

    题目链接 建个图,套个模板. #include <cstdio> #include <cstring> #include <iostream> #include & ...

  10. POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]

    嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...

随机推荐

  1. 007_linuxC++之_构造函数的初级应用

    (一)构造函数:用来在创建对象时初始化对象, 即为对象成员变量赋初始值 (二)构造函数的命名必须和类名完全相同 (三)更对具体的查看:构造函数 (四)直接分析程序 运行结果 解析上面程序: 1. 当程 ...

  2. Oracle 物理结构(七) 文件-归档日志文件

    Oracle 物理结构(七) 文件-归档日志文件

  3. 09_编写脚本,实现人机<石头,剪刀,布>游戏

    #!/bin/bashgame=(石头 剪刀 布)num=$[RANDOM%3]computer=${game[$num]}#通过随机数获取计算机的出拳#出拳的可能性保存在一个数组中,game[0], ...

  4. 51nod 1060

    反素数定义:对于任意正整数 $n$, 其约数个数记为 $f(n)$, 如果某个正整数 $n$ 满足 对于任意正整数 $i, (0 < i < n)$, 都有 $f(i) < f(n) ...

  5. 自己实现dup2

    转自 http://blog.csdn.net/todd911/article/details/11747097 #include <stdio.h> #include <unist ...

  6. 泰乐事(Telos)白皮书中文版 <一> 泰乐事项目(未完成)

    泰乐事项目 一个可持续发展的去中心化EOSIO网络 EOS系统在设计上为人们带来了令人惊喜的一种实现新经济模式的承诺.然而很不幸,其高度中心化的运营正不断地破坏这种承诺.90%的EOS代币掌握在仅仅1 ...

  7. 3-2新建Photoshop图像

    http://www.missyuan.com/thread-350740-1-1.html   [CTRL N][文件 新建] 按住CTRL双击Photoshop的空白区(这个好像是打开文件){快捷 ...

  8. MySQL数据分析-(11)表补充:数据类型

    大家好,我是jacky,很高兴继续跟大家学习<Mysql 数据分析实战系列教程>,上次课程jacky讲解了表层面的增删改查,jacky说最重要的是增,增就是创建表,作为一个严谨的MySQL ...

  9. ThreadGroupAPI

    官方解释 public class ThreadGroup extends Object implements Thread.UncaughtExceptionHandler A thread gro ...

  10. JQuery 行内编辑(即点即改)

    行内编辑 下面是详细的代码: <style> .dian { cursor: pointer; } </style> //这个让鼠标 移动到 span上 的时候 是一个小手 & ...