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 /* ** 混合图欧拉回路 ** 只记录各定点的出度与入度之差,有向 ...
随机推荐
- redis 远程连接出错的解决办法
1. 配置防火墙端口 redis系统的默认端口是6379端口. # 打开端口 $ firewall-cmd --zone=public --add-port=6379/tcp --permanent ...
- 学习笔记TF025:自编码器
传统机器学习依赖良好的特征工程.深度学习解决有效特征难人工提取问题.无监督学习,不需要标注数据,学习数据内容组织形式,提取频繁出现特征,逐层抽象,从简单到复杂,从微观到宏观. 稀疏编码(Sparse ...
- addEventListener以及滑轮滑动事件的应用
addEventListener用于向元素添加事件,而其适用于较新版的IE浏览器(如IE9),对于IE6/7/8来说,应该用attachEvent 下面的代码即为向<img>元素添加事件 ...
- 我发起并创立了一个 .Net 平台上的 Web 业务系统 基础库 开源项目 WebEasy
我 强调一点, 程序员 应该对 程序 有 控制感 . 过多的 控制反转 使 程序员 丧失了 对 程序 的 控制感 . 过多的 依赖注入 束缚了 程序员 的 创造力 . 过度复杂的 架构设计 束缚了 程 ...
- C# 左右补零
//不够4位补零 public static string addZero(int val) { string str = val + ""; int strLen = str.L ...
- git中出现remote: HTTP Basic: Access denied
git中出现remote: HTTP Basic: Access denied 1.git clone时出现 Username for 'http://******': *** remote: HTT ...
- PHP-ML机器学习库之安装篇
1.PHP-ML库安装要求:PHP>=7.1 2.切换到项目的跟目录下,使用composer进行安装:composer require php-ai/php-ml 安装完成后的目录如下: 新建测 ...
- Custom Grid Columns - FireMonkey Guide
原文 http://monkeystyler.com/guide/Custom-Grid-Columns ack to FireMonkey Topics As we saw in TGrid a F ...
- Linux下安装MySQL----来自简书(挺好的)
来自简书的: https://www.jianshu.com/p/f4a98a905011 注: 1. 下载方式: 可以使用命令下载: wget http://downloads.mysql.com/ ...
- Keras处理已保存模型中的自定义层(或其他自定义对象)
如果要加载的模型包含自定义层或其他自定义类或函数,则可以通过 custom_objects 参数将它们传递给加载机制: from keras.models import load_model # 假设 ...