SPOJ 962 Intergalactic Map
Intergalactic Map
This problem will be judged on SPOJ. Original ID: IM
64-bit integer IO format: %lld Java class name: Main
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
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
};
arc e[maxn*];
int head[maxn],d[maxn],cur[maxn];
int tot,S,T,n,m;
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
int q[maxn],hd,tl;
bool bfs(){
hd = tl = ;
memset(d,-,sizeof(d));
q[tl++] = S;
d[S] = ;
while(hd < tl){
int u = q[hd++];
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q[tl++] = e[i].to;
}
}
}
return d[T] > -;
}
int dfs(int u,int low){
if(u == T) return low;
int tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == d[u]+&&(a=dfs(e[i].to,min(e[i].flow,low)))){
e[i].flow -= a;
e[i^].flow += a;
tmp += a;
low -= a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic(){
int ans = ;
while(bfs()){
memcpy(cur,head,sizeof(head));
ans += dfs(S,INF);
}
return ans;
}
int main() {
int cs,u,v;
scanf("%d",&cs);
while(cs--){
scanf("%d %d",&n,&m);
memset(head,-,sizeof(head));
S = tot = ;
T = n<<|;
for(int i = ; i < m; ++i){
scanf("%d %d",&u,&v);
if(u > n || v > n) continue;
add(u+n,v,);
add(v+n,u,);
}
for(int i = ; i <= n; ++i) add(i,i+n,);
add(+n,T,);
add(+n,T,);
add(S,+n,);//注意细节啊
printf("%s\n",dinic() == ?"YES":"NO");
}
return ;
}
SPOJ 962 Intergalactic Map的更多相关文章
- SPOJ 962 Intergalactic Map (网络最大流)
http://www.spoj.com/problems/IM/ 962. Intergalactic Map Problem code: IM Jedi knights, Qui-Gon Jinn ...
- 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 0962 Intergalactic Map
题目大意:在一个无向图中,一个人要从A点赶往B点,之后再赶往C点,且要求中途不能多次经过同一个点.问是否存在这样的路线.(3 <= N <= 30011, 1 <= M <= ...
- [SPOJ962]Intergalactic Map 拆点+最大流
Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padmé Ami ...
- 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) ...
- Intergalactic Map SPOJ - IM
传送门 我觉得我写得已经和题解一模一样了,不知道为什么就是过不了..懒得拍了,反正不是很难,不太想浪费时间. 1~2~3的一条路径相当于从2~1的一条路径+2~3的一条路径,点不能重复经过,于是拆点. ...
- SPOJ - ADAFIELD ,Set+map,STL不会超时!
ADAFIELD - Ada and Field 这个题,如果用一个字来形容的话:-----------------------------------------------嗯! 题意:n*m的空白 ...
- SPOJ962 Intergalactic Map(最大流)
题目问一张无向图能否从1点走到2点再走到3点,且一个点只走一次. 思维定势思维定势..建图关键在于,源点向2点连边,1点和3点向汇点连边! 另外,题目数据听说有点问题,出现点大于n的数据.. #inc ...
随机推荐
- 说说Shell在代码重构中的应用
说说Shell在代码重构中的应用 出处信息 出处:http://blogread.cn/it/article/3426?f=wb 代码重构(Code refactoring)有时是很枯燥的,字符 ...
- HDU 5918 Sequence I
题目来源:2016 CCPC 长春站 题意:给出两个序列 a[] , b[] ,如果b1,b2....bm能够与aq,aq+p,aq+2p...aq+(m-1)p对应( q+(m-1)p<=n ...
- Laravel源码解析之从入口开始
前言 提升能力的方法并非使用更多工具,而是解刨自己所使用的工具.今天我们从Laravel启动的第一步开始讲起. 入口文件 laravel是单入口框架,所有请求必将经过index.php define( ...
- Visual Studio 2015 改变窗体图标 & 任意位置打开窗体 & 禁止鼠标改动窗体大小
1.改变窗体图标 先把图标放到项目文件夹中,然后点击窗体属性的ICON添加即可. 参考:https://www.cnblogs.com/yangxuli/p/8075484.html?tdsource ...
- [Angular] Handle HTTP Errors in Angular with HttpErrorResponse interface
When communicating with some backend API, data travels over the network using the HTTP protocol. As ...
- 数据库之Case When
近期几天的工作本来组长是安排我用mindfocion画几个图,本来以为难点是这个控件的使用,可是开发的时候才发现由于数据量有点多,所以在开发的时候汇总这些信息倒是费了我许多的功夫,最后总结一下就是写了 ...
- Docker 命令行和后台參数
Docker官方为了让用户高速了解Docker,提供了一个交互式教程,旨在帮助用户掌握Docker命令行的用法. Docker 命令行 以下对Docker的命令清单进行简单的介绍,具体内容在兴许章节具 ...
- iOS UI08_UITableView
(http://img.blog.csdn.net/20150808103801391) // // MainViewController.m // UI08_UITableView // // Cr ...
- poj_1952最大下降子序列,统计个数
其实不算难的一道题,但憋了我好久,嗯,很爽. #include<iostream> #include<cstdio> #include<string.h> #inc ...
- IDE-IntelliJ IDEA
IDE-IntelliJ IDEA 主题.字体.编辑区主题.文件编码修改.乱码问题 主题修改 上图标注 1 所示为 IntelliJ IDEA 修改主题的地方,可以通过打开左上角的File -> ...