SPOJ 962 Intergalactic Map (网络最大流)
http://www.spoj.com/problems/IM/
962. Intergalactic MapProblem code: IM |
Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padmé Amidala to save Naboofrom an invasion by
the Trade Federation. They must leave Naboo immediately and go to Tatooine to pick up the proof of the Federation’s evil design. They then must proceed on to the Republic’s capital planet Coruscant to produce it in front of
the Republic’s Senate. To help them in this endeavor, the queen’s captain provides them with an intergalactic map. This map shows connections between planets not yet blockaded by the Trade Federation. Any pair of planets has at most one connection between
them, and all the connections are two-way. To avoid detection by enemy spies, the knights must embark on this adventure without visiting any planet more than once. Can you help them by determining if such a path exists?
Note - In the attached map, the desired path is shown in bold.
Input Description
The first line of the input is a positive integer t ≤ 20, which is the number of test cases. The descriptions of the test cases follow one after the other. The first line of each test case is a pair of positive integers n, m (separated by a single space). 2
≤ n ≤ 30011 is the number of planets and m ≤ 50011 is the number of connections between planets. The planets are indexed with integers from 1 to n. The indices of Naboo, Tatooine and Coruscant are 1, 2, 3 respectively. The next m lines contain two integers
each, giving pairs of planets that have a connection between them.
Output Description
The output should contain t lines. The ith line corresponds to the ith test case. The output for each test case should be YES if the required path exists and NO otherwise.
Example
Input
2
3 3
1 2
2 3
1 3
3 1
1 3
Output
YES
NO
题意:
给出一张无向图,要求从1先走到2。再从2走到3,且每一个点至多经过一次,问是否可能。
分析:
每一个点至多经过一次,显然往网络流上靠,很明显的拆点。
可是要求从1走到2,再从2走到3,显然不太优点理。由于每一个点最多经过一次,所以从1走到2的路径与2走到3的路径显然是全然不同的两条路径。并且还是无向图,那么最好还是考虑从2出发找两条不同的路径分别走到1和3。这样建图就呼之欲出了:s->2,容量为2;1->t,3->t容量均为1,图中全部边容量均为1,在此图中跑最大流就可以。要注意的是输入中不在区间[1,n]内的点要扔掉。
/*
*
* Author : fcbruce <fcbruce8964@gmail.com>
*
* Time : Wed 19 Nov 2014 04:39:23 PM CST
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10 #ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif #define maxm 65555<<3
#define maxn 33333<<1 using namespace std; int n,m; int fir[maxn];
int u[maxm],v[maxm],cap[maxm],flow[maxm],nex[maxm];
int e_max; int lv[maxn],q[maxn],iter[maxn]; inline void add_edge(int s,int t,int c)
{
int &e=e_max;
u[e]=s;v[e]=t;cap[e]=c;
nex[e]=fir[u[e]];fir[u[e]]=e++;
u[e]=t;v[e]=s;cap[e]=0;
nex[e]=fir[u[e]];fir[u[e]]=e++;
} void dinic_bfs(int s)
{
int f,r;
memset(lv,-1,sizeof lv);
q[f=r=0]=s;
lv[s]=0; while (f<=r)
{
int x=q[f++];
for (int e=fir[x];~e;e=nex[e])
{
if (cap[e]>flow[e] && lv[v[e]]<0)
{
lv[v[e]]=lv[x]+1;
q[++r]=v[e];
}
}
}
} int dinic_dfs(int s,int t,int f)
{
if (s==t) return f;
for (int &e=iter[s];~e;e=nex[e])
{
if (cap[e]>flow[e] && lv[s]<lv[v[e]])
{
int d=dinic_dfs(v[e],t,min(f,cap[e]-flow[e]));
if (d>0)
{
flow[e]+=d;
flow[e^1]-=d;
return d;
}
}
}
return 0;
} int max_flow(int s,int t)
{
memset(flow,0,sizeof flow);
int total_flow=0;
for (;;)
{
dinic_bfs(s);
if (lv[t]<0) break; memcpy(iter,fir,sizeof fir); int f;
while ((f=dinic_dfs(s,t,INF))>0)
total_flow+=f;
} return total_flow;
} inline int in(int i)
{
return i;
} inline int out(int i)
{
return i+n;
} int main()
{
#ifdef FCBRUCE
freopen("/home/fcbruce/code/t","r",stdin);
#endif // FCBRUCE int T_T;
scanf("%d",&T_T); while (T_T--)
{
e_max=0;
memset(fir,-1,sizeof fir); scanf("%d%d",&n,&m); int s=0,t=n*2+2;
add_edge(s,out(2),2);
add_edge(in(1),t,1);
add_edge(in(3),t,1);
for (int i=4;i<=n;i++) add_edge(in(i),out(i),1);
for (int i=0,u,v;i<m;i++)
{
scanf("%d%d",&u,&v);
if (u<1 || u>n || v<1 || v>n) continue;
add_edge(out(u),in(v),1);
add_edge(out(v),in(u),1);
} if (max_flow(s,t)==2) puts("YES");
else puts("NO");
} return 0;
}
SPOJ 962 Intergalactic Map (网络最大流)的更多相关文章
- SPOJ 962 Intergalactic Map
Intergalactic Map Time Limit: 6000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Ori ...
- SPOJ 962 Intergalactic Map (从A到B再到C的路线)
[题意]在一个无向图中,一个人要从A点赶往B点,之后再赶往C点,且要求中途不能多次经过同一个点.问是否存在这样的路线.(3 <= N <= 30011, 1 <= M <= 5 ...
- SPOJ IM - Intergalactic Map - [拆点最大流]
题目链接:http://www.spoj.com/problems/IM/en/ Time limit:491 ms Memory limit:1572864 kB Code length Limit ...
- spoj 962 IM - Intergalactic Map【最大流】
因为是无向图,所以从1到2再到3等于从2到1和3.用拆点来限制流量(i,i+n,1),然后连接(s,2+n,1),(1,t,1),(3,t,1),对于原图中的边连接(x+n,y,1)(y+n,x,1) ...
- SPOJ 0962 Intergalactic Map
题目大意:在一个无向图中,一个人要从A点赶往B点,之后再赶往C点,且要求中途不能多次经过同一个点.问是否存在这样的路线.(3 <= N <= 30011, 1 <= M <= ...
- SPOJ962 Intergalactic Map(最大流)
题目问一张无向图能否从1点走到2点再走到3点,且一个点只走一次. 思维定势思维定势..建图关键在于,源点向2点连边,1点和3点向汇点连边! 另外,题目数据听说有点问题,出现点大于n的数据.. #inc ...
- [SPOJ962]Intergalactic Map 拆点+最大流
Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padmé Ami ...
- 图论算法-网络最大流【EK;Dinic】
图论算法-网络最大流模板[EK;Dinic] EK模板 每次找出增广后残量网络中的最小残量增加流量 const int inf=1e9; int n,m,s,t; struct node{int v, ...
- Map Reduce和流处理
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由@从流域到海域翻译,发表于腾讯云+社区 map()和reduce()是在集群式设备上用来做大规模数据处理的方法,用户定义一个特定的映射 ...
随机推荐
- 使用libpqxx访问PostgreSQL数据库(mingw编译libpqxx)
编译前准备 1. 安装mingw 安装mingw(不管是直接安装mingw还是其他如code::blocks附带安装的mingw),输入:gcc -v可显示如下图的版本信息,我的版本是mingw ...
- Oracle+struts2实现用户登入并显示访问次数
实体类: package entity; public class userfo { private int id;//id private String name;//用户名 private Str ...
- WinRT ListView间隔变色(二)
上文说到,WinRt中,我们不能在Style的Setter使用Binding.这个问题其实从SL5之前,一直都不可以.但是,为了使用强大的Binding,人们也一直想使用各种方法来达到Binding ...
- Python+selenium学习(一) 打开Firefox浏览器,IE浏览器和Chrome浏览器
from selenium import webdriver # open Firefox #driver=webdriver.Firefox() # Open IE #driver=webdrive ...
- Linux下ifconfig不显示ip地址问题总结
问题一:ifconfig之后只显示lo,没有看到eth0 ? eth0设置不正确,导致无法正常启动,修改eth0配置文件就好 ubuntu 12.04的网络设置文件是/etc/network/inte ...
- R语言学习 - 线图一步法
首先把测试数据存储到文件中方便调用.数据矩阵存储在line_data.xls和line_data_melt.xls文件中 (直接拷贝到文件中也可以,这里这么操作只是为了随文章提供个测试文件,方便使用. ...
- struts2源码下载链接
http://blog.csdn.net/qq_qun_247286682/article/details/6975298
- UVA - 1620 Lazy Susan(逆序数)
题目: 把1~n(n≤500)放到一个圆盘里,每个数恰好出现一次.每次可以选4个连续的数字翻转顺序.问能不能变成1.2.3....n的顺序. 思路: 这样的题的规律真的是一点都不好推,看了网上的博客知 ...
- java 日历计算农历和节假日的工具类
背景 业务需求需要后端提供这样的接口,网上找了很多java代码例子,虽然功能实现了 但是不完善,特别是节日那一块儿.然后百度发现有这样的插件,但是信息也是java后端提供的非js 然后在开源js插件找 ...
- hust 1017
题意:求01矩阵的精确覆盖. 分析:本来想学习dancing links来解决数独问题,发现dancing links最初解决的问题是精确覆盖,于是就找到这道题来做了.这种NPC问题只能用DFS暴搜的 ...