POJ1637 Sightseeing tour 混合图的欧拉回路+最大流

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情
混合图的欧拉回路判断方法在黑书上面有具体提到。
 
这里采用的方法:
先给无向边定向,让后统计每一个点的出入度,如果有一个点的出入度只差为奇数,则该图不存在欧拉回路(有向图的欧拉回路每个点的度数都是偶数,至于出度=入度,在求最大流时我们会进行调整)。
全部判断完后,开始建图。
1:把每一条无向边建成一条容量为1的弧。
2:出入度之差不为0的点:
如果出>入 则将改点和源点相连 容量为出入度之差/2。
如果入>出 则将点和汇点相连,容量为出入度之差/2。
完成后求一个最大流。如果是满流则存在偶来回路
Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5792 Accepted: 2357

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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>

using namespace std;

#define MAXN 255
#define INF 0xFFFFFF

struct edge
{
	int to,c,next;
};

struct edge2
{
	int u,v,ty;
};

edge e[99999];
edge2 ee[99999];
int deg[MAXN]; 
int que[MAXN*100],dis[MAXN],pre[MAXN];
int head[MAXN],head2[MAXN];
int st,ed,maxflow;
int en,n,m;

void add(int a,int b,int c)
{
	e[en].to=b;
	e[en].c=c;
	e[en].next=head[a];
	head[a]=en++;
	e[en].to=a;
	e[en].c=0;
	e[en].next=head[b];
	head[b]=en++;	
} 

bool bfs() 
{
	memset(dis,-1,sizeof(dis));
	que[0]=st,dis[st]=1;
	int t=1,f=0;
	while(f<t)
	{
		int j=que[f++];
		for(int k=head[j];k!=-1;k=e[k].next)
		{	
			int i=e[k].to;
			if(dis[i]==-1 && e[k].c)
			{
				que[t++]=i;
				dis[i]=dis[j]+1;
				if(i==ed) return true;
			}
		}
	}
	return false; 
} 

int update()  
{  
	int p,flow=INF;  
    for (int i=pre[ed];i!=-1;i=pre[i])
		if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c;    
    for (int i=pre[ed];i!=-1;i=pre[i]) 
		e[head2[i]].c-=flow,e[head2[i]^1].c+=flow;   
    maxflow+=flow; 
    return p;
}  

void dfs() 
{  
	memset(pre,-1,sizeof(pre));
	memcpy(head2,head,sizeof(head2));  
    for(int i=st,j;i!=-1;)  
    {  
        int flag=false;  
        for(int k=head[i];k!=-1;k=e[k].next)    
          if(e[k].c && (dis[j=e[k].to]==dis[i]+1) )  
          {  
                pre[j]=i; 
				head2[i]=k;  
				i=j; 
				flag=true;  
                if(i==ed) 
					i=update();  
                if(flag) 
					break;  
          }  
        if (!flag) dis[i]=-1,i=pre[i];   
    }  
} 

int build()
{
	int temp=0;
	memset(head,-1,sizeof(head));
	for(int i=1;i<=n;i++)
	{
		if(deg[i])
		{
			if(deg[i]>0)
				add(i,ed,deg[i]);
			else
			{
				add(st,i,abs(deg[i]));
				temp+=deg[i];
			}
		}
	}
	for(int i=1;i<=m;i++)
		if(!ee[i].ty) add(ee[i].u,ee[i].v,1);
	return temp;
}

void solve()
{
	int a,b,c;
	scanf("%d%d",&n,&m);
	memset(deg,0,sizeof(deg));
	st=0,ed=1+n;
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		deg[a]--;
		deg[b]++;
		ee[i].u=a;
		ee[i].v=b;
		ee[i].ty=c;
		
	}
	for(int i=1;i<=n;i++)
	{
		if(deg[i]%2!=0)
		{
			printf("impossible\n");
			return;
		}
		deg[i]/=2;
	}
	int fuflow=abs(build());
	maxflow=0;
	while(bfs()) dfs();
	if(maxflow==fuflow)
		printf("possible\n");
	else
		printf("impossible\n");
	
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--) solve();
	return 0;
} 

POJ 1637 Sightseeing tour (混合图欧拉回路,网络最大流) http://poj.org/problem?id=1637 Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7498   Accepted: 3123 Description The city execu 阅读详情

相关推荐

POJ 1637 Sightseeing tour 混合图欧拉回路 最大流

题目大意:给出一张混合图,问是否存在欧拉回路。  思路:成题,直接看题解吧。 http://www.cnblogs.com/Lyush/archive/2013/05/01/3052847.html CODE: #include #include #include #include #include #define MAX 510 #define MAXE

(╯°口°)╯(┴—┴ 800

最大流+混合图欧拉回路POJ-1637 Sightseeing tour

Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K       Description The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so tha

JSure的代码库 844

POJ1637Sightseeing tour最大流】【混合图欧拉回路

【题目链接】 混合图欧拉回路。 论文题,见【网络流建模汇总】 注意入度大于出度必须连接汇点,出度大于入度必须源点去连。不能相反。 /* Pigonometry */ #include #include using namespace std; const int maxn = 205, maxm = 5005, maxq = 10000, inf = 0x3f3f3

BraketBN 489

poj1637Sightseeing tour 混合图欧拉回路最大流

DescriptionThe 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 ev

DQS的树洞 620

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

题目链接 题意 给定一个混合图,里面既有有向边也有无向边。问该图中是否存在一条路径,经过每条边恰好一次。 思路 从欧拉回路说起 首先回顾有向图欧拉回路的充要条件:\(\forall v\in G, d_{in}(v)=d_{out}(v)\). 现在这个图中有一些无向边,那怎么办? 那就转化成有向边呀。 对无向边随意定向,得到一个有向图。在这个有向图中,如果有\(\forall v\in...

ahu12345678的博客 192

POJ - 1637 Sightseeing tour 混合图欧拉回路的判断(最大流

传送门:POJ1637 题意:给出一个混合图,判断是否存在欧拉回路。 用网络流判断混合图中是否存在欧拉回路方法及证明: 【混合图混合图(既有有向边又有无向边的图)中欧拉环、欧拉路径的判定需要借助网络流! (1)欧拉环的判定: 一开始当然是判断原图的基图是否连通,若不连通则一定不存在欧拉环或欧拉路径(不考虑度数为0的点)。 其实,难点在于图中的无向边,需要对所有的无

WA是一笔财富 310

poj1637 Sightseeing tour混合图欧拉回路最大流

判断混合图是否存在欧拉回路。首先有关欧拉回路的一些定义: 如果图G中的一个路径包括每个边恰好一次,则该路径称为欧拉路径(Euler path)。 如果一个回路欧拉路径,则称为欧拉回路(Euler circuit)。 具有欧拉回路的图称为欧拉图(简称E图)。具有欧拉路径但不具有欧拉回路的图称为半欧拉图。有关欧拉回路的一些性质: 以下判断基于此图的基图连通。 无向图存在欧拉回路的充要条件:

Icefox的博客 557

poj1637 Sightseeing tour混合图欧拉回路问题,最大流

混合图欧拉回路问题 题目地址 欧拉回路问题 1 定义 欧拉通路 (Euler tour)——通过图中每条边一次且仅一次,并且过每一顶点的通路。  欧拉回路 (Euler  circuit)——通过图中每条边一次且仅一次,并且过每一顶点的回路。  欧拉图——存在欧拉回路的图。   2 无向图是否具有欧拉通路或回路的判定  G有欧拉通路的充分必要条件为:G 连通,

天亮说晚安 1918

POJ 1637Sightseeing tour 混合图欧拉回路 最大流

<br />题意就是求混合图欧拉回路是否存在。<br />题中说明了是连通图,并且任意一点都可由其它点达到。所以,不必再判断连通性了,直接最大流就OK.<br /> <br />看了看黑书,没看懂,看了看网上的解释,依然没看懂。。<br />后来,只好自己想啊想,想啊想,终于给A了。。<br /> <br />我是先把每个边都当成向边判断是否存在奇度顶点,如果存在肯定不存在欧拉回路了。<br />同时用一个数组d2只统计有向边获取每个点的有向的度(出度记为正,入度记为负)。<br />然后,建立出如下的图

palqing的专栏 775

POJ - 1637 Sightseeing tour混合图欧拉回路的求解--建图跑最大流

题目链接:https://vjudge.net/contest/399194#problem/B 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

zaiyang遇见 406

POJ 1637 Sightseeing tour混合图欧拉回路+最大流

http://poj.org/problem?id=1637 题意:给出n个点和m条边,这些边有些是单向边,有些是双向边,判断是否能构成欧拉回路。 思路: 构成有向图欧拉回路的要求是入度=出度,无向图的要求是所有顶点的度数为偶数。 但不管是那个,顶点的度数若是奇数,那都是不能构成的。 这道题目是非常典型的混合图欧拉回路问题,对于双向边,我们先随便定个向,然后就这样先记录好每个顶点的...

weixin_30340745的博客 130

POJ 1637 - Sightseeing tour 判断混合图是否是欧拉回路(最大流)

题意:                             给一个图..有些边是有向的...有些边是无向的...问能否有一条路径..从某点出发..又回到该点..并且所有的边经过exaclty一次...                   题解                             这题就是判断混合图欧拉回路的裸题....解法是先任意将每个无向边定向..然后来调整...看

zzyzzy12 907

解题报告 之 POJ1637 Sightseeing tour

解题报告 之 POJ1637 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 vis

JUST CODE 883

poj1637Sightseeing tour(混合图欧拉回路)

题目请戳这里 题目大意:求混合图欧拉回路。 题目分析:最大流。竟然用网络流求混合图欧拉回路,涨姿势了啊啊。。 其实仔细一想也是那么回事。欧拉回路是遍历所有边一次又回到起点的回路。双向图只要每个点度数为偶数即可,有向图要保证所有点入度等于出度。求路径的话,dfs即可。 混合图的话,就比较复杂。首先将有向边定向,求出所有点的入度和出度,如果某个点入度和出度之差为奇数,则一定不存在欧拉回路,因

ophunter的专栏 2024

poj 1637 Sightseeing tour(混合图欧拉回路)

混合图判断欧拉回路需要转换为网络流问题,首先把无向边当作有向边,然后对于每个点,如果存在点的入度和出度之差为奇数的话,就肯定不存在欧拉回路。对于都是偶数的情况,我们需要对于入度大于出度的点i,我们需要加一条源点到点i的边,容量为出入度差值的一半(因为只需要改变一半边的方向就可以使得点i满足入度等于出度),对于出度大于入度的边,需要加一条点i到汇点的边,容量为出入度差值的一半。然后对于原来的无向边,

ok_again的专栏 1286

[POJ 1637]Sightseeing tour[混合图欧拉回路]

题目链接:[POJ 1637]Sightseeing tour[混合图欧拉回路] 题意分析: 在一个有着单向边和双向边的图中,问:是否存在欧拉回路?(题目保证图连通) 解题思路: 欧拉回路的特点是:图中的所有点入度等于出度。然而这题多了个无向边。 我们可以考虑把无向边当成:能够随意变向的有向边。初始的时候,随意给无向边一个方向。 所以我们得到了一个弱化的初始判断条件:当某个点出度-入度

闲停 633

POJ 1637 Sightseeing tour 混合图欧拉回路问题

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...

xiji333的博客 251

Sightseeing tour混合图欧拉回路

题目链接:http://poj.org/problem?id=1637 Sightseeing tour Time Limit:1000MS Memory Limit:10000K Total Submissions:10837 Accepted:4560 Description The city executive...

a6334167的博客 190

POJ1637Sightseeing tour 混合图欧拉回路存在性 网络流、

题意:多组数据,最后的0/1表示0无向1有向。 问是否存在欧拉回路。 题解:无向边给它任意定个向。 首先欧拉回路中点入度=出度。 然后发现每个无向边如果修改个方向,原来的入点的入度+1,出度-1,出点反之。 然后我们不妨对入度和出度不同的点跟源汇中之一连边,容量为入出度差一半(每改一条边差-2) 然后原来的无向边联系图中各点,容量1,最后check if(maxflow

辗转山河弋流歌 1403
上一篇: POJ3436 ACM Computer Factory 拆点+网络最大流
下一篇: POJ1815 Friendship 枚举+拆点+最小割
Albafica
博客等级 码龄15年 98粉丝 359原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值