zoukankan      html  css  js  c++  java
  • Codeforces 103B. Cthulhu 寻找奈亚子

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

    ...Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for the little mermaid to appear but alas, he only woke up Cthulhu...

    Whereas on the other end of the world Pentagon is actively collecting information trying to predict the monster's behavior and preparing the secret super weapon. Due to high seismic activity and poor weather conditions the satellites haven't yet been able to make clear shots of the monster. The analysis of the first shot resulted in an undirected graph with n vertices and m edges. Now the world's best minds are about to determine whether this graph can be regarded as Cthulhu or not.

    To add simplicity, let's suppose that Cthulhu looks from the space like some spherical body with tentacles attached to it. Formally, we shall regard as Cthulhu such an undirected graph that can be represented as a set of three or more rooted trees, whose roots are connected by a simple cycle.

    It is guaranteed that the graph contains no multiple edges and self-loops.

    Input

    The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 1000 ≤ m ≤ ).

    Each of the following m lines contains a pair of integers x and y, that show that an edge exists between vertices x and y (1 ≤ x, y ≤ n, x ≠ y). For each pair of vertices there will be at most one edge between them, no edge connects a vertex to itself.

    Output

    Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.

    Sample test(s)
    input
    6 6
    6 3
    6 4
    5 1
    2 5
    1 4
    5 4
    
    output
    FHTAGN!
    input
    6 5
    5 6
    4 6
    3 1
    5 1
    1 2
    
    output
    NO
    Note

    Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and22 and 3, ..., v - 1 and vv and 1.

    A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).

    A rooted tree is a tree where one vertex is selected to be the root.


    奈亚子只有一个环,环上每个点都是一棵树。。。

    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int n,m;
    int a[111][111];
    int p[111]= {0};
    int dis[111]= {0};
    bool v[111]= {0};
    int cnt=0;
    
    bool find_cycle(int i,int papa)
    {
        //cerr<<i<<" ------- "<<papa<<endl;
        v[i]=true;
        for (int j=1; j<=n; j++)
        {
            if (j!=papa&&i!=j&&a[i][j]>0)
            {
                if (!v[j])
                {
                    p[cnt++]=j;
                    if (find_cycle(j,i)) return true;
                    cnt--;
                }
                else
                {
                    p[cnt++]=j;
                    return true;
                }
            }
        }
        return false;
    }
    
    bool dfs(int i,int papa)
    {
        v[i]=true;
        for (int j=1;j<=n;j++)
        {
            if (i!=j&&j!=papa&&a[i][j])
            {
                if (papa==0&&dis[j]==0) continue;
                if (v[j]||dis[j]==0)
                {
                    return true;
                }
                else
                {
                    if (dfs(j,i)) return true;
                }
            }
        }
        return false;
    }
    
    void len_dfs(int i)
    {
        v[i]=true;
        for (int j=1;j<=n;j++)
        {
            if (!v[j]&&a[i][j]>0) len_dfs(j);
        }
    }
    
    int main()
    {
        memset(dis,-1,sizeof(dis));
        cin>>n>>m;
        for (int i=1; i<=m; i++)
        {
            int x,y;
            cin>>x>>y;
            a[x][y]=a[y][x]=1;
        }
        p[cnt++]=1;
        memset(v,0,sizeof(v));
        bool ret=find_cycle(1,0);
        if (ret)
        {
            dis[p[cnt-1]]=0;
            for (int i=cnt-2; i>=0; i--)
            {
                if (p[i]==p[cnt-1]) break;
                dis[p[i]]=0;
            }
            //ready_dfs
            for (int i=1;i<=n;i++)
            {
                if (dis[i]==0)
                {
                    memset(v,0,sizeof(v));
                    if (dfs(i,0))
                    {
                        ret=false;
                        break;
                    }
                }
            }
            //dfs_over
        }
    
        memset(v,0,sizeof(v));
        len_dfs(1);
        for (int i=1;i<=n;i++)
        {
            if (!v[i]) ret=false;
        }
        if (ret) cout<<"FHTAGN!"<<endl;
        else cout<<"NO"<<endl;
    
        return 0;
    }
    




  • 相关阅读:
    inner join ,left join ,right join 以及java时间转换
    华为机试-整形数组合并
    华为机试-公共字串计算
    两个字符串的最长公共子序列的长度
    华为机试-字符串通配符
    工作中总结的编程小技巧
    C语言高效编程的几招(绝对实用,绝对经典)
    Java float保留两位小数或多位小数
    新浪云、阿里云、百度云、谷歌云、亚马逊云
    java经典40+分析
  • 原文地址:https://www.cnblogs.com/cyendra/p/3038458.html
Copyright © 2011-2022 走看看