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. Codeforces_758_D_(区间dp)

    D. Ability To Convert time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. Xamarin View获取属性的绑定信息

    public static Binding GetBinding( BindableObject self, BindableProperty property) { var methodInfo = ...

  3. 使用lombok注解@Getter @Setter方法代码编译成功,但是没有生成get,set方法

    现象描述: 在对应类对象中,添加lombok的@Getter或@Setter注解,编译没有问题,但是在使用类对象时,没有出现对应的get或set方法. 配置且编译ok,但是没有对应的get或set方法 ...

  4. 'dict' object is not callable

    今天学py的map函数时,由于在上面定义了一个dict类型的变量(取的名是map),所以编译后报了这么一个错,哎,以后学py命名要小心了

  5. mysql导出数据到excel的两种方式

    使用第一种方式如果数据中有换行符的话会自动换行,但使用第二种方式就不会出现这种效果了.两种方式自己选择哈 1:select * from into outfile 'c:/Users/a.xls' t ...

  6. React-native SyntaxError: Unexpected token ...

    更新 node.js 版本到  v6.11.1. https://github.com/facebook/react-native/issues/15040

  7. c/c++编程排坑(1)-- 数据类型的“安静”转换

    这里主要介绍ANSI C的特性:当执行算术运算时,操作数的类型如果不同,就会发生转换.数据类型一般朝着精度更高.长度更长的方向转换,整型数如果转换为signed不会丢失信息,就转换为signed,否则 ...

  8. Luogu P1349 广义斐波那契数列

    解题思路 既然广义斐波那契,而且数据范围这么大,那么我们使用矩阵快速幂来进行求解.大家都知道斐波那契的初始矩阵如下 $$\begin{bmatrix}1&1\\1&0\end{bmat ...

  9. Openssl生成RSA公私钥以及将公钥转换成C#支持的格式

    Openssl生成RSA公私钥以及将公钥转换成C#支持的格式 1.RSA算法介绍 RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密.RSA ...

  10. web视频播放插件:Video For Everybody

    相比其它的web视频播放插件(video.js , jwplayer等)来说,Video For Everybody(极力推荐)是一款更好的视频播放插件,无需任何下载,支持html5以及flash播放 ...