• Brain Network (easy)


    Brain Network (easy)

    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
    分析:判断联通和环;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #include <ext/rope>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define vi vector<int>
    #define pii pair<int,int>
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    const int maxn=1e5+10;
    const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
    using namespace std;
    using namespace __gnu_cxx;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,vis[maxn],cnt;
    vi a[maxn];
    void dfs(int now,int pre)
    {
        vis[now]=1;cnt++;
        for(int x:a[now])
        {
            if(vis[x]&&x!=pre)exit(0*puts("no"));
            if(!vis[x])dfs(x,now);
        }
    }
    int main()
    {
        int i,j,k,t;
        scanf("%d%d",&n,&m);
        rep(i,1,m)scanf("%d%d",&j,&k),a[j].pb(k),a[k].pb(j);
        dfs(j,0);
        if(cnt==n)puts("yes");else puts("no");
        //system ("pause");
        return 0;
    }
     
  • 相关阅读:
    Linux Vim编辑器
    Linux sed 流编辑器
    Shell 编程 (变量和条件测试)
    Linux 下 Bash配置文件读取
    Linux 用户、权限
    Linux 指令(一)文件/目录操作
    Ubuntu 下安装 Swoole
    Mysql IN语句查询
    Mysql 查询优化
    Mysql 获取表属性
  • 原文地址:https://www.cnblogs.com/dyzll/p/5668712.html
Copyright © 2020-2023  润新知