Link
http://acm.hdu.edu.cn/showproblem.php?pid=3478
Description
给定一个无向图,问从给定的某一点开始,是否某一时刻能够在任意位置,每一时刻必须移动
Solution
判断是否存在奇环,即判断是否是二分图,搜索染色即可;
Code
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>
using namespace std;
#define inf 0x7f7f7f7f
#define maxn 100050
#define N 100100
#define P 2
typedef long long ll;
typedef struct {
int u, v, next, w;
} Edge;
Edge e[10*maxn];
int cnt, head[maxn];
inline void add(int u, int v) {
e[cnt].u = u;
e[cnt].v = v;
// e[cnt].w = w;
// e[cnt].f=f;
e[cnt].next = head[u];
head[u] = cnt++;
e[cnt].u = v;
e[cnt].v = u;
// e[cnt].w = w;
// e[cnt].f=-f;
e[cnt].next = head[v];
head[v] = cnt++;
}
inline void write(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
int n,m,s,t,color[maxn];
int bfs(){
color[0]=0;
queue<int>q;
q.push(0);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].v;
if(color[v]==-1){
color[v]=color[u]^1;
q.push(v);
}else if(color[v]==color[u])return 0;
}
}
for(int i=0;i<n;i++){
if(color[i]==-1)return 1;
}
return 1;
}
int main(){
t=read();
for(int k=1;k<=t;k++){
cnt=0;
scanf("%d%d%d", &n, &m, &s);
memset(head, -1, sizeof(head));
memset(color,-1,sizeof(color));
int x, y;
for (int i = 0; i < m; i++) {
x = read(), y = read();
add(x, y);
}
printf("Case %d: %s\n",k,bfs()?"NO":"YES");
}
}
我们坚持一件事情,并不是因为这样做了会有效果,而是坚信,这样做是对的。
——哈维尔