Intergalactic Map

Time Limit: 6000ms
Memory Limit: 262144KB

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

 
解题:不错的无向图拆点最大流。
 
由于要求每个点只通过一次,可以把点约束转化为边约束。边流量为1就是了。
 
很有意思的地方啊,S是与2‘相连,而不是2相连。原因嘛!在这道题目,我们需要从1到2再到3,不重复经过点。现在化成边了,也就是不重复经过边。很明显,从2进行两次增广,第一次到1,第二次到3.两次增广都造访了2号顶点,也就是如果S直接与2相连,由于2号点的约束,导致2号点只能造访一次,也就是2-2’这条边,所有只能增广一次。将S与2‘相连,就能进行多次增广了。
 
 #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的更多相关文章

  1. SPOJ 962 Intergalactic Map (网络最大流)

    http://www.spoj.com/problems/IM/ 962. Intergalactic Map Problem code: IM Jedi knights, Qui-Gon Jinn ...

  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 0962 Intergalactic Map

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

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

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

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

  7. Intergalactic Map SPOJ - IM

    传送门 我觉得我写得已经和题解一模一样了,不知道为什么就是过不了..懒得拍了,反正不是很难,不太想浪费时间. 1~2~3的一条路径相当于从2~1的一条路径+2~3的一条路径,点不能重复经过,于是拆点. ...

  8. SPOJ - ADAFIELD ,Set+map,STL不会超时!

    ADAFIELD - Ada and Field 这个题,如果用一个字来形容的话:-----------------------------------------------嗯! 题意:n*m的空白 ...

  9. SPOJ962 Intergalactic Map(最大流)

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

随机推荐

  1. HDU1420 - Prepared for New Acmer

    集训进行了将近2个礼拜,这段时间以恢复性训练为主,我一直在密切关注大家的训练情况,目前为止,对大家的表现相当满意,首先是绝大部分队员的训练积极性很高,其次,都很遵守集训纪律,最后,老队员也起到了很好的 ...

  2. python中try…except的使用,处理程序异常

    通常情况下,在python中运行程序,多多少少会出现程序异常的问题,try……except能很好的解决程序中的异常.以下是其用法,在不同位置时进行什么样的工作和起到什么样的作用. try: 可能出现异 ...

  3. 小学生都能学会的python(闭包和迭代器)

    小学生都能学会的python(闭包和迭代器) 1. 函数名第一类对象 函数名其实就是变量名 1). 可以像变量一样互相赋值. 2). 可以作为函数的参数,进行传递 3). 可以作为返回值返回 4). ...

  4. STM32 SPI 发送第一个数据不成功问题

    STM32的标准库,跟HAL库都是很实用的, 在使用SPI库的过程中一定要注意时序的问题. 我在调试SPI过程中,调试了两个IC,都是用HAL库, 第一个IC没出问题,第二个IC出现了第一次发送数据不 ...

  5. javaScript 通过flie API读取本地文件

    File API是HTML5新增内容,依靠file和FileReader,这两个对象完成,代码如下: var fileInput = document.getElementById('test-ima ...

  6. MyBatis学习总结(5)——实现关联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  7. MyBatis学习总结(3)——优化MyBatis配置文件中的配置

    一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下: <?xml version="1 ...

  8. JDBC、事务和连接池

    一:JDBC 1.什么是JDBC JDBC(Java Data Base Connectivity)SUN公司提供的一套操作数据库的标准规范.具体来讲是一种用于执行SQL语句的Java API,为多种 ...

  9. VC6.0VB6.0 Scratch等软件

    VC6.0VB6.0 Scratch等软件 http://pan.baidu.com/s/1nv4hJrb

  10. HDU 2475 Box 树型转线型 + 伸展树

    树型转线型.第一次听说这个概念. . . , 可是曾经已经接触过了,如LCA的预处理部分和树链剖分等.可是没想到还能这么用,三者虽说有不同可是大体思想还是非常相近的,学习了. 推荐博客http://b ...