cf 80 div1 B(graph)

來源:互聯網
上載者:User

題目意思翻譯過來後就是讓你判斷一個圖是否聯通,且恰只包含一個環。比較笨的方法是先判聯通,然後找出那個環,然後再dfs判環是否唯一。但是其實在判聯通後,只需再看n是否等於m即可,因為圖如果聯通,且點數等於邊數,又無重邊和自環,則圖中一定有且只有一個環。

#include<cstdio>#include<cstring>#include<queue>#include<vector>#include <iostream>#include<algorithm>#include <map>#include <cmath>#include<set>using namespace std;typedef long long LL;const int maxn = 100 + 5;const int INF = 1000000000;vector<int> G[maxn];int vis[maxn];int ans;int sum;void dfs(int x){    vis[x] = 1;    sum++;    for(int i = 0;i < G[x].size();i++){        int to = G[x][i];        if(vis[to] == 0) dfs(to);    }}int main(){    int n,m;    while(cin >> n >> m){        for(int i = 0;i < n;i++) G[i].clear();        for(int i = 0;i < m;i++){            int x,y;            cin >> x >> y;            x--;y--;            G[x].push_back(y);            G[y].push_back(x);        }        ans = 1;        memset(vis,0,sizeof(vis));        sum = 0;        dfs(0);        if(sum != n) ans = 0;        if(n != m) ans = 0;        if(ans == 1) cout << "FHTAGN!\n";        else cout << "NO\n";    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.