hdu 5215 Cycle(判断是否有奇数偶数的环)
来源:互联网 发布:室内设计有什么软件 编辑:程序博客网 时间:2024/06/05 09:38
Cycle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 179 Accepted Submission(s): 52
Problem Description
Ery is interested in graph theory, today he ask BrotherK a problem about it: Given you a undirected graph with N vertexes and M edges, you can select a vertex as your starting point, then you need to walk in the graph along edges. However, you can't pass a edge more than once, even opposite direction is forbidden. At the end, you should come back to the starting point. Assume you has passed X edges, there are two questions:
Question 1: CanX be a odd number ?
Question 2: CanX be a even number ?
(note: you must walk, soX can't be 0)
Question 1: Can
Question 2: Can
(note: you must walk, so
Input
The first line contains a single integer T , indicating the number of test cases.
Each test case begins with two integerN, M , indicating the number of vertexes and the number of edges. Following M lines, each line contains two integers Ui, Vi , indicating there are a edge between vertex Ui and vertex Vi .
T is about 30
1 ≤ N ≤ 100000
0 ≤ M ≤ 300000
1 ≤ Ui,Vi ≤ N
Ui will not equal to Vi
There is at most one edge between any pair of vertex.
Each test case begins with two integer
There is at most one edge between any pair of vertex.
Output
For each test, print two lines.
The first line contains "YES" or "NO" for question 1.
The second line contains "YES" or "NO" for question 2.
The first line contains "YES" or "NO" for question 1.
The second line contains "YES" or "NO" for question 2.
Sample Input
31 03 31 22 33 14 41 22 33 44 1
Sample Output
NONOYESNONOYESHintIf you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.
记录dfs过程中遍历点的深度 记录距离是偶数还是奇数
#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 100010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;vector<int> vec[MAXN];int dep[MAXN],vis[MAXN];int ans[2];int ji[MAXN];void dfs(int x,int fa){ vis[x]=1; int sz=vec[x].size(); for(int i=0;i<sz;i++) { int y=vec[x][i]; if(y==fa) continue; if(vis[y]) { if(dep[x]>=dep[y]) { int len=(dep[x]-dep[y]+1)&1; ans[len]++; if(len%2==1) { ji[x]++; if(ji[y]) ans[0]++;//两个都在奇数环里 } } continue; } dep[y]=dep[x]+1; dfs(y,x); }}int main(){// fread; int tc; scanf("%d",&tc); while(tc--) { int n,m; scanf("%d%d",&n,&m); ans[0]=ans[1]=0; MEM(vis,0); MEM(dep,0); MEM(ji,0); for(int i=0;i<=n;i++) vec[i].clear(); for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); vec[u].push_back(v); vec[v].push_back(u); } for(int i=1;i<=n;i++) { if(!vis[i]) dfs(i,0); }// bug; if(ans[1]) puts("YES"); else puts("NO"); if(ans[0]) puts("YES"); else puts("NO"); } return 0;}
0 0
- hdu 5215 Cycle(判断是否有奇数偶数的环)
- 141. Linked List Cycle (判断单链表中是否有环)
- 奇数和偶数的判断
- 输入任意大小的三个整数,判断其中是否有两个奇数一个偶数。若是,则输出“YES”,不是则输出“NOT” (要求用条件表达式进行判断,不使用if语句)
- Linked List Cycle 判断链表是否有环,如果有环,找到环的入口
- (笔记)两种判断奇数偶数的方法
- (笔记)两种判断奇数偶数的方法
- 判断链表是否有环及环入口点的求法(Linked List Cycle II )
- 判断链表是否有环及环入口点的求法(Linked List Cycle II )
- 判断奇数偶数的时候用%而不是/
- 判断链表是否有环 Linked List Cycle
- Linked List Cycle --判断链表是否有环
- leetcode--Linked List Cycle--判断链表是否有环
- Linked List Cycle 判断链表是否有环
- 141. Linked List Cycle 判断单链表中是否有环
- leetcode_141. Linked List Cycle 判断链表中是否有环
- 141. Linked List Cycle | 判断单链表是否有环
- Linked List Cycle:判断链表是否有环
- hdu 4020题解
- LeetCode Add Two Numbers
- 黑马程序员—泛型
- 4.19
- I.MX6Q(TQIMX6Q/TQE9)学习笔记——新版BSP之根文件系统挂载
- hdu 5215 Cycle(判断是否有奇数偶数的环)
- VMWare WorkStation 11 密钥
- 黑马程序员—集合框架
- HDU5218 Game
- Eclipse:”在对象后输入下点没有提示“问题的修正
- [APIO2009]抢掠计划(强连通分量+缩点+拓扑排序+dp)
- 仿微信底部Tab切换,TabHost+Fragment的用法
- 每日总结
- 又一年 —— 记浙江第十二届省赛