题目链接:http://www.spoj.com/problems/IM/en/

Time limit:491 ms  Memory limit:1572864 kB  Code length Limit:50000 B

Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padmé Amidala to save Naboo from 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到n共n个点,互相之间有m条边连接,主人公呢想从1出发,经过2,到达3,不走重复路也不走重复点,问你可不可行。

怎么说呢,最大流构图题做的多了,就有点感觉了,大概知道怎么个建图方向了。

首先,瞟了一眼[网络流建模汇总][Edelweiss].pdf上的思路,知道了从2出发,往1和3两个汇点做最大流,这样就可以满足题目要求;

然后我们就建立超级源点s,和超级汇点t,点2连到s上去,cap=2;点1、3都连到t上去,cap=1;

然后我们还要想到一件事情,不能走重复边,很简单,所有题目里给的边的cap都设为1;

那不走重复点呢?就需要拆点,说起来有点玄乎,其实就是原本是个点,然后我们两只手拿住他,吧唧那么一拉,诶就变成了一条边,

显然,我们就记这条拆点边为( from = in(i) , to = out(i) , cap = 1);

这样我们的图就建好了,剩下来的就是dinic了。

 #include<cstdio>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAXN 2*(30011+1000)
using namespace std;
int n,m;
struct Edge{
int u,v,c,f;
};
struct Dinic
{
int s,t;
vector<Edge> E;
vector<int> G[MAXN];
bool vis[MAXN];
int lev[MAXN];
int cur[MAXN];
void init(int n)
{
E.clear();
for(int i=;i<=n;i++) G[i].clear();
}
void addedge(int from,int to,int cap)
{
E.push_back((Edge){from,to,cap,});
E.push_back((Edge){to,from,,});
G[from].push_back(E.size()-);
G[to].push_back(E.size()-);
}
bool bfs()
{
memset(vis,,sizeof(vis));
queue<int> q;
q.push(s);
lev[s]=;
vis[s]=;
while(!q.empty())
{
int now=q.front(); q.pop();
for(int i=;i<G[now].size();i++)
{
Edge edge=E[G[now][i]];
int nex=edge.v;
if(!vis[nex] && edge.c>edge.f)
{
lev[nex]=lev[now]+;
q.push(nex);
vis[nex]=;
}
}
}
return vis[t];
}
int dfs(int now,int aug)
{
if(now==t || aug==) return aug;
int flow=,f;
for(int& i=cur[now];i<G[now].size();i++)
{
Edge& edge=E[G[now][i]];
int nex=edge.v;
if(lev[now]+ == lev[nex] && (f=dfs(nex,min(aug,edge.c-edge.f)))>)
{
edge.f+=f;
E[G[now][i]^].f-=f;
flow+=f;
aug-=f;
if(!aug) break;
}
}
return flow;
}
int maxflow()
{
int flow=;
while(bfs())
{
memset(cur,,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
}dinic;
int in(int x){return x;}
int out(int x){return x+n;}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);//n个点,m条边
dinic.init(*n+);
for(int i=;i<=m;i++)
{
int from,to;
scanf("%d%d",&from,&to);
if(<=from && from<=n && <=to && to<=n)
{
dinic.addedge(out(from),in(to),);
dinic.addedge(out(to),in(from),);
}
}
dinic.s=, dinic.t=*n+;
dinic.addedge(dinic.s,in(),);
dinic.addedge(out(),dinic.t,);
dinic.addedge(out(),dinic.t,);
for(int i=;i<=n;i++) dinic.addedge(in(i),out(i),i==?:);
if(dinic.maxflow()==) printf("YES\n");
else printf("NO\n");
}
}

PS.听说测试输入边的时候,还会超出范围?不管是不是真的,反正加个if()语句鲁棒一下,无所谓。

SPOJ IM - Intergalactic Map - [拆点最大流]的更多相关文章

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

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

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

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

  3. SPOJ 962 Intergalactic Map

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

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

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

  5. SPOJ 0962 Intergalactic Map

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

  6. Risk UVA - 12264 拆点法+最大流+二分 最少流量的节点流量尽量多。

    /** 题目:Risk UVA - 12264 链接:https://vjudge.net/problem/UVA-12264 题意:给n个点的无权无向图(n<=100),每个点有一个非负数ai ...

  7. hdu4289 最小割最大流 (拆点最大流)

    最小割最大流定理:(参考刘汝佳p369)增广路算法结束时,令已标号结点(a[u]>0的结点)集合为S,其他结点集合为T=V-S,则(S,T)是图的s-t最小割. Problem Descript ...

  8. Control(拆点+最大流)

    Control http://acm.hdu.edu.cn/showproblem.php?pid=4289 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  9. BZOJ 1877 晨跑 拆点费用流

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1877 题目大意: Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧 ...

随机推荐

  1. OpenSift源代码编译过程记录

    本文记录了在CentOS6.5上编译Sift的开源实现OpenSift的编译过程,同一时候记录了编译过程中的几个问题. sift的理论已经有非常多了,以下会给出链接: 1.Requirements a ...

  2. Java通过复选框控件数组实现添加多个复选框控件

    编写程序,通过复选框控件数组事先选择用户爱好信息的复选框,在该程序中,要求界面中的复选框数量可以根据指定复选框名称的字符串数组的长度来自动调节. 思路如下: 创建JPanel面板对象: 使用JPane ...

  3. C++实现按1的个数排序

    题目内容:有一些0.1字符串,将其按1的个数的多少的顺序进行输出. 输入描述:本题只有一组测试数据.输入数据由若干数字组成,它是由若干个0和1组成的数字. 输出描述:对所有输入的数据,按1的个数进行生 ...

  4. 5 -- Hibernate的基本用法 --4 3 JDBC连接属性

    Hibernate需要进行数据库访问,因此必须设置连接数据库的相关属性.所有Hibernate属性的名字和语义都在org.hibernate.cfg.Environment中定义. 关于JDBC连接配 ...

  5. 5 -- Hibernate的基本用法 --1 1 对象/关系数据库映射(ORM)

    ORM的全称是Object/Relation Mapping ,即对象/关系数据库映射.ORM可理解成一种规范,它概述了这类框架的基本特征:完成面向对象的编程语言到关系数据库的映射.当ORM框架完成映 ...

  6. scala spray 概念性内容的总结

    spray 是基于 akka 的轻量级 scala 库,可用于编写 REST API 服务.了解 spray 的 DSL 后可以在很短的时间内写出一个 REST API 服务,它的部署并不需要 tom ...

  7. JSON XSS

    漏洞实例一: 1.在更新用户信息,修改联系电话,抓包绕过前端20个字符限制,Payload为 111<img src=1 onerror=alert(1)> 2.更新后,访问json 3. ...

  8. Python对文件和文件路径的管理

    1. 使用os.path进行路径和文件管理 1.1 拆分路径 os.path.split                   返回一个二元组,包含文件路径和文件名 os.path.dirname    ...

  9. 如何使用 SSH 连接 VMWare 虚拟机中的 Ubuntu

    环境:VMWare Player 5.0.2,Ubuntu 13.10  VMWare共有3种网络连接模式,分别是: 1. bridged(桥接模式):虚拟机将直接连接到物理局域网,使自身独立于宿主机 ...

  10. 微信小程序--消息推送配置Token令牌错误校验失败如何解决

    微信开放第三方API接口, 申请地址: https://mp.weixin.qq.com/advanced/advanced?action=interface&t=advanced/inter ...