POJ1637_Sightseeing tour
给一个联通图,有的是单向边,有的是双向边,问是否存在欧拉回路。
乍一看毫无思路,可以这样来搞,对于每条无向边,我们随便指定一个方向,看看是否能够做到所有点的度数之和为偶数。
接下来,对于我们指定的边,假设指定的是U->V,那么我们也同时在网络中设置一条同样的边,使得流量为1,最后如果某点的出入度只差不为0,那么我们把那个差除以2,这表示我们在这个点处至少需要改变多少条无向边的方向。对于出大于入的点,我们连源点,对于入大于出的点,我们连汇点,最后跑最大流看看所有的与源点和汇点的边能否满流即可。
一开始的想法是,不指定方向,直接跑最大流,这样是错的,因为无法保证每条无向边最终都被指定了方向,也就是对应回题目里面,不一定无向边都遍历到了,而通过首先指定一个方向,然后再改变方向的方法可以保证这一点。
召唤代码君:
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 2220
#define maxm 22220
using namespace std; int to[maxm],next[maxm],first[maxn],c[maxm],edge;
int d[maxn],tag[maxn],can[maxn],dgr[maxn],TAG=;
int n,m,T,s,t,sum,ans;
int Q[maxn],bot,top;
const int inf=~0U>>; void _init()
{
sum=ans=s=,t=n+,edge=-;
for (int i=s; i<=t; i++) first[i]=-,dgr[i]=;
} bool check()
{
for (int i=; i<=n; i++)
if (dgr[i]&) return false;
return true;
} void addedge(int U,int V,int W)
{
edge++;
to[edge]=V,c[edge]=W,next[edge]=first[U],first[U]=edge;
edge++;
to[edge]=U,c[edge]=,next[edge]=first[V],first[V]=edge;
} bool bfs()
{
Q[bot=top=]=t,d[t]=,tag[t]=++TAG;
while (bot<=top)
{
int cur=Q[bot++];
for (int i=first[cur]; i!=-; i=next[i])
if (c[i^]> && tag[to[i]]!=TAG)
{
tag[to[i]]=TAG,d[to[i]]=d[cur]+;
can[to[i]]=false,Q[++top]=to[i];
if (to[i]==s) return true;
}
}
return false;
} int dfs(int cur,int num)
{
if (cur==t) return num;
int tmp=num,k;
for (int i=first[cur]; i!=-; i=next[i])
if (c[i]> && tag[to[i]]==TAG && d[to[i]]==d[cur]- && !can[to[i]])
{
k=dfs(to[i],min(num,c[i]));
if (k) num-=k,c[i]-=k,c[i^]+=k;
if (num==) break;
}
if (num>) can[cur]=true;
return tmp-num;
} int main()
{
int U,V,W;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
_init();
while (m--)
{
scanf("%d%d%d",&U,&V,&W);
dgr[U]--,dgr[V]++;
if (W==) addedge(U,V,);
}
if (!check())
{
puts("impossible");
continue;
}
for (int i=; i<=n; i++)
{
if (dgr[i]==) continue;
if (dgr[i]<) addedge(s,i,-dgr[i]/);
else
{
sum+=dgr[i]/;
addedge(i,t,dgr[i]/);
}
}
while (bfs()) ans+=dfs(s,inf);
if (ans==sum) puts("possible");
else puts("impossible");
}
return ;
}
POJ1637_Sightseeing tour的更多相关文章
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
- Euler Tour Tree与dynamic connectivity
Euler Tour Tree最大的优点就是可以方便的维护子树信息,这点LCT是做不到的.为什么要维护子树信息呢..?我们可以用来做fully dynamic connectivity(online) ...
- POJ2677 Tour[DP 状态规定]
Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4307 Accepted: 1894 Description ...
- soj 1015 Jill's Tour Paths 解题报告
题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...
- poj1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8859 Accepted: 3728 ...
- A quick tour of JSON libraries in Scala
A quick tour of JSON libraries in Scala Update (18.11.2015): added spray-json-shapeless libraryUpdat ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- POJ 1637 Sightseeing tour (混合图欧拉回路)
Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tou ...
- POJ2135 Farm Tour
Farm Tour Time Limit: 2MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description ...
随机推荐
- Redis架构之防雪崩设计:网站不宕机背后的兵法
互联网系统中不可避免要大量用到缓存,在缓存的使用过程中,架构师需要注意哪些问题?本文以 Redis 为例,详细探讨了最关键的 3 个问题. 一.缓存穿透预防及优化 缓存穿透是指查询一个根本不存在的数据 ...
- Oracle安装到Maven本地仓库
1.由于Maven的特性,并且之前的IDE环境已帮我们集成了Maven.而现在我们需要手动安装MVN本地仓库到电脑. 将mvn绿色安装包bin路径配置到系统环境变量Path中 验证命令: mvn –v ...
- Flink架构分析之Standalone模式启动流程
概述 FLIP6 对Flink架构进行了改进,引入了Dispatcher组件集成了所有任务共享的一些组件:SubmittedJobGraphStore,LibraryCacheManager等,为了保 ...
- Docker 入门之docker容器创建
使用docker容器的大多数人都是因为想要隔离不同运行环境的差异,使得自己的应用能更好的移植和部署.那么我们来看看掌握docker需要掌握哪些方面. 1,搭建docker环境 2,编译镜像并将其运行成 ...
- php从入门到放弃系列-04.php页面间值传递和保持
php从入门到放弃系列-04.php页面间值传递和保持 一.目录结构 二.两次页面间传递值 在两次页面之间传递少量数据,可以使用get提交,也可以使用post提交,二者的区别恕不赘述. 1.get提交 ...
- Node.js开发入门—套接字(socket)编程
Node.js的net模块提供了socket编程接口,方便我们利用较为底层的套接字接口来实现应用协议.这次我们看一个简单的回显服务器示例,包括服务端和客户端的代码. 代码 分服务器和客户端两部分来说吧 ...
- Python20-Day06
常用模块 一.random模块 import random # print(random.random()) #打印0-1之间的小数 # print(random.randint(1,3)) #大于等 ...
- jsweb常用代码
<script> $(function (){ $.ajax({ url: 'https://test.com:8080/api/v1/users?query_not_auth=100&a ...
- Linux 下Web环境搭建————redis
1.安装编译工具(yum -y install make gcc gcc-c++ ncurses-devel)2 2.安装tcl依赖 yum -y install tcl 3.上传redis安装包并解 ...
- 初涉JSP+JDBC 基于SQL2008的登陆验证程序
简单的以代码的形式纪念一下,因为现在还没有解决SQL2008驱动的问题,并且有好多东西要学,所以日后会有更新~ 所安装的软件有:SQL2008,eclipse,tomcat,JDK,涉及环境配置.等等 ...