HDU Catch (二分图判断奇环+并查集判断联通)

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<bits/stdc++.h>
using namespace std;
#define maxnn 2000110
int 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<n;i++)
            f[i]=i;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
            if(gf(x)!=gf(y))
            {
                f[gf(x)]=gf(y);
                sec--;
            }
        }
        if(sec!=1)
        {
            printf("Case %d: ",tot);
            printf("NO\n");
            continue;
        }
        fla=0;
        erfen(s,1);
        printf("Case %d: ",tot);
        if(fla==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
}

原文地址:https://www.cnblogs.com/OIEREDSION/p/11272807.html

时间: 2024-10-25 07:50:26

HDU Catch (二分图判断奇环+并查集判断联通)的相关文章

【板+并查集判断连通性】并查集判断连通性

1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<string> 6 #include<algorithm> 7 #include<queue> 8 #include<vector> 9 10 using namespace std; 11 typedef long long ll;

HDOJ Ice_cream&#39;s world I 2120【并查集判断成环】

Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 848    Accepted Submission(s): 494 Problem Description ice_cream's world is a rich country, it has many fertile lands. Today,

[转载]HDU 3478 判断奇环

题意:给定n个点,m条边的无向图(没有重边和子环).从给定点出发,每个时间走到相邻的点,可以走重复的边,相邻时间不能停留在同一点,判断是否存在某个时间停留在任意的n个点. 分析: (1)首先,和出发点的位置没有关系.因为可以走重复的边,且时间没有限制大小. (2)图必须是联通的 (3) 1)图为:2-0-1-3 从0点出发(时间为0),一个时间后到达1或2(时间为1),再一个时间后到达0或3(时间为2)... 可以发现,点分为两类,奇数时间到达和偶数时间到达,答案为NO 2)图为:2-0-1-2

用并查集判断一个无向图中是否存在环

并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.常常在使用中以森林来表示.集就是让每个元素构成一个单元素的集合,也就是按一定顺序将属于同一组的元素所在的集合合并. Find:确定元素属于哪一个子集.它可以被用来确定两个元素是否属于同一子集合. Union:将两个子集合并成同一个集合. 其实判断一个图是否存在环已经有相应的算法,此文用并查集来判断一个图是否有环. 我们可以用一个一维数组parent[] 来记录子集合. 看下面这个图: 0 |   \

hdu 1829 &amp;amp;poj 2492 A Bug&amp;#39;s Life(推断二分图、带权并查集)

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8528    Accepted Submission(s): 2745 Problem Description Background  Professor Hopper is researching the sexual behavior of a rare

hdu 5424 回溯+并查集判断连通性

题意:给定一个n个点n条边的无向图,判断是否存在哈密顿路径. 思路:先用并查集判断图是否连通,然后从度数最小的点开始回溯,看是否能找到一条哈密顿路径. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <set> 5 using namespace std; 6 7 const int INF = 999999; 8 const int N = 1001; 9

BZOJ 4025 二分图(时间树+并查集)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4025 [题目大意] 给出一张图,有些边只存在一段时间,问在一个每个时间段, 这张图是否是二分图 [题解] 判断是否是二分图只要判断是否存在奇环即可, 我们对时间进行分治,在操作树上加删边, 保留涵盖时间区间的有效操作,将剩余操作按时间划分到两端的子树, 退出子树的时候撤销加边操作. 对于判断奇环,我们用并查集维护每个点与标兵的相对距离的奇偶性即可, 由于需要撤销操作,我们放弃对并查集

hdu 5458 Stability(树链剖分+并查集)

Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total Submission(s): 1347    Accepted Submission(s): 319 Problem Description Given an undirected connected graph G with n nodes and m edges, with possibly r

HDU 3047 Zjnu Stadium 带权并查集

题目来源:HDU 3047 Zjnu Stadium 题意:给你一些人 然后每次输入a b c 表示b在距离a的右边c处 求有多少个矛盾的情况 思路:用sum[a] 代表a点距离根的距离 每次合并时如果根一样 判断sum数组是否符合情况 根不一样 合并两棵树 这里就是带权并查集的精髓 sum[y] = sum[a]-sum[b]+x 这里y的没有合并前b的根 #include <cstdio> #include <cstring> using namespace std; cons