51Nod 1535 深海探险

Problem

很久很久以前的一天,一位美男子来到海边,海上狂风大作。美男子希望在海中找到美人鱼,但是很不幸他只找到了章鱼怪。

然而,在世界的另一端,人们正在积极的收集怪物的行为信息,以便研制出强大的武器来对付章鱼怪。由于地震的多发,以及恶劣的天气,使得我们的卫星不能很好的定位怪物,从而不能很好的命中目标。第一次射击的分析结果会反映在一张由n个点和m条边组成的无向图上。现在让我们来确定这张图是不是可以被认为是章鱼怪。

为了简单起见,我们假设章鱼怪的形状是这样,他有一个球形的身体,然后有很多触须连接在他的身上。可以表现为一张无向图,在图中可以被认为由三棵或者更多的树(代表触须)组成,这些树的根在图中处在一个环中(这个环代表球形身体)。

题目保证,在图中没有重复的边,也没有自环。

Solution

整个图是连通的,只有一个环说明有n个节点n条边,由于没有重边,所以环至少也是3个节点,可以用并查集,所有节点find都相等,并且点数等于边数即可。

Code

#include<stdio.h>
#include<set>
#include<iostream>
#include<stack>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
typedef long long ll;
typedef long double ld;
typedef double db;
#define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int mod=1e9+7;
inline int mo(ll a,int p){
    return a>=p?a%p:a;
}
inline int rd() {
    int x = 0, f = 1;
    char ch;
    while (ch < '0' || ch > '9') {
        if (ch == '-')f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return f * x;
}
inline ll gcd(ll x, ll y){
    return y==0?x:gcd(y,x%y);
}
inline ll speed(ll a,ll b){
    ll cur=a,anss=1;
    while(b){
        if(b&1) anss=anss*cur;
        cur=cur*cur;
        b>>=1;
    }
    return anss;
}
const int MAXN=1e5;
bool ipr[MAXN+20];
int cnt,pri[MAXN/5];
void prime(){//埃式筛法
    int N=sqrt(MAXN)+0.5,mul;
    memset(ipr,true,sizeof(ipr));
    ipr[1]=false;
    for(int i=2;i<=N;i++){
        if(ipr[i]==true){
            i==2?mul=1:mul=2;
            for(int j=i*i;j<=MAXN;j+=i*mul){
                ipr[j]=false;
            }
        }
    }
    for(int i=2;i<=MAXN;i++){
        if(ipr[i]==true){
            pri[++cnt]=i;
        }
    }
}
int n,m;
int f[120];
int find(int x){
    return f[x]==0?x:f[x]=find(f[x]);
}
int main(){
    //io_opt;

    scanf("%d%d",&n,&m);
    if(n!=m){
        printf("NO\n");
        return 0;
    }
    for(int i=1;i<=m;i++){
        int x,y,u,v;
        scanf("%d%d",&x,&y);
        u=find(x),v=find(y);
        if(u!=v) f[u]=v;
    }
    for(int i=2;i<=n;i++){
        if(find(i)!=find(i-1)){
            printf("NO\n");
            return 0;
        }
    }
    printf("FHTAGN!\n");
    return 0;
}

posted @ 2020-06-26 12:01  CCWUCMCTS  阅读(122)  评论(0编辑  收藏  举报