Sightseeing tour

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

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

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.

Sample Input:

4
5 8
2 1 0
1 3 0
4 1 1
1 5 0
5 4 1
3 4 0
4 2 1
2 2 0
4 4
1 2 1
2 3 0
3 4 0
1 4 1
3 3
1 2 0
2 3 0
3 2 0
3 4
1 2 0
2 3 1
1 2 0
3 2 0

Sample Output:

possible
impossible
impossible
possible

题意:

输入包含多组数据,然后给出一个混合图(既有有向边也有无向边),现在问是否能从一个起点出发,经过每一条边一次又重新回到这一个点。

PS:双向边也只能经过一次。

题解:

如果这个题就为单纯的有向图或者无向图就好办了,我们只需要判断每个点的入度和出度就ok了,但这是混合图....反正我开始并没想到用最大流= =

对于欧拉路径,最不可少的就是每个点入度和出度的度数了,我们还是可以统计每个点的入度、出度度数。

假设对于一个点来说,如果将一条边反向,入度和出度的变化之和为0。我们就可以利用这一性质来判断可行性。

假定现在存在了欧拉路径,说明我们至少有一种将一些边反向的方案能够满足条件。

对于双向边而言,我们就先假定任意一个方向然后再来统计度数并且加边,有向边边权为0,意即流不能从这条边通过。

我们假定最大流经过的边就是能够反向的边,那么我们将每条“有向边”(其实是无向边)的容量设置为1即可。

对于源点和汇点,假如一个点当前的入度大于出度,那么源点与之相连,边权为(入度-出度)/2(意即需要将多少边反向);对于汇点也同理。

最后我们跑最大流看看能否满流就行了。

思路还是很巧妙的,主要就在于定无向为有向,再利用最大流来考虑将边反向。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define s 0
#define t n+1
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = ,M = ;
int n,m,T,tot;
int head[N],in[N],out[N],d[N];
struct Edge{
int v,next,c;
}e[M<<];
void adde(int u,int v,int c){
e[tot].v=v;e[tot].next=head[u];e[tot].c=c;head[u]=tot++;
e[tot].v=u;e[tot].next=head[v];e[tot].c=;head[v]=tot++;
}
bool bfs(int S,int T){
memset(d,,sizeof(d));d[S]=;
queue <int > q;q.push(S);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(!d[v] && e[i].c>){
d[v]=d[u]+;
q.push(v);
}
}
}
return d[T]!=;
}
int dfs(int S,int a){
int flow=,f;
if(S==t || a==) return a;
for(int i=head[S];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]!=d[S]+) continue ;
f=dfs(v,min(a,e[i].c));
if(f){
e[i].c-=f;
e[i^].c+=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[S]=-;
return flow;
}
int Dinic(){
int max_flow=;
while(bfs(,t)){
max_flow+=dfs(,INF);
}
return max_flow;
}
int main(){
scanf("%d",&T);
while(T--){
tot=;memset(head,-,sizeof(head));
memset(in,,sizeof(in));memset(out,,sizeof(out));
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int u,v,op;
scanf("%d%d%d",&u,&v,&op);
if(op==) adde(u,v,);
else adde(u,v,);
in[v]++;out[u]++;
}
int flag=,sum=;
for(int i=;i<=n;i++){
int now = in[i]-out[i];
if(now&) flag=;
if(now>) adde(i,t,now/);
if(now<=) adde(s,i,-now/),sum+=now/;
}
sum=-sum;
if(!flag){
puts("impossible");
continue ;
}
int flow = Dinic();
if(flow==sum) puts("possible");
else puts("impossible");
}
return ;
}

POJ1637:Sightseeing tour(混合图的欧拉回路)的更多相关文章

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

                                                                Sightseeing tour Time Limit: 1000MS   Me ...

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

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

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

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

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

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

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

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

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

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

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

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

  8. [POJ1637]混合图的欧拉回路判定|网络流

    混合图的欧拉回路判定 上一篇正好分别讲了有向图和无向图的欧拉回路判定方法 如果遇上了混合图要怎么做呢? 首先我们思考有向图的判定方法:所有点的出度=入度 我们可以先为无向边任意定一个向,算出此时所有顶 ...

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

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

随机推荐

  1. python三大神器之生成器

    生成器Generator: 本质:迭代器(所以自带了__iter__方法和__next__方法,不需要我们去实现) 特点:惰性运算,开发者自定义 在python中有三种方法来获取生成器: 1.通过生成 ...

  2. HDU1209:Clock

    参考:https://blog.csdn.net/libin56842/article/details/8990530 https://blog.csdn.net/u011479875/article ...

  3. 【Leetcode】Jewels and Stones

    Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...

  4. shell重温---基础篇(输入/输出重定向)

        大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回​​到您的终端.一个命令通常从一个叫标准输入的地方读取输入,默认情况下,这恰好是你的终端.同样,一个命令通常将其输出写入到标准 ...

  5. React实现最完整的百度搜索框

    import React,{Component} from 'react' import ReactDOM,{render} from 'react-dom' import 'bootstrap/di ...

  6. 【jQuery】 实用 js

    [jQuery] 实用 js 1. int 处理 parseInt(") // int 转换 isNaN(page) // 判断是否是int类型 2. string 处理 // C# str ...

  7. js鼠标事件相关知识

    1.mousedown->mouseup依次触发后相当于click事件 2.除了mouseenter和mouseleave外,其它的鼠标事件都是冒泡的 3.mouseover和mouseout事 ...

  8. WOW.js 的使用方法

    WOW.js 是一个非常轻量级的动画效果插件,使用它可以组合多种炫酷的效果. 使用WOW.js可以实现我们在网站上常看到的,页面滚动到指定区域时就显示动画的效果. 1.要使用WOW.js必须引入:WO ...

  9. ASP NET Core --- HTTP 翻页、过滤、排序

    参照 草根专栏- ASP.NET Core + Ng6 实战:https://v.qq.com/x/page/v07647j3zkq.html 翻页, 过滤, 排序等 – 如何传递参数? Query ...

  10. Django源码分析之server

    乍见 Django内置的server基本包括两部分:django.core.servers和django.core.handlers 相识 servers.basehttp是Django自身提供的一个 ...