SPOJ 962 IM
来源:互联网 发布:吃奶酪 知乎 编辑:程序博客网 时间:2024/06/06 02:21
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再走到3。
解题思路:
开始怎么也想不到怎么让他先到一个点再到一个点。后来才知道,因为这个图事无向图,我们只需要把中间的节点2作为起点,找两条没有点重合的路径到1和3即可。
对于每个点只经过一次的限定。我们只需要把每个点拆成一个入点,一个出点,然后从入点到出点连一条容量为1的边即可。最后我们把1、3连到汇点,若总流量为2则满足题目要求。
最后,这题有一点很坑,输入的时候,可能有边不在1~n的范围内,需要跳过这些边。
AC代码:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <stack>#include <cmath>#include <cstdlib>#include <string>#include <map>#include <ctime>using namespace std;#define INF 0x3f3f3f3f#define LL long long#define fi first#define se second#define PII pair<LL,LL>#define pb push_back#define mp make_pair#define mem(a,b) memset((a),(b),sizeof(a))const int MAXN=30015;const int MAXV=MAXN*2;struct Edge{ int to,cap,rev; Edge(int t,int c,int r):to(t),cap(c),rev(r){}};int N,M,V;vector<Edge> G[MAXV];int level[MAXV];int iter[MAXV];void init(){ for(int i=0;i<V;++i) G[i].clear();}void add_edge(int from,int to,int cap){ G[from].push_back(Edge(to,cap,G[to].size())); G[to].push_back(Edge(from,0,G[from].size()-1));}void bfs(int s){// mem(level,-1); for(int i=0;i<V;++i) level[i]=-1; queue<int> que; level[s]=0; que.push(s); while(!que.empty()) { int v=que.front(); que.pop(); for(int i=0;i<G[v].size();++i) { Edge &e=G[v][i]; if(e.cap>0&&level[e.to]<0) { level[e.to]=level[v]+1; que.push(e.to); } } }}int dfs(int v,int t,int f){ if(v==t) return f; for(int &i=iter[v];i<G[v].size();++i) { Edge &e=G[v][i]; if(e.cap>0&&level[v]<level[e.to]) { int d=dfs(e.to,t,min(f,e.cap)); if(d>0) { e.cap-=d; G[e.to][e.rev].cap+=d; return d; } } } return 0;}int dinic(int s,int t){ int flow=0; while(true) { bfs(s); if(level[t]<0) return flow; for(int i=0;i<V;++i) iter[i]=0; int f; while((f=dfs(s,t,INF))>0) flow+=f; }}int main(){ int T_T; scanf("%d",&T_T); while(T_T--) { scanf("%d%d",&N,&M); //0 汇点 //1 ~ N 入点 //N+1 N*2 出点 V=N*2+1; int s=2+N,t=0; init(); for(int i=1;i<=N;++i) add_edge(i, i+N, 1); for(int i=0;i<M;++i) { int u,v; scanf("%d%d",&u,&v); if(u<1||u>N||v<1||v>N) continue; add_edge(u+N, v, 1);//无向边拆成两个有向边 add_edge(v+N, u, 1); } add_edge(1+N,t,1);//1、3连接到汇点 add_edge(3+N,t,1); puts(dinic(s,t)==2?"YES":"NO"); } return 0;}
- SPOJ 962 IM
- SPOJ IM -- Intergalactic Map
- SPOJ IM 962 网络流最大流 解题报告
- IM
- IM
- IM
- IM
- IM
- SPOJ 962 网络流
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- argc argv
- HDOJ 2001 计算两点间的距离
- 常见HTTP状态码
- 【网络爬虫】使用HttpClient4.3.5抓取数据
- OpenCV安装教程及错误:Unsupported gpu architecture 'compute_11' 解决方法
- SPOJ 962 IM
- reactnative Dimensions进行获取屏幕宽度和高度信息
- 【JAVA-基础】—类成员单例(Singleton)类
- 学习TP框架(二)
- SSH学习笔记
- HZAU校赛补题PA(动态规划)
- Java异常处理、异常处理使用的一些注意点(例如,基类和子类捕获的顺序问题)
- 扩展欧几里德 求最小整数解 a,b,c可以为负数
- Java基本数据类型与位运算、原码&补码&反码