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. WinRT ListView间隔变色(二)

    上文说到,WinRt中,我们不能在Style的Setter使用Binding.这个问题其实从SL5之前,一直都不可以.但是,为了使用强大的Binding,人们也一直想使用各种方法来达到Binding ...

  2. 第二节:Css重写样式

    一丶 进入浏览器---->F12----->找到要修改的区域的Style 进行重写Css样式 二丶打开新页面 window.open("/Persitent/OtherIndex ...

  3. 如何使用crash分析vmcore - 之基础思路case1

    如何使用crash分析vmcore - 之基础思路case1 dmesg查看内核日志 [2493382.671020] systemd-shutdown[1]: Sending SIGKILL to ...

  4. MySQL基础:show命令总结

    show命令 show命令可以提供关于数据库.表.列,或关于服务器的状态信息. 总结 # 显示二进制文件以及文件大小(需要开启二进制日志记录功能) SHOW {BINARY | MASTER} LOG ...

  5. C++ Primer(第4版)-学习笔记-第5部分:高级主题

    第17章  用于大型程序的工具 异常处理 不存在数组或函数类型的异常.相反,如果抛出一个数组,被抛出的对象转换为指向数组首元素的指针,类似地,如果抛出一个函数,函数被转换为指向该函数的指针. 不要抛出 ...

  6. 洛谷——P1379 八数码难题

    P1379 八数码难题 双向BFS 原来双向BFS是这样的:终止状态与起始状态同时入队,进行搜索,只不过状态标记不一样而已,本题状态使用map来存储 #include<iostream> ...

  7. [Algorithm] 8. Rotate String

    Description Given a string and an offset, rotate string by offset. (rotate from left to right) Examp ...

  8. [Luogu] P3701 「伪模板」主席树

    题目背景 byx和手气君都非常都非常喜欢种树.有一天,他们得到了两颗奇怪的树种,于是各自取了一颗回家种树,并约定几年后比一比谁种出来的树更加牛x. 题目描述 很快,这棵树就开花结果了.byx和手气君惊 ...

  9. HDU 5217 Brackets

    [题意概述] 给出一个有左括号和右括号的序列,左边的左括号和右边的右括号可以合并.现在要求你维护这个序列,支持两种操作: 1,翻转某个位置的括号: 2,查询区间[L,R]合并后第k个括号在原序列中的位 ...

  10. 【Codeforces 372A】Counting Kangaroos is Fun

    [链接] 我是链接,点我呀:) [题意] 如果a[i]*2<=a[j]那么i袋鼠可以装进j袋鼠里面 每只袋鼠都只能装一只袋鼠 [题解] 假设最后的方案是(ai,bi) 这里(ai,bi)表示下标 ...