HDU 3478 Catch(判奇环)

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");
    }
}

我们坚持一件事情,并不是因为这样做了会有效果,而是坚信,这样做是对的。
——哈维尔

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值