Sightseeing tour 【混合图欧拉回路】
题目链接:http://poj.org/problem?id=1637
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions:10837 | Accepted: 4560 |
Description
Input
Output
- #include<stdio.h>
- #include<string.h>
- #include<queue>
- #define mem(a, b) memset(a, b, sizeof(a))
- const int MAXN = ;
- const int MAXM = ;
- const int inf = 0x3f3f3f3f;
- using namespace std;
- int n, m, st, ed, tot; //n个点 m条边(有向 + 无向)
- int out[MAXN], in[MAXN], head[MAXN], cnt;
- queue<int> Q;
- int dep[MAXN];
- struct Edge
- {
- int to, next, flow;
- }edge[ * MAXM];
- void add(int a, int b, int c)
- {
- cnt ++;
- edge[cnt].to = b;
- edge[cnt].next = head[a];
- edge[cnt].flow = c;
- head[a] = cnt;
- }
- int build()
- {
- for(int i = ; i <= n; i ++)
- {
- if((out[i] + in[i]) % ) //如果存在有点的出入跟入度的奇偶性不同 那么无论如何调节都无法做到入度和出度相等
- return ;
- if(out[i] > in[i])
- {
- int x = (out[i] - in[i]) / ;
- tot += x;
- add(st, i, x);
- add(i, st, );
- }
- else if(in[i] > out[i])//入度与出度相等的情况 加不加边无影响
- {
- int x = (in[i] - out[i]) / ;
- add(i, ed, x);
- add(ed, i, );
- }
- }
- return ;
- }
- int bfs()
- {
- if(st == ed)
- return ;
- mem(dep, -);
- dep[st] = ;
- Q.push(st);
- while(!Q.empty())
- {
- int index = Q.front();
- Q.pop();
- for(int i = head[index]; i != -; i = edge[i].next)
- {
- int to = edge[i].to;
- if(edge[i].flow > && dep[to] == -)
- {
- dep[to] = dep[index] + ;
- Q.push(to);
- }
- }
- }
- return dep[ed] != -;
- }
- int dfs(int now, int zx)
- {
- if(now == ed)
- return zx;
- for(int i = head[now]; i != -; i = edge[i].next)
- {
- int to = edge[i].to;
- if(dep[to] == dep[now] + && edge[i].flow > )
- {
- int flow = dfs(to, min(zx, edge[i].flow));
- if(flow > )
- {
- edge[i].flow -= flow;
- edge[i ^ ].flow += flow;
- return flow;
- }
- }
- }
- return -;
- }
- int dinic()
- {
- int ans = ;
- while(bfs())
- {
- while()
- {
- int inc = dfs(st, inf);
- if(inc == -)
- break;
- ans += inc;
- }
- }
- return ans;
- }
- int main()
- {
- int T;
- scanf("%d", &T);
- while(T --)
- {
- mem(out, ), mem(in, ), mem(head, -);
- cnt = -, tot = ;
- scanf("%d%d", &n, &m);
- st = , ed = n + ;
- for(int i = ; i <= m; i ++)
- {
- int a, b, op;
- scanf("%d%d%d", &a, &b, &op);
- out[a] ++ ,in[b] ++;
- if(op == ) //只有无向边才能自调节
- {
- add(a, b, );
- add(b, a, );
- }
- }
- if(build())
- {
- int maxflow = dinic();
- if(maxflow == tot) //最大流等于源点出去的边满流
- printf("possible\n");
- else
- printf("impossible\n");
- }
- else
- printf("impossible\n");
- }
- return ;
- }
Sightseeing tour 【混合图欧拉回路】的更多相关文章
- POJ 1637 Sightseeing tour ★混合图欧拉回路
[题目大意]混合图欧拉回路(1 <= N <= 200, 1 <= M <= 1000) [建模方法] 把该图的无向边随便定向,计算每个点的入度和出度.如果有某个点出入度之差为 ...
- poj1637 Sightseeing tour(混合图欧拉回路)
题目链接 题意 给出一个混合图(有无向边,也有有向边),问能否通过确定无向边的方向,使得该图形成欧拉回路. 思路 这是一道混合图欧拉回路的模板题. 一张图要满足有欧拉回路,必须满足每个点的度数为偶数. ...
- POJ1637 Sightseeing tour (混合图欧拉回路)(网络流)
Sightseeing tour Time Limit: 1000MS Me ...
- poj 1637 Sightseeing tour 混合图欧拉回路 最大流 建图
题目链接 题意 给定一个混合图,里面既有有向边也有无向边.问该图中是否存在一条路径,经过每条边恰好一次. 思路 从欧拉回路说起 首先回顾有向图欧拉回路的充要条件:\(\forall v\in G, d ...
- POJ 1637 Sightseeing tour (混合图欧拉回路)
Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tou ...
- poj1637 Sightseeing tour 混合图欧拉回路判定
传送门 第一次做这种题, 尽管ac了但是完全不知道为什么这么做. 题目就是给一些边, 有向边与无向边混合, 问你是否存在欧拉回路. 做法是先对每个点求入度和出度, 如果一条边是无向边, 就随便指定一个 ...
- poj1637Sightseeing tour(混合图欧拉回路)
题目请戳这里 题目大意:求混合图欧拉回路. 题目分析:最大流.竟然用网络流求混合图的欧拉回路,涨姿势了啊啊.. 其实仔细一想也是那么回事.欧拉回路是遍历所有边一次又回到起点的回路.双向图只要每个点度数 ...
- POJ1637:Sightseeing tour(混合图的欧拉回路)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10581 Accepted: 4466 ...
- POJ 1637 Sightseeing tour(混合图的欧拉回路)
题目链接 建个图,套个模板. #include <cstdio> #include <cstring> #include <iostream> #include & ...
- POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]
嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...
随机推荐
- bzoj2725
* 给出一张图 * 每次删掉一条边后求 the shortest path from S to T * 线段树维护最短路径树 * 具体维护从某点开始偏离最短路而到达 T 的最小距离 * 首先记录下最短 ...
- [Luogu] U18430 萌萌的大河
https://www.luogu.org/problemnew/show/U18430 思路比较好想 树链剖分 对于1操作 只需将以该点为根的子树打标记,将所有数存入数组排序 每次进行1操作时,判断 ...
- Codeforces Round #572 (Div. 2)
目录 Contest Info Solutions A. Keanu Reeves B. Number Circle C. Candies! D1. Add on a Tree D2. Add on ...
- Ubuntu18.04安装和配置Django,并实现简单示例
一.前言(系统,django介绍,window.mac.linux简单区别) Django是python开发过程最重要的web框架.因为在看的Django教学视频是在mac下安装的,我自己用的是Lin ...
- Applications (ZOJ 3705)
题解:就是题目有点小长而已,可能会不想读题,但是题意蛮好理解的,就是根据条件模拟,计算pts.(送给队友zm. qsh,你们不适合训练了.) #include <iostream> #in ...
- sql文件导入老是失败
这是因为sql文件中的编码格式与库的格式不一致造成的,遇到这种问题先进入sql文件看看其编程格式是什么后再建立库的格式一般我们建立库都只选择utf-8下面的格式都没有选择
- sublime的一些记录
{ "keys": ["tab"], "command": "reindent", "context" ...
- 图解Linux进程间通信实现原理(1)
为Linux应用程序的开发人员,对Linux的进程间通信方式肯定是了如指掌,平时的开发中应该会大量的使用到.当你迅速的在键盘上按下[CTRL+C]终止掉一个正在运行中的命令时,你有没有仔细的思考过背后 ...
- linux安装puppeteer
1.安装 下载淘宝镜像的,可以同时下载puppeteer和chromium下面两条语句即可 npm install -g cnpm --registry=https://registry.npm.ta ...
- 冲刺阶段——Day1
[成员分工及任务量] 成员 分工 任务量(小时) 王梓鸿 完成页面设计并编写输入输出图形界面,部分代码测试 20 童皓祯 编写注册和登录模块代码,部分代码测试 20 林郅聪 绘制燃尽图,程序功能整合及 ...