题目链接: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. Windows开发:窗口最大化问题

    MoveWindow(0, 0, 1920, 1080)和SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0)的区别 MoveWindow(, , , ); 我们可以分 ...

  2. AT3913 XOR Tree(巧妙转换+状压dp)

    Step1:首先定义一个点的权值为与其相连边的异或和.那么修改一条路径,权值改变的只有两个端点.边权都为0和点权都为0实质相同. Step2:那么现在和树的结构就没有什么关系了.每次选两个点,然后同时 ...

  3. MySQL备忘点(上)

    给自己看的,所以以举例子为主了 检索数据 SELECT 检索单列 SELECT name FROM student 检索多列 SELECT no, name FROM student  检索所有列 S ...

  4. Mybatis 返回值 返回Map的为空的值

    第一种.springMVC和boot通用配置:(Mybatis.xml) <?xml version="1.0" encoding="UTF-8"?> ...

  5. 使用ICEM绘制非结构网格时,如何提高网格质量?【转载】

    作者:杨淑娟 链接:https://www.zhihu.com/question/20851390/answer/26152732 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  6. 为什么使用Spring Boot

    原文:https://dzone.com/articles/why-springboot 作者:Siva Prasad Reddy Katamreddy 译者:Oopsguy 本文将介绍各种 Spri ...

  7. LinkedBlockingQueue和ArrayBlockingQueue的异同

    相同: 1.LinkedBlockingQueue和ArrayBlockingQueue都实现了BlockingQueue接口: 2.LinkedBlockingQueue和ArrayBlocking ...

  8. Maven的New中没有Servlet问题(IDEA)

    1.问题 第一次使用Maven骨架创建Web项目的时候,遇到了 New 里面没有 servlet 的问题. 2.原因 经过查询,是因为IDEA检测到项目中没有导入相关的 jar 包导致. 3.解决方法 ...

  9. Gurobi建模遇到的坑

    1.quicksum好像不支持嵌套 最好还是尽可能多的使用一些中间变量来表达. 另外,quicksum()返回的是表达式而不是var,像addGenConstrMax(resvar, [var, va ...

  10. [oracle]TX行锁发生的常见场景(转贴)

    TX行锁发生的常见场景: 1.当前会话要更新或删除的记录,已经被其他会话更新或删除. 2.对于表上有唯一索引的情况,多个会话插入或更新为相同的键值. 3.对于表上有位图索引的情况,多个会话即使更新不同 ...