YZOJ P2872 [POJ 1637]Sightseeing tour

YZOJ P2872 [POJ 1637]Sightseeing tour

Time Limit:1000MS      Memory Limit:131072KB

Difficulty: \(6.0\)

  • 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 \leq m \leq 200\), \(1 \leq s \leq 1000\) being the number of junctions and streets, respectively. The following s lines contain the streets. Each street is described with three integers, \(x_i\), \(y_i\), and \(d_i\), \(1 \leq x_i,y_i \leq m\), \(0 \leq d_i \leq 1\), where \(x_i\) and \(y_i\) are the junctions connected by a street. If \(d_i=1\), then the street is a one-way street (going from \(x_i\) to \(y_i\)), 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

  • Sample Output

 

 

 

Source: POJ 1637


 

 

 

混合图欧拉回路。

首先把该图的无向边任意定向,计算每个点的入度和出度。

如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点 \(din=dout\), 也就是总度数为偶数,存在奇数度点必不能有欧拉回路。

所以,每个点入度和出度之差均为偶数,设 \(x=\frac{\left| din-dout \right|}{2}\) 。

也就是说,对于每一个点,只要将 \(x\) 条边改变方向(\(din>dout\) 就是把入边变向,\(dout>din\) 就是把出边变向),就能保证 \(din=dout\) 。如果每个点都是 \(din=dout\),那么该图就是欧拉回路。

问题转化为,改变一些边,可以让每个点 \(din=dout\) 。

首先,有向边不能改变方向,没有建出来的必要。

因为 \(din, dout\) 是把无向边定向后算出来的,所以图中应有这一条定向后的边,容量为 \(1\) 。

新建 \(S,T\) :对于 \(din>dout\) 的点 \(u\),连 \(u \rightarrow T\) 容量为 \(x_u\) 的边;对于 \(dout>din\) 的点 \(v\) ,连 \(S \rightarrow v\) 容量为 \(x_v\) 的边。

然后跑最大流,满流则可以存在欧拉回路。

没有连接 \(S,T\) 的边容量为 \(1\) ,有流量表示这条边反向;与 \(S, T\) 相连的边表示有容量为 \(x\) 条边需要反向,判断满流即可。

https://wenku.baidu.com/view/0ad00abec77da26925c5b01c.html

 

 

 

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注