因为是无向图,所以从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),跑一遍dinic看答案是否为2即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=1000005,inf=1e9;
int T,n,m,h[N],cnt,s,t,le[N];
struct qwe
{
int ne,to,va;
}e[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v,int w)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
e[cnt].va=w;
h[u]=cnt;
}
void ins(int u,int v,int w)
{//cout<<u<<" "<<v<<endl;
add(u,v,w);
add(v,u,0);
}
bool bfs()
{
memset(le,0,sizeof(le));
queue<int>q;
le[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=h[u];i;i=e[i].ne)
if(!le[e[i].to]&&e[i].va>0)
{
le[e[i].to]=le[u]+1;
q.push(e[i].to);
}
}
return le[t];
}
int dfs(int u,int f)
{
if(u==t||!f)
return f;
int us=0;
for(int i=h[u];i&&us<f;i=e[i].ne)
if(le[e[i].to]==le[u]+1&&e[i].va>0)
{
int t=dfs(e[i].to,min(e[i].va,f-us));
e[i].va-=t;
e[i^1].va+=t;
us+=t;
}
return us;
}
int dinic()
{
int re=0;
while(bfs())
re+=dfs(s,inf);
return re;
}
int main()
{
T=read();
while(T--)
{
n=read(),m=read();
memset(h,0,sizeof(h));
s=0,t=2*n+1;cnt=1;
ins(s,2+n,2);
ins(1,t,1);
ins(3,t,1);
for(int i=4;i<=n;i++)
ins(i,i+n,1);
for(int i=1;i<=m;i++)
{
int x=read(),y=read();
if(x<=0||y<=0||x>n||y>n)
continue;
ins(x+n,y,1);
ins(y+n,x,1);
}
/*
ins(s,2,2);
ins(1+n,t,1);
ins(3+n,t,1);
for(int i=4;i<=n;i++)
ins(i+n,i,1);
for(int i=1;i<=m;i++)
{
int x=read(),y=read();
if(x<=0||y<=0||x>n||y>n)
continue;
ins(x,y+n,1);
ins(y,x+n,1);
}
*/
if(dinic()==2)
puts("YES");
else
puts("NO");
}
return 0;
}

spoj 962 IM - 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 IM - Intergalactic Map - [拆点最大流]

    题目链接:http://www.spoj.com/problems/IM/en/ Time limit:491 ms Memory limit:1572864 kB Code length Limit ...

  3. SPOJ 962 Intergalactic Map

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

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

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

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

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

  6. SPOJ 0962 Intergalactic Map

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

  7. Intergalactic Map SPOJ - IM

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

  8. SPOJ962 Intergalactic Map(最大流)

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

  9. SPOJ 4110 Fast Maximum Flow (最大流模板)

    题目大意: 无向图,求最大流. 算法讨论: Dinic可过.终于我的常数还是太大.以后要注意下了. #include <cstdio> #include <cstring> # ...

随机推荐

  1. python学习之-- socketserver模块

    socketserver 模块简化了网络服务器的编写,主要实现并发的处理. 主要有4个类:这4个类是同步进行处理的,另外通过ForkingMixIn和ThreadingMixIn类来支持异步.sock ...

  2. PAT (Advanced Level) 1035. Password (20)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  3. POJ 3254 【状态压缩DP】

    题意: 给一块n*m的田地,1代表肥沃,0代表贫瘠. 现在要求在肥沃的土地上种草,要求任何两个草都不能相邻. 问一共有多少种种草的方法. 种0棵草也是其中的一种方法. n和m都不大于12. 思路: 状 ...

  4. 51nod 1499 (最小割)

    题意 分析 将一些点分成两个集合,很明显的最小割问题 设一个S.T,和S相连的点表示在B集合中,和T相连的点表示在A集合中 因为原题是完美值最大,我们转换一下,变成损失的价值最小,那么就是最小割问题了 ...

  5. Java并发包——线程安全的Collection相关类

    Java并发包——线程安全的Collection相关类 摘要:本文主要学习了Java并发包下线程安全的Collection相关的类. 部分内容来自以下博客: https://www.cnblogs.c ...

  6. jq自定义裁剪,代码超级简单,易修改

    1.自定义宽高效果 1.html 代码  index.html <!DOCTYPE html> <html lang="en"> <head> ...

  7. [Rust] Load a WebAssembly Function Written in Rust and Invoke it from JavaScript

    In this lesson we are going to setup a project from scratch by introducing the JavaScript snippet to ...

  8. android 多进程 Binder AIDL Service

    本文參考http://blog.csdn.net/saintswordsman/article/details/5130947 android的多进程是通过Binder来实现的,一个类,继承了Bind ...

  9. 重载OverLoad。隐藏new

    <1> using System; using System.Collections.Generic; using System.Linq; using System.Text; name ...

  10. Apach POI 如何拿到有公式的单元格,计算结果

    public static void getFormulaCellValue(){ FileInputStream fis = new FileInputStream("c:/temp/te ...