zoukankan      html  css  js  c++  java
  • poj2492

    本来是并查集,我用BFS做的。

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    using namespace std;

    #define maxm 2000005
    #define maxn 2005

    struct Edge
    {
    int v, next;
    } edge[maxm];

    int m, n;
    int count, head[maxn];
    int q[maxn], front, rear;
    int sex[maxn];

    void addedge(int a, int b)
    {
    edge[count].next
    = head[a];
    edge[count].v
    = b;
    head[a]
    = count;
    count
    ++;
    edge[count].next
    = head[b];
    edge[count].v
    = a;
    head[b]
    = count;
    count
    ++;
    }

    void input()
    {
    scanf(
    "%d%d", &n, &m);
    for (int i = 0; i < m; i++)
    {
    int a, b;
    scanf(
    "%d%d", &a, &b);
    a
    --;
    b
    --;
    addedge(a, b);
    }
    }

    bool bfs(int s)
    {
    q[
    0] = s;
    front
    = 0;
    rear
    = 1;
    sex[s]
    = 0;
    while (front != rear)
    {
    int a = q[front++];
    if (front == maxn)
    front
    = 0;
    for (int i = head[a]; i != -1; i = edge[i].next)
    {
    int v = edge[i].v;
    if (sex[v] == -1)
    {
    sex[v]
    = 1 - sex[a];
    q[rear
    ++] = v;
    if (rear == maxn)
    rear
    = 0;
    continue;
    }
    else if (sex[v] == sex[a])
    return false;
    }
    }
    return true;
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    int t, s = 0;
    scanf(
    "%d", &t);
    while (t--)
    {
    memset(head,
    -1, sizeof(head));
    memset(sex,
    -1, sizeof(sex));
    count
    = 0;
    s
    ++;
    printf(
    "Scenario #%d:\n", s);
    input();
    bool ans = true;
    for (int i = 0; i < n && ans; i++)
    if (sex[i] == -1)
    ans
    = bfs(i);
    if (ans)
    printf(
    "No suspicious bugs found!\n\n");
    else
    printf(
    "Suspicious bugs found!\n\n");
    }
    return 0;
    }

  • 相关阅读:
    Python编码和文件操作
    Python的list和tuple及dictionary
    Python代码对比
    python的基础知识
    day2 springcloud组件(nacos注册/配置中心 feign组件请求调用 gateway网关)
    springcloud与springboot版本对应关系
    git配置
    day1 分布式基础概念
    spring中的依赖注入(DI)笔记
    20210223 爱生气的书店老板(滑动窗口)
  • 原文地址:https://www.cnblogs.com/rainydays/p/2053608.html
Copyright © 2011-2022 走看看