博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU Catch (二分图判断奇环+并查集判断联通)
阅读量:5244 次
发布时间:2019-06-14

本文共 2534 字,大约阅读时间需要 8 分钟。

 

Problem Description
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 条边的无向图,再给一个起点 x,从起点出发,每个单位时间只能从一点走到相邻的点上,现在问有没有一个时刻,可以在图的任意结点上。
思路:首先,图要是不连通的,则不存在这个时刻,其次,若图是二分图也不存在完美时刻(图被分为两半,一半是奇数时刻到达,一半偶数时刻到达)
因此,只有在图连通,且非二分图的时候,才能有时刻的存在。通过并查集判断连通,再判断图是否二分图即可
 
仿照染色法进行构图 根据二分图无奇环性质解决
 
code:
//#include
using namespace std;#define maxnn 2000110int n,m,s;int las[maxnn],nex[maxnn],tot,en[maxnn],col[maxnn],fla=0;int f[maxnn];int T;int gf(int v){ return f[v]==v?v:f[v]=gf(f[v]);}void add(int a,int b){ en[++tot]=b; nex[tot]=las[a]; las[a]=tot;}void erfen(int v,int num){ col[v]=num; if(fla==1) return ; for(int i=las[v];i;i=nex[i]) { int y=en[i]; if(col[y]==col[v]) { fla=1; return ; } if(!col[y]) { erfen(y,-num); } } return ;}int main(){ int x,y,z; cin>>T; int tot=0; while(T--) { tot++; scanf("%d%d%d",&n,&m,&s); memset(las,0,sizeof(las)); memset(col,0,sizeof(col)); int sec=n; for(int i=0;i

 

转载于:https://www.cnblogs.com/OIEREDSION/p/11272807.html

你可能感兴趣的文章
Java项目xml相关配置
查看>>
按钮实现A标签新窗口打开(不用window.open)
查看>>
三维变换概述
查看>>
第三次作业
查看>>
Python的classmethod和staticmethod区别
查看>>
Ubuntu12.04 英文环境下使用ibus输入中文并自动启动输入法
查看>>
SpringMVC 拦截器HandlerInterceptor(一)
查看>>
mvc知识应用
查看>>
数据结构之排序三:插入排序
查看>>
Class.forName(),classloader.loadclass用法详解
查看>>
vue route 跳转
查看>>
Device Tree Usage
查看>>
【雷电】源代码分析(二)-- 进入游戏攻击
查看>>
POJ 1220 高精度/进制转换
查看>>
cocos2d-x中CCLabelAtlas的小图片拼接
查看>>
【学习笔记】深入理解js原型和闭包系列学习笔记——精华
查看>>
深入理解js——prototype原型
查看>>
Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。...
查看>>
Ubuntu 安装之python开发
查看>>
恶心的struts标签,等我毕业设计弄完了,瞧我怎么收拾你。
查看>>