TZOJ 2099 Sightseeing tour(网络流判混合图欧拉回路)
描述
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.
输入
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.
输出
For each scenario, output one line containing the text "possible" or "impossible", whether or not it's possible to construct a sightseeing tour.
样例输入
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
样例输出
possible
impossible
impossible
possible
题意
m个点,s条边,问是否存在欧拉回路
题解
网络流判混合路欧拉回路
关键是把图变成有向图再判断
容易知道如果是欧拉回路那么所有点的入度in,等于出度out
可以发现无向边(u,v),有向边(u,v)或是有向边(v,u),in[i]-out[i]的奇偶性不变
那我们就可以先假定无向边(u,v)变成有向边(u,v)
统计所有点的入出度
如果存在i,使得abs(in[i]-out[i])%2==1那么图不存在欧拉回路
对于in[i]>out[i]的点,说明i点需要多流出流量,建边(S,i)流量(in[i]-out[i])/2
对于in[i]<out[i]的点,说明i点需要多流入流量,建边(i,T)流量(out[i]-in[i])/2
对于所有无向边(u,v),建边(u,v)流量1
跑S->T的最大流,若满流即存在一种方法通过改变无向边的方向使得每个点的入度=出度(是不是类似于上下界可行流是否有解问题)
代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn=1e5+;
const int maxm=2e5+;
const int INF=0x3f3f3f3f; int TO[maxm],CAP[maxm],NEXT[maxm],tote;
int FIR[maxn],gap[maxn],cur[maxn],d[maxn],q[];
int n,m,S,T; void add(int u,int v,int cap)
{
//printf("i=%d %d %d %d\n",tote,u,v,cap);
TO[tote]=v;
CAP[tote]=cap;
NEXT[tote]=FIR[u];
FIR[u]=tote++; TO[tote]=u;
CAP[tote]=;
NEXT[tote]=FIR[v];
FIR[v]=tote++;
}
void bfs()
{
memset(gap,,sizeof gap);
memset(d,,sizeof d);
++gap[d[T]=];
for(int i=;i<=n;++i)cur[i]=FIR[i];
int head=,tail=;
q[]=T;
while(head<=tail)
{
int u=q[head++];
for(int v=FIR[u];v!=-;v=NEXT[v])
if(!d[TO[v]])
++gap[d[TO[v]]=d[u]+],q[++tail]=TO[v];
}
}
int dfs(int u,int fl)
{
if(u==T)return fl;
int flow=;
for(int &v=cur[u];v!=-;v=NEXT[v])
if(CAP[v]&&d[u]==d[TO[v]]+)
{
int Min=dfs(TO[v],min(fl,CAP[v]));
flow+=Min,fl-=Min,CAP[v]-=Min,CAP[v^]+=Min;
if(!fl)return flow;
}
if(!(--gap[d[u]]))d[S]=n+;
++gap[++d[u]],cur[u]=FIR[u];
return flow;
}
int ISAP()
{
bfs();
int ret=;
while(d[S]<=n)ret+=dfs(S,INF);
return ret;
}
void init()
{
tote=;
memset(FIR,-,sizeof FIR);
}
int main()
{
int N,u,v,op,s,_;
scanf("%d",&_);
while(_--)
{
int in[]={},out[]={};
init();
scanf("%d%d",&N,&m);
S=N+,T=S+,n=T;
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&op);
in[v]++,out[u]++;
if(op==)
add(u,v,);
}
int flag=;
for(int i=;i<=N;i++)
if((out[i]-in[i])%==)
{
flag=;
break;
}
if(!flag)
{
printf("impossible\n");
continue;
}
int sum=;
for(int i=;i<=N;i++)
{
if(in[i]>out[i])
{
sum+=(in[i]-out[i])/;
add(i,T,(in[i]-out[i])/);
}
else if(out[i]>in[i])
add(S,i,(out[i]-in[i])/);
}
printf("%s\n",ISAP()==sum?"possible":"impossible");
}
return ;
}
TZOJ 2099 Sightseeing tour(网络流判混合图欧拉回路)的更多相关文章
- POJ1637 Sightseeing tour(判定混合图欧拉回路)
有向连通图存在欧拉回路的充要条件是所有点入度=出度. 首先随便给定所有无向边一个方向(不妨直接是u->v方向),记录所有点的度(记:度=入度-出度). 这时如果有点的度不等于0,那么就不存在欧拉 ...
- POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]
嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...
- Sightseeing tour 【混合图欧拉回路】
题目链接:http://poj.org/problem?id=1637 Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total ...
- [POJ1637]Sightseeing tour:混合图欧拉回路
分析 混合图欧拉回路问题. 一个有向图有欧拉回路当且仅当图连通并且对于每个点,入度\(=\)出度. 入度和出度相等可以联想到(我也不知道是怎么联想到的)网络流除了源汇点均满足入流\(=\)出流.于是可 ...
- POJ 1637 Sightseeing tour ★混合图欧拉回路
[题目大意]混合图欧拉回路(1 <= N <= 200, 1 <= M <= 1000) [建模方法] 把该图的无向边随便定向,计算每个点的入度和出度.如果有某个点出入度之差为 ...
- poj1637 Sightseeing tour(混合图欧拉回路)
题目链接 题意 给出一个混合图(有无向边,也有有向边),问能否通过确定无向边的方向,使得该图形成欧拉回路. 思路 这是一道混合图欧拉回路的模板题. 一张图要满足有欧拉回路,必须满足每个点的度数为偶数. ...
- POJ 1637 Sightseeing tour(混合图欧拉回路+最大流)
http://poj.org/problem?id=1637 题意:给出n个点和m条边,这些边有些是单向边,有些是双向边,判断是否能构成欧拉回路. 思路: 构成有向图欧拉回路的要求是入度=出度,无向图 ...
- poj1637Sightseeing tour(混合图欧拉回路)
题目请戳这里 题目大意:求混合图欧拉回路. 题目分析:最大流.竟然用网络流求混合图的欧拉回路,涨姿势了啊啊.. 其实仔细一想也是那么回事.欧拉回路是遍历所有边一次又回到起点的回路.双向图只要每个点度数 ...
- 混合图欧拉回路POJ1637Sightseeing tour
http://www.cnblogs.com/looker_acm/archive/2010/08/15/1799919.html /* ** 混合图欧拉回路 ** 只记录各定点的出度与入度之差,有向 ...
随机推荐
- NumPy 基础用法
NumPy 是高性能科学计算和数据分析的基础包. 它是 pandas 等其他各种工具的基础. 主要功能: ndarray 一个多维数组结构, 高效且节省空间 无需循环对整组数据进行快速运算的数学函数 ...
- gitlab部署步骤+汉化
系统环境centos7 下载gitlab安装包 https://packages.gitlab.com/gitlab/gitlab-ce 我下载的版本是 gitlab-ce-11.1.4-ce.0.e ...
- Linux系统时钟的更改
linux系统时钟有两个,一个是硬件时钟,即BIOS时间,就是我们进行CMOS设置时看到的时间,另一个是系统时钟,是linux系统Kernel时间. 查看.设置硬件时间: 查看系统硬件时钟 hwclo ...
- ionic3 热更新发布步骤记录
1.安装基本框架npm install -g ionic@latest npm install -g cordova ionic 验证版本号 ionic –version cordova -versi ...
- asp.net验证码
asp.net 生成验证码问题 .添加一个.ashx文件 <%@ WebHandler Language="C#" class="CheckCode" % ...
- C++Primer第五版——习题答案详解(二)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...
- python之路——7
王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 1. 小数据池 int -5---256 str 特殊字符 *202. ASCII码 8位 1字节 - ...
- ZooKeeper和CAP理论及一致性原则
一.CAP理论概述CAP理论告诉我们,一个分布式系统不可能同时满足以下三种 一致性(C:Consistency)可用性(A:Available)分区容错性(P:Partition Tolerance) ...
- Java访问Phoenix连接
两种方法,一种是直接使用jdbc连接,一种是使用spring连接. jdbc连接和访问oracle步骤相同: ///////////// 测试Phoenix连接 /////////////// Str ...
- eclipse常用快捷键整理
Ctrl + F11 按上次方式执行 Ctrl + Shift + / 加上注释/**/ Ctrl + Shift + \ 取消注释/**/ Ctrl + / 加上或消除行注释 Ctrl + D 删除 ...