POJ 1637 Sightseeing tour (混合图欧拉回路)
Sightseeing tour
Description
Input
Output
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
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f,MAXN=;
int in[MAXN],out[MAXN],m,s;
struct Edge
{
int from,to,cap,flow;
Edge(){}
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){};
};
struct dinic
{
int s,t;
vector<Edge>edges;
vector<int>G[MAXN];
bool vis[MAXN];
int d[MAXN];
int cur[MAXN];
void init()
{
for(int i=;i<=m+;i++)G[i].clear();
edges.clear();
memset(in,,sizeof(in));
memset(out,,sizeof(out));
}
void addedge(int from,int to,int cap)
{
edges.push_back((Edge){from,to,cap,});
edges.push_back((Edge){to,from,,});
int m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool bfs()
{
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=;
vis[s]=;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=;
d[e.to]=d[x]+;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t||a==)return a;
int flow=,f;
for(int& i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>)
{
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==)break;
}
}
return flow;
}
int maxflow(int s,int t)
{
this->s=s,this->t=t;
int flow=;
while(bfs())
{
memset(cur,,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
}dc;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
dc.init();
scanf("%d%d",&m,&s);
for(int i=;i<s;i++)
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
out[u]++,in[v]++;
if(d==)dc.addedge(u,v,);
}
bool ok=;
int sum=;
for(int i=;i<=m;i++)
if((in[i]-out[i])%==)
{
ok=;
break;
}
if(!ok){puts("impossible");continue;}
for(int i=;i<=m;i++)
{
if(in[i]<out[i])
dc.addedge(,i,(out[i]-in[i])/);
else if(in[i]>out[i])
{
dc.addedge(i,m+,(in[i]-out[i])/);
sum+=(in[i]-out[i])/;
}
}
puts(sum==dc.maxflow(,m+)?"possible":"impossible");
}
return ;
}
POJ 1637 Sightseeing tour (混合图欧拉回路)的更多相关文章
- POJ 1637 Sightseeing tour ★混合图欧拉回路
[题目大意]混合图欧拉回路(1 <= N <= 200, 1 <= M <= 1000) [建模方法] 把该图的无向边随便定向,计算每个点的入度和出度.如果有某个点出入度之差为 ...
- poj 1637 Sightseeing tour 混合图欧拉回路 最大流 建图
题目链接 题意 给定一个混合图,里面既有有向边也有无向边.问该图中是否存在一条路径,经过每条边恰好一次. 思路 从欧拉回路说起 首先回顾有向图欧拉回路的充要条件:\(\forall v\in G, d ...
- POJ 1637 Sightseeing tour(混合图的欧拉回路)
题目链接 建个图,套个模板. #include <cstdio> #include <cstring> #include <iostream> #include & ...
- poj1637 Sightseeing tour(混合图欧拉回路)
题目链接 题意 给出一个混合图(有无向边,也有有向边),问能否通过确定无向边的方向,使得该图形成欧拉回路. 思路 这是一道混合图欧拉回路的模板题. 一张图要满足有欧拉回路,必须满足每个点的度数为偶数. ...
- POJ1637 Sightseeing tour (混合图欧拉回路)(网络流)
Sightseeing tour Time Limit: 1000MS Me ...
- POJ 1637 Sightseeing tour 建图+网络流
题意: 给定一个混合图,所谓混合图就是图中既有单向边也有双向边,现在求这样的图是否存在欧拉回路. 分析: 存在欧拉回路的有向图,必须满足[入度==出度],现在,有些边已经被定向,所以我们直接记录度数即 ...
- poj 1637 Sightseeing tour —— 最大流+欧拉回路
题目:http://poj.org/problem?id=1637 建图很妙: 先给无向边随便定向,这样会有一些点的入度不等于出度: 如果入度和出度的差值不是偶数,也就是说这个点的总度数是奇数,那么一 ...
- poj1637 Sightseeing tour 混合图欧拉回路判定
传送门 第一次做这种题, 尽管ac了但是完全不知道为什么这么做. 题目就是给一些边, 有向边与无向边混合, 问你是否存在欧拉回路. 做法是先对每个点求入度和出度, 如果一条边是无向边, 就随便指定一个 ...
- poj 1637 Sightseeing tour——最大流+欧拉回路
题目:http://poj.org/problem?id=1637 先给无向边随便定向,如果一个点的入度大于出度,就从源点向它连 ( 入度 - 出度 / 2 ) 容量的边,意为需要流出去这么多:流出去 ...
随机推荐
- MapReduce的输入输出
mapper和reducer的划分 mapper的数量等于输入文件被划分成的分块数,这取决于输入文件的大小以及文件块的大小.一个map操作只处理一个输入分片.运行作业的客户端通过调用getSplits ...
- nginx相关配置说明
基础: nginx配置文件主要分为六个区域:main section.events section.http section.sever section. location section.upstr ...
- [solr] - IKAnalyzer 分词加入
1.下载IK Analyzer中文分词器:http://ik-analyzer.googlecode.com/files/IK%20Analyzer%202012FF_hf1.zip 2.解压出zip ...
- Joint Deep Learning for Pedestrian Detection笔记
1.结构图 Introduction Feature extraction, deformation handling, occlusion handling, and classification ...
- JQuery Placeholder - Input提示信息
JQuery Placeholder Placeholder属性是HTML5为Input添加的,在Input上提供一个占位符,文字形式展示输入文字预期值的提示信息. 如: 需要使用:placehold ...
- silverlight简单数据绑定2
<Grid x:Name="LayoutRoot" Background="white" Loaded="LayoutRoot_Loaded&q ...
- AR
http://jingyan.baidu.com/article/6766299727dcfc54d41b8455.html 1.注册.然后下载sdk(注册账号主要是为了第3步中制作识别图而用的) 下 ...
- Django base view
class django.views.generic.base.View 它是基类的基类,其它View基类都是从这继承的. 官例: from django.http import HttpRespon ...
- Python从题目中学习:range()和xrange()
近期给公司培训Python,好好啃了啃书本,查了查资料,总结一些知识点. --------------------------------------------------------------- ...
- Slider.js轻量级图片播放控件
Slider.js基于HTML5和CSS3实现的Slideshow 1.Slider.js 是一个图片播放Slideshow引擎,采用jQuery.CSS3和HTML5 canvas技术实现. 2.可 ...