描述

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(网络流判混合图欧拉回路)的更多相关文章

  1. POJ1637 Sightseeing tour(判定混合图欧拉回路)

    有向连通图存在欧拉回路的充要条件是所有点入度=出度. 首先随便给定所有无向边一个方向(不妨直接是u->v方向),记录所有点的度(记:度=入度-出度). 这时如果有点的度不等于0,那么就不存在欧拉 ...

  2. POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]

    嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...

  3. Sightseeing tour 【混合图欧拉回路】

    题目链接:http://poj.org/problem?id=1637 Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total ...

  4. [POJ1637]Sightseeing tour:混合图欧拉回路

    分析 混合图欧拉回路问题. 一个有向图有欧拉回路当且仅当图连通并且对于每个点,入度\(=\)出度. 入度和出度相等可以联想到(我也不知道是怎么联想到的)网络流除了源汇点均满足入流\(=\)出流.于是可 ...

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

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

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

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

  7. POJ 1637 Sightseeing tour(混合图欧拉回路+最大流)

    http://poj.org/problem?id=1637 题意:给出n个点和m条边,这些边有些是单向边,有些是双向边,判断是否能构成欧拉回路. 思路: 构成有向图欧拉回路的要求是入度=出度,无向图 ...

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

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

  9. 混合图欧拉回路POJ1637Sightseeing tour

    http://www.cnblogs.com/looker_acm/archive/2010/08/15/1799919.html /* ** 混合图欧拉回路 ** 只记录各定点的出度与入度之差,有向 ...

随机推荐

  1. hsdfz -- 6.17 -- day2

    今日依旧康复…… 当天晚上被老师拉去小吃街了,晚上回来精力憔悴,所以并没有当天写 反正就惨,因为估错复杂度,期望得分100分最后结果20分 (我的复杂度是nlog^2n的,正确性有保障,稳! 事后:还 ...

  2. xpython在Windos下的安装及简单的文本打开、保存

    前几天写自动化部署脚本,用的是paramiko和shell相结合,paramiko可是实现ssh登录,文件及文件夹的上传下载,这些功能,然后一直想自己写个东西出来,于是就想把这些功能我把他放到图形化界 ...

  3. lavarel 中间件

    创建中间件 php artisan make:policy UserPolicy 所有生成的授权策略文件都会被放置在 app/Policies 文件夹下. 让我们为默认生成的用户授权策略添加 upda ...

  4. Struts1 不能进入Action,页面空白问题解决

    http://blog.csdn.net/barry_liao/article/details/35592935 Struts1 不能进入Action,页面空白问题解决 标签: Struts1Acti ...

  5. Android批量验证渠道、版本号(windows版)

    功能:可校验单个或目录下所有apk文件的渠道号.版本号,此为windows版,稍后整理Linux版使用说明:1.copy需要校验的apk文件到VerifyChannelVersion目录下2.双击运行 ...

  6. Spark 性能调优-内存设置-GC设置

    http://mt.sohu.com/20150604/n414449770.shtml http://my.oschina.net/mkh/blog/330386 http://itindex.ne ...

  7. Spring Boot之执行器端点(Actuator Endpoint)实现剖析

    整体实现思路是将端点(Endpoint)适配委托给MVC层策略端点(MvcEndpoint),再通过端点MVC适配器(EndpointMvcAdapter)将端点暴露为HTTP请求方式的MVC端点,最 ...

  8. 手动卸载CAD 删除残留文件 清理遗留的文件

    手动卸载基于 AutoCAD 产品的文件,从而删除所有残留文件. 清理安装失败所遗留的文件. 一.解决方案: 通过"控制面板"卸载该程序. 删除以下位置残留的 AutoCAD 文件 ...

  9. delphi combobox屏蔽鼠标滑动

    //第1种方法 procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; Mo ...

  10. import 搜索路径

    来源 http://www.runoob.com/python/python-mysql.html 搜索路径 当你导入一个模块,Python 解析器对模块位置的搜索顺序是: 1.当前目录 2.如果不在 ...