HDU 3478 Catch【二分图+思维】
来源:互联网 发布:弹弓淘宝网 编辑:程序博客网 时间:2024/06/14 22:17
A thief is running away!
We can consider the city where he locates as an undirected graph in which nodes stand for crosses and edges stand for streets. The crosses are labeled from 0 to N–1.
The tricky thief starts his escaping from cross S. Each moment he moves to an adjacent cross. More exactly, assume he is at cross u at the moment t. He may appear at cross v at moment t + 1 if and only if there is a street between cross u and cross v. Notice that he may not stay at the same cross in two consecutive moment.
The cops want to know if there’s some moment at which it’s possible for the thief to appear at any cross in the city.
Input
The input contains multiple test cases:
In the first line of the input there’s an integer T which is the number of test cases. Then the description of T test cases will be given.
For any test case, the first line contains three integers N (≤ 100 000), M (≤ 500 000), and S. N is the number of crosses. M is the number of streets and S is the index of the cross where the thief starts his escaping.
For the next M lines, there will be 2 integers u and v in each line (0 ≤ u, v < N). It means there’s an undirected street between cross u and cross v.
Output
For each test case, output one line to tell if there’s a moment that it’s possible for the thief to appear at any cross. Look at the sample output for output format.
Sample Input
2
3 3 0
0 1
0 2
1 2
2 1 0
0 1
Sample Output
Case 1: YES
Case 2: NO
题意:n,m,s分别表示地点的数,边的个数,出发点,可以看成无向图,问你从s点出发一直不断地跑,问你是否存在一个时间点,使得他的位置在任意地点都有可能.
思路:(题意稍微难懂点)其实用笔画画,就是问你是否存在二分图,存在NO(稍微注意下,多个图的情况),否则YES.
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <cmath>#include <queue>using namespace std;const int MAXN = 5e5 + 10;int vis[MAXN];vector<int> v[MAXN];bool flag;void dfs(int x, int col) { vis[x] = col; for(int i = 0; i < v[x].size(); i++) { int cnt = v[x][i]; if(vis[cnt] == vis[x]) flag = true; else if(vis[cnt] == -1) dfs(cnt, !col); }}void init() { memset(vis, -1, sizeof(vis)); for(int i = 0; i < MAXN; i++) { v[i].clear(); }}int main() { int T, p = 1; scanf("%d", &T); while(T--) { init(); int n, m, s, p1, p2; flag = false; scanf("%d %d %d", &n, &m, &s); for(int i = 1; i <= m; i++) { scanf("%d %d", &p1, &p2); v[p1].push_back(p2); v[p2].push_back(p1); } dfs(s, 0); for(int i = 0; i < n; i++) { if(vis[i] == -1) { flag = false; //不止一张图 break; } } printf("Case %d: ", p++); puts(flag ? "YES" : "NO"); } return 0;}
- HDU 3478 Catch【二分图+思维】
- HDU 3478 Catch(二分图判定)
- hdu 3478 Catch 二分图判断
- HDU 3478 Catch【kurskal+二分图染色】
- hdu 3478 Catch【并查集+二分图染色】
- HDU-3478 Catch(二分图染色+并查集)
- hdu-5726 GCD 思维||二分
- Hdu 5093 Battle ships【思维+二分图匹配】
- hdu3478 Catch (二分图判断)
- Catch hdu 3478
- hdu 3478 Catch
- HDU 3478 Catch
- HDOJ 题目3478 Catch(染色法判二分图)
- HDU 5178 pairs —— 思维 + 二分
- Hdu 6105 Gameia【思维+二分匹配】
- Hdu 5936 Difference【思维+折半枚举+二分】
- HDU 3478 二分图判断
- 【染色法】hdu 3478 Catch
- 初试快速幂
- 系统间的通信方式之(Java RMI方式详解下)(九)
- 微信小程序图文混编
- Creating a universal SNP and small indel variant caller with deep neural networks理解
- 1021. 个位数统计 (15)
- HDU 3478 Catch【二分图+思维】
- web应用添加socket接口
- PHP FPM php-fpm.conf设置详解
- jzoj P1595 过路费
- Python 中pandas读取文件Initializing from file failed
- mac 终端常用命令和vim普通命令使用
- HDOJ 1418 抱歉
- 在别人电脑上运行本机tomcat项目的流程以及问题解决
- 习题6.5