codeforces-103B Cthulhu

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GrimCake/article/details/79967454

链接:http://codeforces.com/problemset/problem/103/B
题意:给定一个图,判断该图是否由若干棵树的树根构成一个环

题解:先并查集判连通,并且满足n=m

#include<iostream>
#include<cstring>
#include<map>
#include<vector>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
using namespace std;

int n, m;
int fa[110];
vector<int> G[110];

int Find(int x){
    return x == fa[x]?x:fa[x] = Find(fa[x]);
}

void join(int x, int y){
    int root1 = Find(x);
    int root2 = Find(y);
    if(root1!=root2){
        fa[root1] = root2;
    }
}


int main(){
    cin>>n>>m;
    for(int i = 1; i<=n; i++) fa[i] = i;
    for(int i =0 ;i<m; i++){
        int u, v;
        cin>>u>>v;
        G[u].push_back(v);
        G[v].push_back(u);
        join(u, v);
    }

    /*for(int i = 1; i<=n; i++){
        cout<<i<<" "<<fa[i]<<endl;
    }*/
    bool flag = true;

    for(int i = 1; i<=n; i++){
        if(Find(i)!=Find(1)){
            flag = false;
        }
    }
    if(n!=m) flag = false;

    if(flag == false) cout<<"NO"<<endl;
    else cout<<"FHTAGN!"<<endl;

    return 0;
}

阅读更多

没有更多推荐了,返回首页