http://www.spoj.com/problems/IM/

962. Intergalactic Map

Problem 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 (网络最大流)的更多相关文章

  1. SPOJ 962 Intergalactic Map

    Intergalactic Map Time Limit: 6000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Ori ...

  2. SPOJ 962 Intergalactic Map (从A到B再到C的路线)

    [题意]在一个无向图中,一个人要从A点赶往B点,之后再赶往C点,且要求中途不能多次经过同一个点.问是否存在这样的路线.(3 <= N <= 30011, 1 <= M <= 5 ...

  3. SPOJ IM - Intergalactic Map - [拆点最大流]

    题目链接:http://www.spoj.com/problems/IM/en/ Time limit:491 ms Memory limit:1572864 kB Code length Limit ...

  4. 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) ...

  5. SPOJ 0962 Intergalactic Map

    题目大意:在一个无向图中,一个人要从A点赶往B点,之后再赶往C点,且要求中途不能多次经过同一个点.问是否存在这样的路线.(3 <= N <= 30011, 1 <= M <= ...

  6. SPOJ962 Intergalactic Map(最大流)

    题目问一张无向图能否从1点走到2点再走到3点,且一个点只走一次. 思维定势思维定势..建图关键在于,源点向2点连边,1点和3点向汇点连边! 另外,题目数据听说有点问题,出现点大于n的数据.. #inc ...

  7. [SPOJ962]Intergalactic Map 拆点+最大流

    Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padmé Ami ...

  8. 图论算法-网络最大流【EK;Dinic】

    图论算法-网络最大流模板[EK;Dinic] EK模板 每次找出增广后残量网络中的最小残量增加流量 const int inf=1e9; int n,m,s,t; struct node{int v, ...

  9. Map Reduce和流处理

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由@从流域到海域翻译,发表于腾讯云+社区 map()和reduce()是在集群式设备上用来做大规模数据处理的方法,用户定义一个特定的映射 ...

随机推荐

  1. C++11:using 的各种作用

    C++11中using关键字的主要作用是:为一个模板库定义一个别名. 文章链接:派生类中使用using别名改变基类成员的访问权限  一.<Effective Modern C++>里有比较 ...

  2. 场景分割:MIT Scene Parsing 与DilatedNet 扩展卷积网络

    MIT Scene Parsing Benchmark简介 Scene parsing is to segment and parse an image into different image re ...

  3. @objc and dynamic

      @objc and dynamic Objective-C runtime visibility and the depths of dynamic dispatch in the modern ...

  4. 梦想CAD控件安卓选择集

    在本示例中将使用构造选择集对被过滤对象进行过滤,该类封装了选择集及其处理函数,支持如下过滤条件. 参数类型 类型 RTDXF0 TEXT 文字 MTEXT 多行文字 CIRCLE 圆 ARC 圆弧 L ...

  5. 梦想CAD控件系统变量说明

    这里介绍一些常用系统变量有String.double.long.McGePoint3d等类型,其中有部分系统变量是随图纸保存,再次打开时就会读取图纸中的系统变量,有些系统变量不随图纸保存,其作用来控制 ...

  6. ubuntu 16.04 添加网卡

    root@ubuntu:~# ls /sys/class/net/ enp0s3 enp0s8 lo root@ubuntu:~# vim /etc/network/interfaces # This ...

  7. 【转载】appium自动化环境搭建

    1.java开发环境JDK 2.android SDK(platform/platform tools/tools/build tools) 3.python下载安装(pip) 4.appium下载安 ...

  8. python bs4库

    Beautiful Soup parses anything you give it, and does the tree traversal stuff for you. BeautifulSoup ...

  9. Tomcat 使用redis实现session共享

    准备工作: 1.安装nginx 环境搭建参考:https://blog.csdn.net/fd2025/article/details/79878326 nginx.conf的编辑: 2.同一台机器配 ...

  10. 题解 洛谷P1501/BZOJ2631【[国家集训队]Tree II】

    Link-Cut-Tree 的懒标记下传正确食用方法. 我们来逐步分析每一个操作. 1:+ u v c:将u到v的路径上的点的权值都加上自然数c; 解决方法: 很显然,我们可以 split(u,v) ...