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 ...
随机推荐
- P4568 [JLOI2011]飞行路线 分层图最短路
思路:裸的分层图最短路 提交:1次 题解: 如思路 代码: #include<cstdio> #include<iostream> #include<cstring> ...
- learning express step(六)
code: use application middleware var express = require('express'); var app = express(); app.use(func ...
- fixed 失效
1. 父元素设置 transform 属性后, 会导致 position: fixed 失效. 2. 设置以下属性也会影响 fixed 属性. -webkit-perspective: 1000; - ...
- 牛客练习赛39 B.选点
链接:https://ac.nowcoder.com/acm/contest/368/B 来源:牛客网 题目描述 有一棵n个节点的二叉树,1为根节点,每个节点有一个值wi.现在要选出尽量多的点. 对于 ...
- [Luogu] 打砖块
题面:https://www.luogu.org/problemnew/show/P1174 题解:https://www.zybuluo.com/wsndy-xx/note/1145569
- redis系列(一):安装配置
1.下载安装 下载可到redis官网下载(可根据自己的操作系统下载,我的是Ubuntu) 目前redis最新版本是5.0.2:下载地址为:http://download.redis.io/releas ...
- 在做nios ii uart232 实验时出现undefined reference to `fclose'等错误。
程序如下 #include<stdio.h> #include<string.h> #include "system.h" int main () { ...
- JavaEE三大框架的整合
JavaEE三大框架的整合 ...
- Hive 参数
hive.exec.max.created.files •说明:所有hive运行的map与reduce任务可以产生的文件的和 •默认值:100000 hive.exec.dynamic.partit ...
- win10下linux子系统的文件夹的布局
我这里的目录为:C:\Users\com\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\Loca ...