题目链接:https://vjudge.net/contest/399194#problem/B

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

翻译:
能否建造一个旅游系统:城市的每条街道都能准确地参观一次,开始和结束在同一个路口。
输入
t:几组样例
m和s(1 <= m <= 200,1 <= s <= 1000 )
路口数和街道数

s条道路: xi, yi di(0 <= di <= 1)
di=1:xi—>yi
di=0:双向道路(无向边)
输出:“possible” or “impossible”,是否能建造一个观光旅游

分析:
把该图的无向边随便定向(从x–>y或从y–>x),计算每个点的入度和出度。
如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。

现在每个点入度和出度之差均为偶数。那么将这个偶数除以 2,得 x。
也就是说,对于每一个点,只要将 x 条边改变方向(入>出就是变入,出>入就是变出),就能保证出=入。
我该改变哪些边,可以让每个点出=入?
1. 有向边是不能改变方向的,要之无用,删
2. 无向边已经定向,边的容量为1

新建 s 和t。对于入>出的点 u,连接边(u, t)、容量为 x,对于出>入的点 v,连接边(s, u),容量为x。
察看是否有满流的分配。有就是能有欧拉回路,没有就是没有

邻接矩阵+EK

#include<cstdio>
#include<cstring>
#include<math.h>
#include<queue>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int N=2*1e2+10;
const int M=1e3+10;
const int INF=0x3f3f3f3f;
int m,s;
int ind[N];///记录入度,出度
int capacity[N][N],flow[N];
int pre[N];
bool book[N];
int bfs(int source,int sink)
{memset(pre,-1,sizeof(pre));memset(book,false,sizeof(book));queue<int>Q;Q.push(source);book[source]=true;pre[source]=0;flow[source]=INF;while(!Q.empty()){int u=Q.front();Q.pop();if(u==sink)break;for(int i=0; i<=m+1; i++){if(!book[i]&&capacity[u][i]>0){pre[i]=u;book[i]=true;flow[i]=min(flow[u],capacity[u][i]);Q.push(i);}}}if(pre[sink]!=-1)return flow[sink];elsereturn -1;
}
int max_flow(int source,int sink)
{memset(flow,0,sizeof(flow));int ans=0;for(;;){int k=bfs(source,sink);if(k==-1)return ans;ans+=k;for(int i=sink; i!=source; i=pre[i]){capacity[pre[i]][i]-=k;capacity[i][pre[i]]+=k;}}
}
int main()
{int t;scanf("%d",&t);while(t--){memset(ind,0,sizeof(ind));memset(capacity,0,sizeof(capacity));scanf("%d%d",&m,&s);///m个结点 s条边while(s--){int x,y,d;scanf("%d%d%d",&x,&y,&d);ind[x]--,ind[y]++;///出度--,入度++if(!d)///无向边capacity[x][y]+=1;}bool ck=true;for(int i=1; i<=m; i++){if(ind[i]&1)///欧拉图{ck=false;break;}}if(!ck)printf("impossible\n");else{int sum=0;for(int i=1; i<=m; i++){if(ind[i]<0)///出>入capacity[0][i]=(-ind[i])>>1;if(ind[i]>0)//入>出{capacity[i][m+1]=ind[i]>>1;sum+=(ind[i]>>1);}}int mxx_flow=max_flow(0,m+1);if(mxx_flow==sum)printf("possible\n");elseprintf("impossible\n");}}return 0;
}

邻接表+dinic

#include<cstdio>
#include<cstring>
#include<math.h>
#include<queue>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int N=2*1e2+10;
const int M=2*1e5+10;
const int INF=0x3f3f3f3f;
int m,s;
struct node
{int nex;int to,w;
} side[M];
int fir[N],tot;///邻接表建图
int ind[N];///记录入度,出度
int level[N];///记录每一个点的层次
int cut[M];
void Inx(int x,int y,int z)
{side[++tot].to=y,side[tot].w=z;side[tot].nex=fir[x];fir[x]=tot;
}int bfs()
{memset(level,-1,sizeof(level));queue<int>Q;level[0]=1;Q.push(0);while(!Q.empty()){int u=Q.front();if(u==m+1)break;Q.pop();for(int i=fir[u]; ~i; i=side[i].nex){if(side[i].w&&level[side[i].to]<0){level[side[i].to]=level[u]+1;///构建层次网络Q.push(side[i].to);}}}return level[m+1]!=-1;
}
int dfs(int u,int low)
{if(u==m+1)return low;int temp=0,a;for(int &i=cut[u]; ~i; i=side[i].nex)///i的改变带着fir[u]的改变,提高效率,所以用cut[]数组保留原来的fir[u]{if(side[i].w&&level[side[i].to]==level[u]+1&&(a=dfs(side[i].to,min(low,side[i].w)))){temp+=a;side[i].w-=a;side[i^1].w+=a;low-=a;if(!low)break;}}if(temp==0)level[u]=-1;///走到u不能走return temp;
}
int dinic()
{int ans=0;while(bfs())///找到一条增广路{for(int i=0; i<=m+1; i++)cut[i]=fir[i];ans+=dfs(0,INF);}return ans;
}
int main()
{int t;scanf("%d",&t);while(t--){tot=-1;memset(ind,0,sizeof(ind));memset(fir,-1,sizeof(fir));scanf("%d%d",&m,&s);while(s--){int x,y,d;scanf("%d%d%d",&x,&y,&d);if(x==y)continue;ind[x]--,ind[y]++;///出度--,入度++if(!d)///无向边{Inx(x,y,1);///把无向边随便规定一个方向Inx(y,x,0);}}bool bo=true;for(int i=1; i<=m; i++){if(ind[i]&1)///欧拉图{bo=false;break;}}if(!bo)printf("impossible\n");else{int sum=0;for(int i=1; i<=m; i++){if(ind[i]<0)///出>入{Inx(0,i,(-ind[i])>>1);Inx(i,0,0);}if(ind[i]>0)//入>出{Inx(i,m+1,ind[i]>>1);Inx(m+1,i,0);sum+=(ind[i]>>1);}}int mxx_flow=dinic();if(mxx_flow==sum)printf("possible\n");elseprintf("impossible\n");}}return 0;
}

POJ - 1637 Sightseeing tour(混合图欧拉回路的求解--建图跑最大流)相关推荐

  1. poj 1637 Sightseeing tour 混合欧拉图判定

    POJ - 1637点我点我:-) Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %ll ...

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

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

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

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

  4. POJ 1637 Sightseeing tour(最大流)

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

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

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

  6. poj 1637 Sightseeing tour

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

  7. walking机器人入门教程-视觉建图-rtabmap使用视觉建图和导航

    系列文章目录 walking机器人入门教程-目录 walking机器人入门教程-硬件清单 walking机器人入门教程-软件清单 walking机器人入门教程-测试底盘 walking机器人入门教程- ...

  8. 【POJ - 1275】Cashier Employment(差分约束,建图)

    题干: A supermarket in Tehran is open 24 hours a day every day and needs a number of cashiers to fit i ...

  9. codevs1024一塔湖图(丧心病狂的建图)

    /* 丧心病狂的最短路 关键是建图 根据题目中给的路 拆出节点来 建图 (i,j) -->(j-1)*n+i 然后根据障碍 把死路 湖覆盖的dis改变成极大值 然后Floyd 然后 然后就没有然 ...

最新文章

  1. nacos1.0.0 服务管理使用mysql
  2. Android SharedPreferences存储map的方法
  3. 洛谷P1337 [JSOI2004]平衡点 / 吊打XXX(模拟退火)
  4. python列表下表_Python 列表下标操作
  5. 3.mysql的中文问题,database级操作,表级操作,数据CRUD,分组操作,时间和日期,字符串相关函数,表的约束
  6. 将矩阵转为一行_LeetCode 力扣官方题解 | 861. 翻转矩阵后的得分
  7. google的api key调用次数是多少_Sprint Boot如何基于Redis发布订阅实现异步消息系统的同步调用?...
  8. linux 下运行libnids,libnids出错
  9. php 前端模板 yii,php – Yii2高级模板:添加独立网页
  10. c# mysql 操作_c#对mysql数据库的基本操作
  11. 正能量:为web前端发开者代言
  12. Linux日常运维管理技巧(四)文件同步工具-rsync、Linux系统日志、dmesg命令、lastb命令查看登录失败的用户、screen工具虚拟屏幕
  13. [转载]:C# 中结构与类的区别
  14. QT-C++ Nesting排料优化,广告,服装,木工排料(支持矩形、异形排版,提高优化效率)
  15. 宏基4736ZG更换键盘图解
  16. JAR文件概述(2021版)
  17. Python if else elif
  18. 关于关系型数据库锁机制的理解
  19. python绘图颜色代码_Python绘图的颜色设置
  20. FPGA实现贪吃蛇小游戏

热门文章

  1. PaddleX---Mask RCNN实例分割
  2. UITT 自动跟单系统
  3. 乐理分析笔记(一) 巴赫《平均律钢琴曲集》BWV.846 序曲
  4. python爬去新浪微博_!如何通过python调用新浪微博的API来爬取数据
  5. stopstart按钮怎么用_汽车Start-Stop启停技术简明讲解
  6. 如何能正常获取17track物流网站的物流信息?
  7. oracle数据库中小数小于1时0不显示
  8. linux删除文件面面观
  9. 免Root获取WIFI密码
  10. 北京大学可视化发展前沿研究生暑期学校第二讲