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. rails小技巧之分组查询统计并去重

    分组查询并统计 SpecialGroup.group(:special_type).count select special_type,count(*) from special_groups gro ...

  2. RubyMine常用快捷键

    一级必会 Shift+F10:运行running Ctrl+Alt+R:弹出RakeCtrl+Alt+G:弹出GenerateCtrl+Alt+L:格式化代码Alt+F1:切换视图(Project, ...

  3. hack游戏攻略(梦之光芒黑客小游戏)

    2019.2.11 继续玩~~还是黑客游戏闯关类的 地址:http://monyer.com/game/game1/ 直接查看页面代码: first.php就是了: 查看源代码: 这里尝试输入 两个空 ...

  4. ffplay使用笔记

    ffplay播放yuv文件命令: ffplay -f rawvideo -video_size 1920x1080 a.yuv   ffplay播放mp4.h.264.hevc文件命令: ffplay ...

  5. 最小生成树算法 1.Prim算法

    最小生成树(MST):一个有N个点的图,边一定是大于等于N-1条边的.在这些边中选择N-1条出来,连接所有N个点.这N-1条边的边权之和是所有方案中最小的. Prim算法的时间复杂度时O(n^2)的, ...

  6. Hibernate-ORM:06.Hibernate中三种状态

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客主要叙述Hibernate中的三种状态:临时状态(瞬时状态),持久状态,游离状态 commit和flu ...

  7. 用es6写一个分数库

    es6发布后nodejs开始更新.最近写一些库发现新特性还是很好用的,于是回来写一个分数库练手. 对于es6本身 ... => 以及 array.includes 很简洁.class依然不是很顺 ...

  8. 「日常训练」 Soldier and Cards (CFR304D2C)

    题意 (Codeforces 546C) 按照指定的规则打牌,问谁胜或无穷尽. 分析 又是一条模拟,用set+queue(这里手写了)处理即可.注意到两种局势"1 234"和&qu ...

  9. ZooKeeper完全分布式安装与配置

    Apache ZooKeeper是一个为分布式应用所设计开源协调服务,其设计目是为了减轻分布式应用程序所承担的协调任务.可以为用户提供同步.配置管理.分组和命名服务. 1.环境说明 在三台装有cent ...

  10. Web负载均衡技术

    Web负载均衡(Load Balancing),简单地说就是给我们的服务器集群分配“工作任务”,而采用恰当的分配方式,对于保护处于后端的Web服务器来说,非常重要. 负载均衡的策略有很多,我们从简单的 ...