[DFS search] codeforces beta round #80 (Div. 1 only) -- B. Cthulhu

Source: Internet
Author: User

Whether there are multiple trees in the question, and their roots form a ring. Simply put, it is to find whether there is only one ring in an undirected graph and the points are connected.

Method: The number of edges = points is the property of only one ring in the tree. If this condition is met, it cannot be determined because multiple ring fields may meet this condition. Therefore, DFS judges the connectivity.

#include <map>#include <set>#include <list>#include <queue>#include <deque>#include <stack>#include <string>#include <time.h>#include <cstdio>#include <math.h>#include <iomanip>#include <cstdlib>#include <limits.h>#include <string.h>#include <iostream>#include <fstream>#include <algorithm>using namespace std;#define LL long long#define MIN INT_MIN#define MAX INT_MAX#define PI acos(-1.0)#define FRE freopen ("input.txt","r",stdin)#define FF  freopen ("output.txt","w",stdout)#define eps 1e-8#define N 105int g[N][N];bool vis[N];int cnt;int n;void dfs(int x) {    int i,j;    vis[x] = 1;    cnt++;    for (i = 1; i <= n; i++) {        if (!vis[i] && g[x][i]) {            dfs(i);        }    }}int main(){    int m;    while(scanf("%d%d",&n,&m) != -1) {        int i,j,k;        int mm = m;        memset(vis,0,sizeof(vis));        while (m--) {            int a,b;            scanf("%d%d",&a,&b);            g[a][b] = g[b][a] = 1;        }        if (n == mm) {            cnt = 0;            dfs(1);            if(cnt == n) puts("FHTAGN!");            else puts("NO");        }        else puts("NO");    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.