zoukankan      html  css  js  c++  java
  • Helvetic Coding Contest 2016 online mirror C1

    Description

    One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (as previously suspected) any kind of brain defect – it's the opposite! Independent researchers confirmed that the nervous system of a zombie is highly complicated – it consists of nbrains (much like a cow has several stomachs). They are interconnected by brain connectors, which are veins capable of transmitting thoughts between brains. There are two important properties such a brain network should have to function properly:

    1. It should be possible to exchange thoughts between any two pairs of brains (perhaps indirectly, through other brains).
    2. There should be no redundant brain connectors, that is, removing any brain connector would make property 1 false.

    If both properties are satisfied, we say that the nervous system is valid. Unfortunately (?), if the system is not valid, the zombie stops thinking and becomes (even more) dead. Your task is to analyze a given nervous system of a zombie and find out whether it is valid.

    Input

    The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 1000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is given as a pair of brains ab it connects (1 ≤ a, b ≤ na ≠ b).

    Output

    The output consists of one line, containing either yes or no depending on whether the nervous system is valid.

    Examples
    input
    4 4
    1 2
    2 3
    3 1
    4 1
    output
    no
    input
    6 5
    1 2
    2 3
    3 4
    4 5
    3 6
    output
    yes
    并查集判断环~
    #include<stdio.h>
    //#include<bits/stdc++.h>
    #include<string.h>
    #include<iostream>
    #include<math.h>
    //#include<map>
    #include<sstream>
    #include<set>
    #include<queue>
    #include<vector>
    #include<algorithm>
    #include<limits.h>
    #define inf 0x3fffffff
    using namespace std;
    const double pi = acos(-1.);
    #define maxn 100005
    int fa[maxn];
    bool ff[maxn];
    int findd( int x )
    {
        int k, j, r;
        r = x;
        while(r != fa[r])     //查找跟节点
            r = fa[r];      //找到跟节点,用r记录下
        k = x;
        while(k != r)             //非递归路径压缩操作
        {
            j = fa[k];         //用j暂存parent[k]的父节点
            fa[k] = r;        //parent[x]指向跟节点
            k = j;                    //k移到父节点
        }
        return r;         //返回根节点的值
    }
    int main()
    {
        int u,v,x,y,flag = 1;
        memset(ff, false, sizeof(ff));
        int k = 0;
        int n,m;
        cin>>n>>m;
        for(int i = 1; i <=n; i++)
        {
            fa[i]=i;
        }
        for(int j=1;j<=m;j++)
        {
            cin>>u>>v;
            x = findd(u), y = findd(v);
            if(x!=y)
                fa[x]=y;
            else
                flag=0;
        }
        for(int i=1;i<=n;i++)
        {
            if(fa[i]==i)
            {
                k++;
            }
        }
        if(flag&&k<=1)
        {
            puts("yes");
        }
        else
        {
            puts("no");
        }
        return 0;
    }
    

      

  • 相关阅读:
    Neutron介绍
    docker下jenkins环境搭建
    OpenStack allinone环境搭建
    go中channel的部分理解
    总结一下我的面试经历
    MYSQL基础总结(1)
    struts2中判断数组的长度
    antd组件Checkbox
    React使用antd组件Radio
    React使用Echarts
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/5658642.html
Copyright © 2011-2022 走看看