• codeforces 690C1 C1. Brain Network (easy)(水题)


    题目链接:

    C1. Brain Network (easy)

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 n brains (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

    题意:

    给n个节点和m条边,判断是否是一棵树;

    思路:

    bfs();

    AC代码:

    #include <bits/stdc++.h>
    /*
    #include <vector>
    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <map>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    */
    using namespace std;
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef  long long LL;
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e18;
    const int N=3e5+10;
    const int maxn=1005;
    const double eps=1e-10;
    
    
    
    int n,m,vis[1005];
    vector<int>ve[1005];
    queue<int>qu;
    
    void bfs()
    {
    
        qu.push(1);
        vis[1]=1;
        int num=0;
        while(!qu.empty())
        {
            int fr=qu.front();
            qu.pop();
            num++;
            int len=ve[fr].size();
            for(int i=0;i<len;i++)
            {
                int y=ve[fr][i];
                if(!vis[y])
                {
                    vis[y]=1;
                    qu.push(y);
                }
            }
        }
        if(num==n&&m==n-1)cout<<"yes"<<"
    ";
        else cout<<"no"<<"
    ";
    }
    
    int main()
    {
            int u,v;
            read(n);read(m);
            For(i,1,m)
            {
                read(u);read(v);
                ve[u].push_back(v);
                ve[v].push_back(u);
            }
            bfs();
            return 0;
    }
  • 相关阅读:
    [BZOJ]1040: [ZJOI2008]骑士
    [BZOJ]1177: [Apio2009]Oil
    【luogu3384】【模板】树链剖分
    【NOIP2012TG】solution
    【NOIP2014TG】solution
    【NOIP2016TG】solution
    【NOIP2015TG】solution
    【NOIP2016】【LCA】【树上差分】【史诗级难度】天天爱跑步
    【网络流】【BZOJ1221】【HNOI2001】软件开发
    【网络流】【BZOJ1061】【NOI2008】志愿者招募
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5661787.html
Copyright © 2020-2023  润新知