POJ - 1637点我点我:-)

Sightseeing tour
Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %lld & %llu

Submit Status

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

题意:给出一个n个节点,m条边的图,m条边的描述为0, 1,如果是1则是单向路,0是双向路,问是否存在欧拉回路

题解:这是混合欧拉图的判定,有一些不错的收获!

我们原来做的欧拉路判定只是单向的,现在加了一些双向的边,我们这么办:

1.判断出入度时,如果我们给双向边随意定一个方向,那我们就可以算出所有的点的出入度,我们发现不论是把双向边定义为哪个方向,都不会改变点的出入度之差(一个数量加一,一个数量减一)。如果有点的出入度之差为奇数,那么不存在欧拉回路(--->所有点的出入度之差为0)

          2.但是双向路的方向还不能确定,我们的最终目的是把所有点的出入度之差变为0,我们需要调整路的方向看最终是否可以达到条件,方法是网络流: 我们把所有双向边的点按所定的初始方向加边,然后把入度小于出度的点与总源点连边,大小为(出度-入度)/2,  把出度小于入度的点与总汇点连边,大小为(入度-出度)/2, 这表示每个点的需调整量,算算此网络流是否满流,满流说明可以将所有点的出入度之差变为0,否则不行。

poj 1637 代码如下:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>using namespace std;#define MAXN (200+5)
#define pb push_back
#define INF 0x3f3f3f3f
#define Set(a, v) memset(a, v, sizeof(a))
#define For(i, a, b) for(int i = (a); i <= (int)(b); i++)struct Edge{int from, to, cap, flow;
};int n, rm;struct Dinic{int m, s, t;vector<Edge> edges;vector<int> G[MAXN];bool vis[MAXN];int d[MAXN], cur[MAXN];void init(){edges.clear();For(i, 0, MAXN-1) G[i].clear();}void AddEdge(int u, int v, int w){edges.pb((Edge){u, v, w, 0}); edges.pb((Edge){v, u, 0, 0});m = edges.size();G[u].pb(m-2); G[v].pb(m-1);};bool Bfs(){Set(vis, 0);queue<int> q;q.push(s);d[s] = 0; vis[s] = true;while(!q.empty()){int now = q.front(); q.pop();For(i, 0, G[now].size()-1){Edge e = edges[G[now][i]];if(!vis[e.to] && e.cap > e.flow){vis[e.to] = true;d[e.to] = d[now]+1;q.push(e.to);}}}return vis[t];}int Dfs(int now, int a){if(now == t || !a) return a;int flow = 0, f;for(int& i = cur[now]; i < G[now].size(); i++){Edge &e = edges[G[now][i]];if(d[now]+1 == d[e.to] && (f = Dfs(e.to, min(a, e.cap-e.flow))) > 0){e.flow += f;edges[G[now][i]^1].flow -= f;flow += f; a -= f;if(!a) break;}}return flow;}int Max_flow(int s, int t){this->s = s; this->t = t;int flow = 0;while(Bfs()){Set(cur, 0);flow += Dfs(s, INF);}return flow;}
}Din;int in[MAXN], out[MAXN];int get_map(){int ret = 0;For(i, 1, n){if(in[i] < out[i]) Din.AddEdge(0, i, (out[i]-in[i])/2);else{Din.AddEdge(i, n+1, (in[i]-out[i])/2);ret += (in[i]-out[i])/2;}}return ret;
}void solve(){bool flag = true;For(i, 1, n)if((in[i]-out[i]) % 2){flag = false;break;}if(!flag) printf("impossible\n");else{int sum = get_map();if(Din.Max_flow(0, n+1) == sum) printf("possible\n");else printf("impossible\n");}
}int main(){int T;scanf("%d", &T);while(T--){Set(in, 0); Set(out, 0);Din.init();scanf("%d%d", &n, &rm);For(i, 1, rm){int u, v, w;scanf("%d%d%d", &u, &v, &w);in[v]++; out[u]++;if(!w) Din.AddEdge(u, v, 1);}solve();}return 0;
}

poj 1637 Sightseeing tour 混合欧拉图判定相关推荐

  1. POJ 1637 Sightseeing tour 混合图欧拉回路存在性判断

    没有想到网络流还能解决这一类问题,完全想不到@_@ 一开始把所有的无向边制定任意方向有当做有向边看,然后统计每个点的入度和出度.以前有向图的欧拉回路判定是每个点的入读都等于出度,这样可以保证可以回到起 ...

  2. poj 1637 Sightseeing tour 混合欧拉 最大流

    #include <cstdio> #include <vector> #include <queue> #include <cstring> usin ...

  3. POJ 1637 Sightseeing tour(最大流)

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

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

    题目链接:https://vjudge.net/contest/399194#problem/B The city executive board in Lund wants to construct ...

  5. poj 1637 Sightseeing tour

    http://poj.org/problem?id=1637 题意: 给出一张混合图,判断是否存在欧拉回路 原理: 1.存在欧拉回路的充要条件:所有点入度=出度 2.给无向边随便定向不会影响点的|出度 ...

  6. poj 1637 Sightseeing tour——最大流+欧拉回路

    题目:http://poj.org/problem?id=1637 先给无向边随便定向,如果一个点的入度大于出度,就从源点向它连 ( 入度 - 出度 / 2 ) 容量的边,意为需要流出去这么多:流出去 ...

  7. TZOJ 2099 Sightseeing tour(网络流判混合图欧拉回路)

    描述 The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that to ...

  8. POJ 1637 混合图的欧拉回路 + Dinic

    题意 传送门 POJ 1637 题解 有向图存在欧拉回路的充要条件: 所有顶点入度等于出度,且图为连通图. 混合图存在欧拉回路的判断的基本思路: 混合图 G(V,E)G(V,E)G(V,E) 通过假设 ...

  9. POJ 1637 混合图的欧拉回路判定

    题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定: 一开始当然是判断原图的基图是否连通,若不连通则一 ...

最新文章

  1. 云从科技完成B+轮超10亿元融资,多个国家基金进入
  2. [业界资讯]Window7下的IE8新漏洞KB973874成功修复
  3. 虚拟机VMware里 windows server 2003 扩充C盘方法
  4. python pow和**_第005篇:Python中的数字
  5. ❤️缓存集合(一级缓存、二级缓存、缓存原理以及自定义缓存—源码+图文分析,建议收藏) ❤️
  6. FFMPEG中最关键的结构体之间的关系
  7. java java se_Java SE 9:尝试资源改进
  8. chown: `mysql#039;: invalid user_centos无法正常启动,报chown: invalid user:'root:root'
  9. CTF_BUGKU_WEB_game1
  10. 基于大数据的个性化推荐系统
  11. 关于海康相机ip地址无法更改问题
  12. 关系数据库——关系代数
  13. 本地开发H5页面如何发版成为微信公众号?
  14. android启用hdcp_如何在Android Auto上启用开发人员设置
  15. PCB_焊盘工艺设计规范
  16. K8S调试工具之--nsenter
  17. 判断输入的日期是一年的第几天或者星期几
  18. Android多语言设置
  19. 几何校正(image to image)
  20. vue屏幕长宽自适应

热门文章

  1. 年后跳槽全过程总结(上)——从面试准备到拿到offer
  2. 轻松禁用WinRAR设置
  3. 2017cad光标大小怎么调_如何更改CAD光标大小及颜色?
  4. 无缘无故,Oralce使用normal模式登录用户失败
  5. mysql增加数据 条件,mysql根据条件决定是否插入数据
  6. Maven 父文件的依赖 子文件不能接收_maven报错:Non-resolvable parent POM for com...
  7. (干货)电源方案合集
  8. pmp中ram和raci的区别_【PMP考前冲刺】知识点大全(四)
  9. PMP(第六版)中的各种矩阵表格
  10. Xu_Learning_to_Restore_Low-Light_Images_via_Decomposition-and-Enhancement_CVPR_2020_paper