把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点入度 = 出度,也就是总度数为偶数,存在奇数度点必不能有欧拉回路;

好了,现在每个点入度和出度之差均为偶数。那么将这个偶数除以2,得x。也就是说,对于每一个点,只要将x条边改变方向(入>出就是变入,出>入就是变出),就能保证出=入。如果每个点都是出=入,那么很明显,该图就存在欧拉回路。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int M=;
const int inf=0x3f3f3f3f;
inline int read(){
int sum=,x=;
char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')
x=;
ch=getchar();
}
while(ch>=''&&ch<='')
sum=(sum<<)+(sum<<)+(ch^),ch=getchar();
return x?sum:-sum;
}
inline void write(int x){
if(x<)
putchar('-'),x=-x;
if(x>)
write(x/);
putchar(x%+'');
}
struct node{
int v,w,nextt;
}e[];
int deep[M],cur[M],head[M],du[M],tot,s,t;
bool bfs(){
for(int i=;i<=t;i++)
deep[i]=;
queue<int>que;
que.push(s);
deep[s]=;
while(!que.empty()){
int u=que.front();
que.pop();
for(int i=head[u];~i;i=e[i].nextt){
int v=e[i].v;
if(e[i].w>&&deep[v]==){
deep[v]=deep[u]+;
if(v==t){
return true;
}
que.push(v);
}
}
}
return deep[t]==?false:true;
}
int dfs(int u,int fl){
if(u==t){
return fl;
}
int x,ans=;
for(int i=cur[u];~i;i=e[i].nextt){
int v=e[i].v;
if(e[i].w>&&deep[v]==deep[u]+){
x=dfs(v,min(fl-ans,e[i].w));
e[i].w-=x;
e[i^].w+=x;
if(e[i].w)
cur[u]=i;
ans+=x;
if(ans==fl)
return ans;
}
}
if(ans==)
deep[u]=;
return ans;
}
int dinic(){
int ans=;
while(bfs()){
//cout<<ans<<endl;
for(int i=;i<=t;i++)
cur[i]=head[i];
ans+=dfs(s,inf);
}
return ans;
}
void addedge(int u,int v,int w){
e[tot].v=v;
e[tot].w=w;
e[tot].nextt=head[u];
head[u]=tot++;
e[tot].v=u;
e[tot].w=;
e[tot].nextt=head[v];
head[v]=tot++;
}
void init(){
for(int i=;i<=t;i++)
head[i]=-,du[i]=;
tot=;
}
int main(){
int p=read(); while(p--){
int n=read(),m=read();
s=,t=n+;
init();
while(m--){
int u=read(),v=read(),op=read();
du[u]++,du[v]--;
if(!op)
addedge(u,v,);
}
//jud
int flag=;
for(int i=;i<=n;i++){
if(du[i]&){
flag=;
break;
}
}
if(flag){
puts("impossible");
continue;
}
int sum=;
for(int i=;i<=n;i++){
if(du[i]<)
addedge(i,t,-du[i]/);
else if(du[i]>)
addedge(s,i,du[i]/),sum+=du[i]/;
}
if(sum!=dinic())
puts("impossible");
else
puts("possible");
}
return ;
}

混合欧拉回路poj 1637 Sightseeing tour的更多相关文章

  1. POJ 1637 Sightseeing tour(最大流)

    POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...

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

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

  3. POJ 1637 Sightseeing tour (混合图欧拉回路)

    Sightseeing tour   Description The city executive board in Lund wants to construct a sightseeing tou ...

  4. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

  5. POJ 1637 Sightseeing tour (SAP | Dinic 混合欧拉图的判断)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6448   Accepted: 2654 ...

  6. POJ 1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9276   Accepted: 3924 ...

  7. 网络流(最大流) POJ 1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8628   Accepted: 3636 ...

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

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

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

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

随机推荐

  1. GIT 操作文档

    https://git-scm.com/book/en/v2 安装git地址:https://git-scm.com/downloads 一.初始化设置 1.设置你用户名称与邮件地址(每一个 Git ...

  2. html+css web storage课上笔记 2019.3.18

    存储 cookie cookie 使用文本来存储信息 使用时服务器发送cookie给客户端,下一次时,浏览器发送给服务器 web storage local storage 本地的硬件设备中,关闭后不 ...

  3. 1-4 无监督学习(Unsupervised Learning)

    无监督学习定义: [无监督学习]中没有任何的标签或者是有相同的标签或者就是没标签.所以我们已知数据集,却不知如何处理,也未告知每个数据点是什么.别的都不知道,就是一个数据集.你能从数据中找到某种结构吗 ...

  4. JaveSE--getResource

    System.out.println(ConfigUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath()); ...

  5. Java数据的存储

    在JAVA中,有六个不同的地方可以存储数据: 1. 寄存器(register).这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据需求进 ...

  6. Linux不进入网卡配置文件更改静态ip

    1.找到网卡配置文件名ls /etc/sysconfig/network-scripts/ 2.备份并查看原始配置文件(若原先有配置IP的,则按照第五点方式修改) 3.修改随机自启和IP地址echo ...

  7. JIT Debug Info 简介

    原总结debug调试dump转储文件JITprocdumpJIT Debugging 前言 在上一篇介绍 JIT Debugging 的文章 -- 你需要了解的JIT Debugging 中,我们了解 ...

  8. ORB-SLAM2的编译运行以及TUM数据集测试

    ORB-SLAM2的编译运行以及TUM数据集测试 徐大徐 2018.02.06 17:04 字数 1838 阅读 2167评论 0喜欢 2 近段时间一直在学习高翔博士的<视觉SLAM十四讲> ...

  9. tensorflow实现卷积层的几种方式

    #coding:utf-8 #第一种实现 tf.nn import tensorflow as tf import tensorflow.contrib.slim as slim tf.reset_d ...

  10. [Scoi2014]方伯伯的OJ(动态开点splay)

    开始没看数据范围差点以为是这题了:https://www.cnblogs.com/hfctf0210/p/10911340.html 然后看到n<=1e8,怎么这么大? 所以这题需要用动态开点线 ...