text
stringlengths
291
465k
### Prompt Develop a solution in JAVA to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.InputMismatchException; public class C implements Runnable { private InputReader in; private PrintWriter out; private static final int MAX = 100+5; public static void main(String[] args) { new C().run(); } public C() { in = new InputReader(System.in); out = new PrintWriter(System.out); } boolean[][] matrix = new boolean[MAX][MAX]; boolean[] gray = new boolean[MAX]; boolean[] black = new boolean[MAX]; boolean[] conect = new boolean[MAX]; int N, M; private void flood(int s) { conect[s] = true; for (int i = 0; i < N; i++) if (matrix[s][i] && !conect[i]) flood(i); } private boolean isConnected() { flood(0); for (int i = 0; i < N; i++) if (!conect[i]) return false; return true; } private int visit(int s, int par) { if (black[s]) return -1; if (gray[s]) return s; gray[s] = true; for (int i = 0; i < N; i++) if (matrix[s][i] && i != par) { int r = visit(i, s); if (r != -1) return r; } gray[s] = false; black[s] = true; return -1; } /** Ends up with all elements in the loop being gray */ private int findLoop() { int s = -1; for (int i = 0; i < N; i++) { if (!black[i]) s = visit(i, i); if (s != -1) break; } if (s == -1) return s; Arrays.fill(gray, false); Arrays.fill(black, false); visit(s, s); return s; } /** Destructive */ private boolean isTree(int s) { black[s] = true; for (int i = 0; i < N; i++) { if (matrix[s][i]) { if (black[i]) return false; matrix[i][s] = false; if (!isTree(i)) return false; } } return true; } public void run() { N = in.readInt(); M = in.readInt(); for (int i = 0; i < M; i++) { int a = in.readInt()-1; int b = in.readInt()-1; matrix[a][b] = matrix[b][a] = true; } if (!isConnected()) { out.println("NO"); out.close(); return; } int s = findLoop(); if (s == -1) { out.println("NO"); out.close(); return; } Arrays.fill(black, false); for (int i = 0; i < N; i++) { if (gray[i]) { for (int j = 0; j < N; j++) { if (matrix[i][j] && !gray[j]) { matrix[j][i] = false; if (!isTree(j)) { //System.out.println(j + "isnt"); out.println("NO"); out.close(); return; } } } } } out.println("FHTAGN!"); out.close(); return; } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1000]; private int curChar, numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuffer buf = new StringBuffer(); int c = read(); while (c != '\n' && c != -1) { buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) return readLine(); else return readLine0(); } public BigInteger readBigInteger() { try { return new BigInteger(readString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } } } ```
### Prompt Construct a java code solution to the problem outlined: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class ABC { static int N, E, counter; static ArrayList<Integer>[] adjList; static Scanner sc; static PrintWriter out; static int[] parent; static boolean visited[]; static int[] dfs_low, dfs_num; public static void main(String[] args) throws IOException { sc = new Scanner(System.in); out = new PrintWriter(System.out); construct(); int cc = 0; for(int i = 0; i < N; i++) if(!visited[i]) { cc++; dfs(i); } if(cc == 1 && N == E) out.println("FHTAGN!"); else out.println("NO"); out.flush(); } static void dfs(int u) { visited[u] = true; for(int v : adjList[u]) if(!visited[v]) dfs(v); } static void construct()throws IOException { N = sc.nextInt(); E = sc.nextInt(); adjList = new ArrayList[N]; visited = new boolean[N]; parent = new int[N]; for(int i = 0; i < N; i++) adjList[i] = new ArrayList<>(); for(int i = 0; i < E; i++) { int from = sc.nextInt()-1, to = sc.nextInt()-1; adjList[from].add(to); adjList[to].add(from); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream System) { br = new BufferedReader(new InputStreamReader(System)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine()throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public char nextChar()throws IOException { return next().charAt(0); } public Long nextLong()throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() { for(long i = 0; i < 3e9; i++){} } } } ```
### Prompt Construct a cpp code solution to the problem outlined: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, a[100][100], vis[100], p[100]; void dfs(int x) { vis[x] = 1; for (int i = 0; i < n; i++) if (a[x][i]) { if (vis[i]) { if (p[x] != i) m++; } else { p[i] = x; dfs(i); } } } int main() { int x, y; cin >> n >> m; memset(p, -1, sizeof(p)); for (int i = 0; i < m; i++) { cin >> x >> y; x--; y--; a[x][y] = a[y][x] = 1; } m = 0; dfs(0); y = 0; for (int i = 0; i < n; i++) if (!vis[i]) { y = 1; break; } if (m != 2 || y) printf("NO"); else printf("FHTAGN!"); } ```
### Prompt Construct a cpp code solution to the problem outlined: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int table[105][105]; int used[105]; int N, M; int cycle = 0; int visit[105]; void DFS(int from, int s) { int i, j, k; for (i = 1; i <= N; i++) { if (table[s][i] && i != from && !visit[i]) { if (used[i]) { cycle++; } else { used[i] = 1; DFS(s, i); used[i] = 0; } } } visit[s] = 1; } int main() { int i, j, k; int temp1, temp2; cin >> N >> M; for (i = 0; i < M; i++) { cin >> temp1 >> temp2; table[temp1][temp2] = table[temp2][temp1] = 1; } int COM = 0; used[1] = 1; DFS(0, 1); for (i = 1; i <= N; i++) { if (visit[i]) COM++; } if (COM != N || cycle != 1) { cout << "NO"; } else { cout << "FHTAGN!"; } return 0; } ```
### Prompt Please formulate a cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T1> void deb(T1 e1) { cout << e1 << endl; } template <class T1, class T2> void deb(T1 e1, T2 e2) { cout << e1 << " " << e2 << endl; } template <class T1, class T2, class T3> void deb(T1 e1, T2 e2, T3 e3) { cout << e1 << " " << e2 << " " << e3 << endl; } const int maxn = 5000; vector<int> graph[maxn]; bool used[maxn]; int parent[maxn]; int nodes, edges, from, to, cc, cycle, flag = 0; bool Hascycle; void dfs(int u, int parent) { used[u] = 1; for (auto child : graph[u]) { if (used[child] == 0) { dfs(child, u); } else if (child != parent) { Hascycle = true; return; } } } int Rank[maxn], p[maxn]; vector<int> setSize; int numSets; struct DisjointSet { void make_set(int n) { numSets = n; setSize.assign(n + 1, 1); for (int i = 1; i <= n; i++) { Rank[i] = 0; p[i] = i; } } int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); } bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } void unionSet(int i, int j) { if (!isSameSet(i, j)) { numSets--; int x = findSet(i), y = findSet(j); if (Rank[x] > Rank[y]) { p[y] = x; setSize[x] += setSize[y]; } else { p[x] = y; setSize[y] += setSize[x]; if (Rank[x] == Rank[y]) { Rank[y]++; } } } } int numofdisjointset() { return numSets; } int sizeOfSet(int i) { return setSize[findSet(i)]; } } UF; int main() { scanf("%d %d", &nodes, &edges); UF.make_set(nodes); for (int i = 0; i < edges; i++) { scanf("%d %d", &from, &to); UF.unionSet(from, to); graph[from].push_back(to); graph[to].push_back(from); } memset(used, 0, sizeof(used)); for (int i = 1; i <= nodes; i++) { if (used[i] == 0) { cc++; dfs(i, -1); } } if (nodes == edges and UF.numofdisjointset() == 1) { cout << "FHTAGN!\n"; } else { cout << "NO\n"; } return 0; } ```
### Prompt Your task is to create a Java solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.ArrayList; import java.util.Scanner; public class Main{ static ArrayList<Integer> adjList[]; static int n, m; static boolean visited[]; public static void main(String args[]) { Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); if (m != n) { System.out.println("NO"); return; } visited = new boolean[n]; adjList = new ArrayList[n]; for (int i = 0; i < n; i++) adjList[i] = new ArrayList<Integer>(); for (int i = 0; i < m; i++) { int a = in.nextInt() - 1; int b = in.nextInt() - 1; adjList[a].add(b); adjList[b].add(a); } boolean flag = false; dfs(0); // for(int i=0;i<n;i++){ // if(dfs(i)){ // flag=true; // break; // } // visited=new boolean[n]; // } for (int i = 0; i < n; i++) { if (!visited[i]) { System.out.println("NO"); return; } } // if(!flag) System.out.println("FHTAGN!"); // else // System.out.println("NO"); } public static void dfs(int node) { // if(visited[node]) // return true; visited[node] = true; for (int i = 0; i < adjList[node].size(); i++) if (!visited[adjList[node].get(i)]) dfs(adjList[node].get(i)); // return false; } } ```
### Prompt Your challenge is to write a PYTHON solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```python import sys def read_values(): return map(int,raw_input().split()) n,m = read_values() a = [[False]*n for _ in range(n)] for _ in range(m): u,v = read_values() u-=1 v-=1 a[u][v] = True a[v][u] = True for k in range(n): for i in range(n): for j in range(n): a[i][j] |= a[i][k] and a[k][j] res = 'FHTAGN!' if m!=n: res = 'NO' for i in range(n): for j in range(n): if not a[i][j]: res = 'NO' print res ```
### Prompt Generate a CPP solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; void FastInputOutput() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void ReadFromFile(string name, string extention) { string fullPath = name + "." + extention; freopen(fullPath.c_str(), "r", stdin); } inline int D() { int t; scanf("%d", &t); return t; } inline long long LLD() { long long t; scanf("%lld", &t); return t; } inline char C() { char c; scanf("%c", &c); return c; } inline long long power(long long x, long long p, long long m) { long long res = 1; while (p) { if (p & 1) res = (res * x) % m; x = (x * x) % m; p >>= 1; } return res; } long double ModLog(long double base, long double x) { return (logl(x) / logl(base)); } int gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } const int N = 1e4 + 5; vector<int> adj[N]; int vst[N]; void DFS(int u) { vst[u] = 1; for (auto v : adj[u]) if (!vst[v]) DFS(v); } int main() { int n = D(), m = D(); for (int i = 0; i < m; ++i) { int u = D(), v = D(); adj[u].push_back(v); adj[v].push_back(u); } DFS(1); bool ans = n == m; for (int i = 1; i <= n && ans; ++i) ans &= vst[i]; puts(ans ? "FHTAGN!" : "NO"); return 0; } ```
### Prompt Please provide a Java coded solution to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class DvorkinKtylxy { static int[][] matr; static boolean[] versh; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = in.readLine(); StringTokenizer st = new StringTokenizer(str); int sizeV = Integer.parseInt(st.nextToken()); int sizeE = Integer.parseInt(st.nextToken()); if(sizeV != sizeE){ System.out.print("NO"); return; } int l,r; versh = new boolean[sizeV]; matr = new int[sizeV][sizeV]; for(int i=0;i<sizeE;i++){ str = in.readLine(); st = new StringTokenizer(str); l = Integer.parseInt(st.nextToken()); r = Integer.parseInt(st.nextToken()); matr[l-1][r-1] = 1; matr[r-1][l-1] = 1; } dfs(0); for(int i=0;i<versh.length;i++){ if (versh[i] == false) { System.out.print("NO"); return; } } System.out.print("FHTAGN!"); return; } public static void dfs(int v){ versh[v] = true; for(int i=0;i<matr[0].length;i++){ if(matr[v][i] == 1) if(versh[i] == false) dfs(i); } } } ```
### Prompt Create a solution in java for the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.*; import java.io.*; import java.math.BigInteger; public class Tests{ static Scanner in = new Scanner (System.in); static PrintWriter out = new PrintWriter (System.out); static ArrayList <Integer>[] adjList ; static boolean vis []; static int n , m , count=0; public static void main (String []args) { n = in.nextInt(); m = in.nextInt(); if(n!=m){ out.println("NO"); out.flush(); System.exit(0); } adjList = new ArrayList[n]; vis=new boolean[n]; for(int i = 0 ; i<n ; i++){ adjList[i]=new ArrayList <Integer>(); } for(int i = 0 ; i<n ; i++){ int u = in.nextInt()-1; int v = in.nextInt()-1; adjList[u].add(v); adjList[v].add(u); } CC(); if(count == 1){ out.println("FHTAGN!"); }else{ out.println("NO"); } out.flush(); out.close(); } static void dfs (int v){ vis[v]=true; for(int u : adjList[v]){ if(! vis[u]){ dfs(u); } } } static void CC (){ for(int i = 0 ; i<n ; i++){ if(! vis[i]){ dfs(i); count++; } } //out.println(count); //out.close(); } } ```
### Prompt Please create a solution in CPP to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct FindCycle { private: int n; bool vs[100 + 5]; int prv[100 + 5]; vector<int> adj[100 + 5]; vector<int> cycle; bool found; public: FindCycle(vector<int> _adj[], int _n) { found = false; n = _n; for (int _b = (n), i = (1); i <= _b; i++) { adj[i] = _adj[i]; } memset(vs, false, sizeof(vs)); } vector<int> getCycle() { if (found) return cycle; for (int _b = (n), ii = (1); ii <= _b; ii++) prv[ii] = -1; startFindFrom(1); return cycle; } void startFindFrom(int x) { for (int i = 0, _a = (adj[x].size()); i < _a; i++) { if (found) return; int cur = adj[x][i]; if (!vs[cur]) { int save = prv[cur]; prv[cur] = x; vs[cur] = true; startFindFrom(cur); vs[cur] = false; prv[cur] = save; } else if (vs[cur] && prv[x] != cur && prv[x] != -1) { cycle.clear(); prv[cur] = -1; cur = x; found = true; while (cur != -1) { cycle.push_back(cur); cur = prv[cur]; } return; } } } }; struct P103B { private: int n, m; vector<int> adj[100 + 5]; bool con[100 + 5][100 + 5]; int r[100 + 5]; set<int> c; void readFromInput() { scanf("%d %d", &n, &m); int a, b; memset(con, false, sizeof(con)); for (int i = 0, _a = (m); i < _a; i++) { scanf("%d %d", &a, &b); con[a][b] = true; adj[a].push_back(b); adj[b].push_back(a); } } int getRoot(int x) { return x == r[x] ? x : r[x] = getRoot(r[x]); } bool unite(int a, int b) { int ra = getRoot(a), rb = getRoot(b); if (ra == rb) return false; r[ra] = rb; return true; } public: void solve() { readFromInput(); FindCycle f(adj, n); vector<int> cycle = f.getCycle(); if (cycle.empty()) { puts("NO"); return; } for (int _b = (n), i = (1); i <= _b; i++) r[i] = i; for (int i = 0, _a = (cycle.size()); i < _a; i++) { c.insert(cycle[i]); r[cycle[i]] = cycle[0]; } for (int i = 0; i < cycle.size(); ++i) { int u = cycle[i], v = (i == cycle.size() - 1 ? cycle[0] : cycle[i + 1]); con[u][v] = false, con[v][u] = false; } for (int i = 0, _a = (cycle.size()); i < _a; i++) for (int j = 0, _a = (cycle.size()); j < _a; j++) { if (con[cycle[i]][cycle[j]]) { puts("NO"); return; } } for (int _b = (n), i = (1); i <= _b; i++) { for (int _b = (n), j = (1); j <= _b; j++) { if (con[i][j]) { if (!unite(i, j)) { puts("NO"); return; } } } } for (int _b = (n), i = (1); i <= _b; i++) { if ((adj[i].size() == 0) && c.count(i) == 0) { puts("NO"); return; } } puts("FHTAGN!"); } }; int main() { P103B p; p.solve(); } ```
### Prompt Your challenge is to write a Java solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.StringTokenizer; /** * * @author Prateep */ public class JavaApplication2 { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB{ ArrayList<Integer> list[]; boolean[] isVisited; int total,count; public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException{ int n=in.nextInt(),m=in.nextInt(); if(n!=m){ out.println("NO"); return; } list=new ArrayList[n+1]; isVisited=new boolean[n+1]; for(int i=0;i<n+1;i++) list[i]=new ArrayList<>(); for(int i=0;i<m;i++){ int u=in.nextInt(); int v=in.nextInt(); list[u].add(v); list[v].add(u); } dfs(1); if(total==n)out.println("FHTAGN!"); else out.println("NO"); } void dfs(int n){ total++; isVisited[n]=true; for(int i=0;i<list[n].size();i++) if(!isVisited[list[n].get(i)])dfs(list[n].get(i)); } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; private BufferedReader br; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextWhole() throws IOException{ br=new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } public String nextLine(){ try{ return reader.readLine(); }catch (IOException e){ throw new RuntimeException(e); } } } ```
### Prompt Please provide a java coded solution to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner r = new Scanner(System.in); int n = r.nextInt(); int m = r.nextInt(); boolean[][] g = new boolean[n][n]; for(int k = 0; k < m; k++){ int i = r.nextInt()-1; int j = r.nextInt()-1; g[i][j] = g[j][i] = true; } boolean[] v = new boolean[n]; int cnt = 0; for(int i = 0; i < n; i++)if(!v[i]){ v[i] = true; cnt++; dfs(i, v, g, n); } if(cnt != 1){ System.out.println("NO"); System.exit(0); } // for(boolean[] i:g) // System.out.println(Arrays.toString(i)); v = new boolean[n]; v[0] = true; int cycles = mdfs(-1, 0, v, g, n); // System.out.println(cycles); if(cycles == 2)System.out.println("FHTAGN!"); else System.out.println("NO"); } private static int mdfs(int p, int i, boolean[] v, boolean[][] g, int n) { int res = 0; for(int j = 0; j < n; j++){ if(g[i][j]){ // System.out.println(i+", "+j); if(v[j] && j != p)res++; if(!v[j]){ v[j] = true; res += mdfs(i, j, v, g, n); } // break; } } return res; } private static void dfs(int i, boolean[] v, boolean[][] g, int n) { for(int j = 0; j < n; j++)if(!v[j] && g[i][j]){ v[j] = true; dfs(j, v, g, n); } } } ```
### Prompt Your task is to create a Python solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```python R = lambda: map(int, raw_input().split()) n, m = R() if m != n: print 'NO' exit() matrix = [[0]*n for _ in range(n)] for i in range(m): x, y = R() x, y = x-1, y-1 matrix[x][y] = 1 matrix[y][x] = 1 v = [0]*n m = matrix def dfs(i): for j in range(n): if v[j]: continue if m[i][j]: v[j] = 1 dfs(j) dfs(0) for i in range(n): if not v[i]: print 'NO' break else: print 'FHTAGN!' ```
### Prompt Your task is to create a CPP solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[102][102], reach[102], n; void dfs(int v) { int i; reach[v] = 1; for (i = 1; i <= n; i++) if (a[v][i] && !reach[i]) { dfs(i); } } int main() { int i, j, count = 0; int c, b, m; scanf("%d%d", &n, &m); for (i = 0; i <= n; i++) for (j = 0; j <= n; j++) { reach[i] = 0; a[i][j] = 0; } for (i = 1; i <= m; i++) { cin >> c >> b; a[c][b] = 1; a[b][c] = 1; } dfs(1); for (i = 1; i <= n; i++) if (reach[i]) count++; if (count == n && n == m) cout << "FHTAGN!"; else cout << "NO"; return (0); } ```
### Prompt Construct a CPP code solution to the problem outlined: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m; vector<int> uf; void init_uf() { fill(uf.begin(), uf.end(), -1); } int find(int x) { if (uf[x] < 0) { return x; } return uf[x] = find(uf[x]); } void join(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } if (uf[x] > uf[y]) { swap(x, y); } uf[x] += uf[y]; uf[y] = x; } int main() { cin >> n >> m; if (n != m) { cout << "NO" << "\n"; return 0; } uf.resize(n + 1); init_uf(); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; join(a, b); } for (int i = 1; i <= n; i++) { int a = find(1); if (a != find(i)) { cout << "NO" << "\n"; return 0; } } cout << "FHTAGN!" << "\n"; return 0; } ```
### Prompt Please formulate a JAVA solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.*; import java.util.*; import java.math.*; public class Main { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer=null; int n,m; int adjMat[][]; hello ver[]; int cycle[]; int cycles = 0; public static void main(String[] args) throws IOException { new Main().execute(); } void debug(Object...os) { System.out.println(Arrays.deepToString(os)); } int ni() throws IOException { return Integer.parseInt(ns()); } long nl() throws IOException { return Long.parseLong(ns()); } double nd() throws IOException { return Double.parseDouble(ns()); } String ns() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(br.readLine()); return tokenizer.nextToken(); } String nline() throws IOException { tokenizer=null; return br.readLine(); } //Main Code starts Here int totalCases, testNum; void execute() throws IOException { totalCases = 1; for(testNum = 1; testNum <= totalCases; testNum++) { if(!input()) break; solve(); } } void solve() { ver = new hello [n+1]; //debug(adjMat); initialize(); cycle = new int[n]; bfs(1,0); //debug(ver[1].visited,ver[1].parent); //debug(cycle); //debug(cycles); boolean carryOn = true; for(int i = 1;i<n+1;i++) { if(ver[i].visited == 0) carryOn = false; } if(cycles/2 == 1 && carryOn) System.out.println("FHTAGN!"); else System.out.println("NO"); } void initialize() { for(int i = 0;i<n+1;i++) { ver[i] = new hello(); } } void bfs(int v,int par) { //System.out.println("bfs "+v+" "+par); if(ver[v].visited == 0) { ver[v].visited = 1; ver[v].parent = par; for(int i = 1;i<=n;i++) { if(adjMat[v][i] == 1 && i!=par) { bfs(i,v); } } } else if(ver[v].visited == 1) { //ver[v].visited = 2; if(cycles == 0) { cycles++; int ind = 0; while(par!=v) { cycle[ind] = par; ind++; par = ver[par].parent; } cycle[ind] = par; } else { cycles++; } } } boolean input() throws IOException { n=ni(); m=ni(); adjMat = new int[n+1][n+1]; for(int i = 1;i<=m;i++) { //System.out.println("Entered"); int a = ni(); int b = ni(); adjMat[a][b] = 1; adjMat[b][a] = 1; } return true; } public class hello { int visited = 0; int parent = 0; } } ```
### Prompt Your task is to create a Cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m; bool visit[105]; vector<int> Adj[105]; void dfs(int s) { visit[s] = true; for (int i = 0; i < Adj[s].size(); i++) { int to = Adj[s][i]; if (to != s && !visit[to]) dfs(to); } } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; Adj[x].push_back(y); Adj[y].push_back(x); } int ct = 0; for (int i = 1; i <= n; i++) { if (!visit[i]) { dfs(i); ct++; } } if (ct == 1 && n == m) cout << "FHTAGN!" << endl; else cout << "NO" << endl; } ```
### Prompt Generate a CPP solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using P = pair<long long, long long>; using WeightedGraph = vector<vector<P>>; using UnWeightedGraph = vector<vector<long long>>; using Real = long double; using Point = complex<Real>; using Vector2d = complex<Real>; const long long INF = 1LL << 60; const long long MOD = 1000000007; const double EPS = 1e-15; const double PI = 3.14159265358979323846; template <typename T> long long getIndexOfLowerBound(vector<T> &v, T x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } template <typename T> long long getIndexOfUpperBound(vector<T> &v, T x) { return upper_bound(v.begin(), v.end(), x) - v.begin(); } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } istream &operator>>(istream &is, Point &p) { Real a, b; is >> a >> b; p = Point(a, b); return is; } template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &p_var) { is >> p_var.first >> p_var.second; return is; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { cerr << '{'; os << pair_var.first; cerr << ','; os << " " << pair_var.second; cerr << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { cerr << '['; for (long long i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : " "); cerr << ']'; return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &df) { for (auto &vec : df) os << vec; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { cerr << "{"; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << *itr; itr++; if (itr != map_var.end()) cerr << ", "; itr--; } cerr << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { cerr << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; itr++; if (itr != set_var.end()) cerr << ", "; itr--; } cerr << "}"; return os; } void print() { cout << endl; } template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; print(forward<Tail>(tail)...); } void dump_func() { cerr << '#' << endl; } template <typename Head, typename... Tail> void dump_func(Head &&head, Tail &&...tail) { cerr << head; if (sizeof...(Tail) > 0) cerr << ", "; dump_func(std::move(tail)...); } signed main(void) { cin.tie(0); ios::sync_with_stdio(false); long long n, m; cin >> n >> m; UnWeightedGraph g(n); for (long long(i) = 0; (i) < (m); (i)++) { long long a, b; cin >> a >> b; a--, b--; g[a].push_back(b), g[b].push_back(a); } queue<long long> q; q.push(0); vector<long long> v(n); while (!q.empty()) { long long now = q.front(); q.pop(); v[now] = 1; for (auto e : g[now]) { if (!v[e]) q.push(e); } } vector<long long> deg(n); for (long long(i) = 0; (i) < (n); (i)++) { deg[i] = g[i].size(); } if (count(v.begin(), v.end(), 1) != n) { print("NO"); return 0; } bool ok = true; while (ok) { ok = false; for (long long(i) = 0; (i) < (n); (i)++) { if (deg[i] == 1) { long long to = g[i][0]; g[i].erase(g[i].begin()); for (long long(j) = 0; (j) < (g[to].size()); (j)++) { if (g[to][j] == i) { g[to].erase(j + g[to].begin()); break; } } ok = true; deg[i]--, deg[to]--; break; } } } if (accumulate(deg.begin(), deg.end(), 0LL) == 0) { print("NO"); return 0; } for (long long(i) = 0; (i) < (n); (i)++) { if (deg[i] != 0 && deg[i] != 2) { print("NO"); return 0; } } print("FHTAGN!"); } ```
### Prompt Your challenge is to write a JAVA solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.Scanner; public class CF103b { private void run() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); if(n != m) { //木構造ならn = m-1。一箇所連結していればn=mとなる。 System.out.println("NO"); return; } boolean[][] e = new boolean[n][n]; for (int i = 0; i < n; i++) { e[i][i] = true; } for (int i = 0; i < m; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; e[a][b] = e[b][a] = true; } for (int k = 0; k < e.length; k++) { for (int i = 0; i < e.length; i++) { for (int j = 0; j < e.length; j++) { e[i][j] |= e[i][k] && e[k][j]; } } } for (int i = 0; i < e.length; i++) { for (int j = 0; j < e.length; j++) { if(!e[i][j]) { System.out.println("NO"); return; } } } System.out.println("FHTAGN!"); sc.close(); } public static void main(String args[]) { new CF103b().run(); } } ```
### Prompt Your challenge is to write a CPP solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 100010; int n, m, k, f[N]; vector<int> v[N]; void dfs(int c) { f[c] = true; k++; for (auto i : v[c]) if (!f[i]) dfs(i); } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) { int x, y; scanf("%d %d", &x, &y); v[x].push_back(y); v[y].push_back(x); } dfs(1); if (k != n || m != n) printf("NO"); else printf("FHTAGN!"); return 0; } ```
### Prompt Construct a JAVA code solution to the problem outlined: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.ArrayList; import java.util.Scanner; import java.util.Stack; //인접 리스트로 표현.. public class B103 { static int n = 0; static int m = 0; static ArrayList<ArrayList<Integer>> grape = new ArrayList<>(); static Stack st = new Stack(); static int[] visit = null; public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); if(n!=m){ System.out.println("NO"); return; } visit = new int[n+1]; for(int i=0;i<n+1;i++) //안에 담을 리스트 초기화(0번째는 아예 사용x) grape.add(new ArrayList<>()); while(n-- >0){ int a = sc.nextInt(); int b = sc.nextInt(); grape.get(a).add(b); grape.get(b).add(a); } Dfs(); } public static void Dfs(){ st.add(1); while(!st.isEmpty()){ int now = (int)st.pop(); visit[now]++; if(!grape.contains(now)){ //뭔가 있다면 for(int i=0;i<grape.get(now).size();i++){ if(visit[grape.get(now).get(i)] == 0) //이미 팝한게 아니라면 st.add(grape.get(now).get(i)); //다 넣어주고 } } } int count = 0; for(int i=1;i<visit.length;i++){ if(visit[i]==2){ count++; } if(visit[i]==0) { System.out.println("NO"); return; } } if(count==1){ System.out.println("FHTAGN!"); return; } System.out.println("NO"); } } ```
### Prompt Please formulate a java solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.Scanner; public class Cthulhu { static int numOfCycles = 0; static int[] parents; public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int nodes = input.nextInt(); int edges = input.nextInt(); int[] connection = new int[2 * edges + 1]; parents = new int[nodes + 1]; for (int i = 1; i <= 2 * edges; i++) { connection[i] = input.nextInt(); } for (int i = 1; i <= nodes; i++) { parents[i] = i; } for (int i = 1; i <= 2 * edges; i += 2) { merge(connection[i], connection[i + 1]); } if (numOfCycles == 1 && edges >= nodes) { System.out.println("FHTAGN!"); } else { System.out.println("NO"); } } static void merge(int node1, int node2) { int parent1 = find(node1); int parent2 = find(node2); if (parent1 == parent2) { numOfCycles++; } parents[parent2] = parent1; } static int find(int node) { if (parents[node] == node) { return node; } else { return parents[node] = find(parents[node]); } } } ```
### Prompt Please provide a JAVA coded solution to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.*; import java.math.BigInteger; import java.util.*; public class Main implements Runnable { // leave empty to read from stdin/stdout private static final String TASK_NAME_FOR_IO = ""; // file names private static final String FILE_IN = TASK_NAME_FOR_IO + ".in"; private static final String FILE_OUT = TASK_NAME_FOR_IO + ".out"; BufferedReader in; PrintWriter out; StringTokenizer tokenizer = new StringTokenizer(""); public static void main(String[] args) { new Thread(new Main()).start(); } int n, m, cycles = 0; List<Integer> []g; int []used; private void solve() throws IOException { n = nextInt(); m = nextInt(); g = new LinkedList[n]; for(int i = 0; i < n; ++i) g[i] = new LinkedList<Integer>(); for(int i = 0; i < m; ++i) { int u = nextInt()-1, v = nextInt()-1; g[v].add(u); g[u].add(v); } used = new int[n]; int comp = 0; for(int i = 0; i < n; ++i) { if (used[i] == 0) { dfs(i, -1); ++comp; } } if (comp > 1) { out.println("NO"); }else { if (cycles == 1) { out.println("FHTAGN!"); }else { out.println("NO"); } } } private void dfs(int x, int p) { used[x] = 1; for(int i : g[x]) { if (i == p) continue; if (used[i] == 0) { dfs(i, x); }else if (used[i] == 1) { ++cycles; } } used[x] = 2; } public void run() { long timeStart = System.currentTimeMillis(); boolean fileIO = TASK_NAME_FOR_IO.length() > 0; try { if (fileIO) { in = new BufferedReader(new FileReader(FILE_IN)); out = new PrintWriter(new FileWriter(FILE_OUT)); } else { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); } solve(); in.close(); out.close(); } catch (IOException e) { throw new IllegalStateException(e); } long timeEnd = System.currentTimeMillis(); if (fileIO) { System.out.println("Time spent: " + (timeEnd - timeStart) + " ms"); } } private String nextToken() throws IOException { while (!tokenizer.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } tokenizer = new StringTokenizer(line); } return tokenizer.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private BigInteger nextBigInt() throws IOException { return new BigInteger(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } } ```
### Prompt Please provide a python3 coded solution to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```python3 inp = input().split() n = int(inp[0]) m = int(inp[1]) def dfs(x): f.add(x) for y in e[x]: if not y in f: dfs(y) if n >= 3 and n == m: e, f = [[] for i in range(n + 1)], set() for j in range(m): x, y = map(int, input().split()) e[x].append(y) e[y].append(x) dfs(1) print('FHTAGN!' if len(f) == n else 'NO') else: print('NO') ```
### Prompt Develop a solution in CPP to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> const int INF = 0x3f3f3f3f; using namespace std; bool bfs(vector<vector<int> >& graph, vector<int>& usados, int atual, int tamanho) { bool temloop = false; vector<int> pai(tamanho, -1); queue<int> filona; int aux = 0; filona.push(atual); while (!filona.empty()) { int at = filona.front(); usados[at] = 1; filona.pop(); for (int i = 0; i < tamanho; i++) { if (graph[at][i] == 1 and usados[i] == 0) { filona.push(i); pai[i] = at; } else if (graph[at][i] == 1 and usados[i] == 1 and pai[at] != i) { temloop = true; aux++; } } } if (aux > 2) temloop = false; return temloop; } int main() { int n, m; cin >> n >> m; vector<vector<int> > graph(n, vector<int>(n, 0)); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; graph[x - 1][y - 1] = 1; graph[y - 1][x - 1] = 1; } vector<int> usados(n, 0); bool temciclo = bfs(graph, usados, n - 1, n); for (int i = 0; i < n; i++) if (usados[i] == 0) { cout << "NO" << "\n"; return 0; } if (temciclo) cout << "FHTAGN!" << "\n"; else cout << "NO" << "\n"; return 0; } ```
### Prompt Construct a PYTHON code solution to the problem outlined: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```python def ktulu(): verts, edges = map(int, raw_input().split()) if verts != edges: return 'NO' comps = [[i] for i in range(verts)] cycles = 0 for e in range(edges): v1, v2 = map(lambda x: int(x) - 1, raw_input().split()) i1, = [i for i in range(len(comps)) if v1 in comps[i]] i2, = [i for i in range(len(comps)) if v2 in comps[i]] if i1 == i2: if cycles > 0: return 'NO' else: cycles += 1 else: comps[i1] += comps[i2] del comps[i2] return 'FHTAGN!' print ktulu() ```
### Prompt Develop a solution in cpp to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int parent[101]; int ssize[101]; int find_set(int vector) { if (vector == parent[vector]) return vector; return parent[vector] = find_set(parent[vector]); } void make_set(int vector) { parent[vector] = vector; ssize[vector] = 1; } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (ssize[a] < ssize[b]) swap(a, b); parent[b] = a; ssize[a] += ssize[b]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; bool flag = false; for (int i = 1; i <= n; i++) make_set(i); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; union_sets(a, b); } set<int> s; for (int i = 1; i <= n; i++) { s.insert(find_set(i)); } if (m == n && s.size() == 1) cout << "FHTAGN!" << endl; else { cout << "NO" << endl; } return 0; } ```
### Prompt In CPP, your task is to solve the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> graph[10000]; bool visited[10000]; int nodes = 1; void dfs(int x) { visited[x] = true; for (int i = 0; i < graph[x].size(); i++) { if (visited[graph[x][i]]) continue; nodes++; dfs(graph[x][i]); } } int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; graph[x].push_back(y); graph[y].push_back(x); } for (int i = 1; i <= n; i++) if (!visited[i]) dfs(i); if (nodes == m && nodes >= n) cout << "FHTAGN!"; else cout << "NO"; return 0; } ```
### Prompt Please provide a JAVA coded solution to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Cthulhu { static boolean isCthulhu(int n, int m, List<Integer>[] edges) { if (n < 3 || n != m) { return false; } boolean[] visited = new boolean[edges.length]; dfs(0, visited, edges); for (boolean b : visited) { if (!b) { return false; } } return true; } private static void dfs(int i, boolean[] visited, List<Integer>[] edges) { visited[i] = true; for (int v : edges[i]) { if (!visited[v]) { dfs(v, visited, edges); } } } public static void main(String[] args) throws IOException { int n, m; try (Scanner scanner = new Scanner(System.in)) { n = scanner.nextInt(); m = scanner.nextInt(); @SuppressWarnings("unchecked") List<Integer>[] edges = new List[n]; for (int i = 0; i < edges.length; i++) { edges[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = scanner.nextInt(); int v = scanner.nextInt(); edges[u - 1].add(v - 1); edges[v - 1].add(u - 1); } System.out.println(isCthulhu(n, m, edges) ? "FHTAGN!" : "NO"); } } } ```
### Prompt Your challenge is to write a java solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import static java.util.Arrays.*; import static java.lang.Math.*; import static java.math.BigInteger.*; import java.util.*; import java.math.*; import java.io.*; public class B implements Runnable { String file = "input"; boolean TEST = false; void solve() throws IOException { n = nextInt(); m = nextInt(); a = new boolean[n + 1][n + 1]; adj = new ArrayList[n + 1]; for(int i = 1; i <= n; i++) adj[i] = new ArrayList<Integer>(); for(int i = 0; i < m; i++) { int u = nextInt(), v = nextInt(); a[u][v] = a[v][u] = true; adj[u].add(v); adj[v].add(u); } boolean ok = false; outer: for(int u = 1; u <= n; u++) for(int v = u + 1; v <= n; v++) { boolean[] vs = new boolean[n + 1]; ArrayList<Integer> as = new ArrayList<Integer>(); if(DFS(u, v, vs, as, new boolean[n + 1])) { vs[u] = false; vs[v] = false; if(DFS(v, u, vs, as, new boolean[n + 1])) { if(as.size() <= 2) continue; //print(as); Integer[] o = as.toArray(new Integer[0]); for(int j = 0; j < o.length; j++) a[o[j]][o[(j + 1) % o.length]] = a[o[(j + 1) % o.length]][o[j]] = false; boolean[] visi = new boolean[n + 1]; for(int j = 0; j < o.length; j++) if(visi[o[j]]) break outer; else { //print(o[j]); if(tree(o[j], -1, visi)) continue; else break outer; } for(int i = 1; i <= n; i++) if(!visi[i]) break outer; ok = true; break outer; } } } if(ok) out.println("FHTAGN!"); else out.println("NO"); } boolean[][] a; ArrayList<Integer>[] adj; int n, m; boolean tree(int u, int prev, boolean[] vs) { //print("VS ", u); vs[u] = true; for(int v = 1; v <= n; v++) { if(!a[u][v]) continue; if(v == prev) continue; if(vs[v]) return false; tree(v, u, vs); } return true; } boolean DFS(int u, int v, boolean[] vs, ArrayList<Integer> as, boolean[] done) { done[u] = true; if(u == v) return true; for(int i : adj[u]) if(!done[i] && !vs[i]) { if(DFS(i, v, vs, as, done)) { vs[i] = true; as.add(i); return true; } } return false; } String next() throws IOException { while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } void print(Object... o) { System.out.println(deepToString(o)); } void gcj(Object o) { String s = String.valueOf(o); out.println("Case #" + test + ": " + s); System.out.println("Case #" + test + ": " + s); } BufferedReader input; PrintWriter out; StringTokenizer st; int test; void init() throws IOException { if(TEST) input = new BufferedReader(new FileReader(file + ".in")); else input = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedOutputStream(System.out)); } public static void main(String[] args) throws IOException { new Thread(null, new B(), "", 1 << 20).start(); } public void run() { try { init(); if(TEST) { int runs = nextInt(); for(int i = 0; i < runs; i++) solve(); } else solve(); out.close(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } } ```
### Prompt Please provide a Java coded solution to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.BufferedInputStream; import java.io.IOException; import java.io.PrintWriter; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.InputMismatchException; import java.util.Iterator; import java.util.LinkedList; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; import java.util.Set; import java.util.SortedSet; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; public class Main { /********************************************** a list of common variables **********************************************/ private MyScanner scan = new MyScanner(); private PrintWriter out = new PrintWriter(System.out); private final double PI = Math.PI; private final int INF = (int)(1e9); private final double EPS = 1e-6; private final int SIZEN = (int)(1e5); private final int MOD = (int)(1e9 + 7); private final long MODH = 10000000007L, BASE = 10007; private final int[] DX = {0, 1, 0, -1}, DY = {-1, 0, 1, 0}; private int n; private boolean[][] map; private boolean[] vis; public void dfs(int u) { vis[u] = true; for(int i = 1;i <= n;++i) { if(map[u][i] && !vis[i]) dfs(i); } } public void foo() { n = scan.nextInt(); int m = scan.nextInt(); map = new boolean[n + 1][n + 1]; for(int i = 0;i < m;++i) { int u = scan.nextInt(); int v = scan.nextInt(); map[u][v] = map[v][u] = true; } vis = new boolean[n + 1]; dfs(1); for(int i = 1;i <= n;++i) { if(!vis[i]) { out.println("NO"); return; } } out.println(m == n ? "FHTAGN!" : "NO"); } public static void main(String[] args) { Main m = new Main(); m.foo(); m.out.close(); } /********************************************** a list of common algorithms **********************************************/ /** * 1---Get greatest common divisor * @param a : first number * @param b : second number * @return greatest common divisor */ public long gcd(long a, long b) { return 0 == b ? a : gcd(b, a % b); } /** * 2---Get the distance from a point to a line * @param x1 the x coordinate of one endpoint of the line * @param y1 the y coordinate of one endpoint of the line * @param x2 the x coordinate of the other endpoint of the line * @param y2 the y coordinate of the other endpoint of the line * @param x the x coordinate of the point * @param y the x coordinate of the point * @return the distance from a point to a line */ public double getDist(long x1, long y1, long x2, long y2, long x, long y) { long a = y2 - y1; long b = x1 - x2; long c = y1 * (x2 - x1) - x1 * (y2 - y1); return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b); } /** * 3---Get the distance from one point to a segment (not a line) * @param x1 the x coordinate of one endpoint of the segment * @param y1 the y coordinate of one endpoint of the segment * @param x2 the x coordinate of the other endpoint of the segment * @param y2 the y coordinate of the other endpoint of the segment * @param x the x coordinate of the point * @param y the y coordinate of the point * @return the distance from one point to a segment (not a line) */ public double ptToSeg(long x1, long y1, long x2, long y2, long x, long y) { double cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1); if(cross <= 0) { return (x - x1) * (x - x1) + (y - y1) * (y - y1); } double d = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1); if(cross >= d) { return (x - x2) * (x - x2) + (y - y2) * (y - y2); } double r = cross / d; double px = x1 + (x2 - x1) * r; double py = y1 + (y2 - y1) * r; return (x - px) * (x - px) + (y - py) * (y - py); } /** * 4---KMP match, i.e. kmpMatch("abcd", "bcd") = 1, kmpMatch("abcd", "bfcd") = -1. * @param t: String to match. * @param p: String to be matched. * @return if can match, first index; otherwise -1. */ public int kmpMatch(char[] t, char[] p) { int n = t.length; int m = p.length; int[] next = new int[m + 1]; next[0] = -1; int j = -1; for(int i = 1;i < m;++i) { while(j >= 0 && p[i] != p[j + 1]) { j = next[j]; } if(p[i] == p[j + 1]) { ++j; } next[i] = j; } j = -1; for(int i = 0;i < n;++i) { while(j >= 0 && t[i] != p[j + 1]) { j = next[j]; } if(t[i] == p[j + 1]) { ++j; } if(j == m - 1) { return i - m + 1; } } return -1; } /** * 5---Get the hash code of a String * @param s: input string * @return hash code */ public long hash(String s) { long key = 0, t = 1; for(int i = 0;i < s.length();++i) { key = (key + s.charAt(i) * t) % MODH; t = t * BASE % MODH; } return key; } /** * 6---Get x ^ n % MOD quickly. * @param x: base * @param n: times * @return x ^ n % MOD */ public long quickMod(long x, long n) { long ans = 1; while(n > 0) { if(1 == n % 2) { ans = ans * x % MOD; } x = x * x % MOD; n >>= 1; } return ans; } /** * 7---judge if a point is located inside a polygon * @param x0 the x coordinate of the point * @param y0 the y coordinate of the point * @return true if it is inside the polygon, otherwise false */ /*public boolean contains(double x0, double y0) { int cross = 0; for(int i = 0;i < n;++i) { double s = x[i + 1] == x[i] ? 100000000 : (double)(y[i + 1] - y[i]) / (x[i + 1] - x[i]); boolean b1 = x[i] <= x0 && x0 < x[i + 1]; boolean b2 = x[i + 1] <= x0 && x0 < x[i]; boolean b3 = y0 < s * (x0 - x[i]) + y[i]; if((b1 || b2) && b3) ++cross; } return cross % 2 != 0; }*/ /** * 8---judge if a point is located on the segment * @param x1 the x coordinate of one point of the segment * @param y1 the y coordinate of one point of the segment * @param x2 the x coordinate of another point of the segment * @param y2 the y coordinate of another point of the segment * @param x the x coordinate of the point * @param y the y coordinate of the point * @return true if it is located on the segment, otherwise false */ public boolean isOnSeg(long x1, long y1, long x2, long y2, long x, long y) { return (x - x1) * (y2 - y1) == (x2 - x1) * (y - y1) && x >= Math.min(x1, x2) && x <= Math.max(x1, x2) && y >= Math.min(y1, y2) && y <= Math.max(y1, y2); } /** * 9---get the cross product * @param p1 point A * @param p2 point B * @param p point O * @return cross product of OA x OB */ /*public long cross(Point p1, Point p2, Point p) { return (long)(p1.x - p.x) * (p2.y - p.y) - (long)(p2.x - p.x) * (p1.y - p.y); }*/ /** * 10---implement topsort and tell if it is possible * @return true if it is possible to implement topsort, otherwise false */ /*public boolean topsort() { Queue<Integer> q = new LinkedList<Integer>(); StringBuilder ans = new StringBuilder(); int[] in = new int[26]; for(int i = 0;i < 26;++i) { if(0 == in[i]) { ans.append((char)('a' + i)); q.add(i); } } while(!q.isEmpty()) { int u = q.poll(); for(int i = 0;i < 26;++i) { if(map[u][i]) { --in[i]; if(0 == in[i]) { ans.append((char)('a' + i)); q.add(i); } } } } return 26 == ans.length(); }*/ class MyScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; BufferedInputStream bis = new BufferedInputStream(System.in); public int read() { if (-1 == numChars) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = bis.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c & 15; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c & 15) * m; c = read(); } } return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c; } } } ```
### Prompt Create a solution in cpp for the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> adj[105]; vector<pair<int, int>> edge; bool mark[105]; void dfs(int x, int dx, int dy) { mark[x] = true; for (int i = 0; i < adj[x].size(); i++) { int y = adj[x][i]; if (mark[y]) continue; if (x == dx && y == dy) continue; if (x == dy && y == dx) continue; dfs(y, dx, dy); } } int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; edge.push_back(make_pair(x, y)); adj[x].push_back(y); adj[y].push_back(x); } if (n != m || n < 3) { cout << "NO"; return 0; } for (int i = 0; i < m; i++) { int x = edge[i].first; int y = edge[i].second; memset(mark, 0, sizeof(mark)); dfs(x, x, y); bool flag = true; for (int i = 1; i <= n; i++) flag &= mark[i]; if (flag) { cout << "FHTAGN!" << endl; return 0; } } cout << "NO"; } ```
### Prompt Your task is to create a cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long double eps = 1e-9; vector<vector<int> > g; vector<int> state; vector<int> st; vector<int> cyc; set<pair<int, int> > forbid; int cnt; void dfs(int u, int pr = -1) { cnt++; state[u] = 1; st.push_back(u); for (int i = 0; i < g[u].size(); ++i) if (g[u][i] != pr && !forbid.count(make_pair(u, g[u][i]))) { int v = g[u][i]; if (state[v] == 0) dfs(v, u); else if (state[v] == 1 && cyc.size() == 0) { int j = st.size() - 1; while (st[j] != v) cyc.push_back(st[j]), --j; cyc.push_back(v); } } state[u] = 2; st.pop_back(); } int n; int main() { cin >> n; int m; cin >> m; g.resize(n); for (int i = 0; i < m; ++i) { int u, v; scanf("%d%d", &u, &v); u--; v--; g[u].push_back(v); g[v].push_back(u); } state.resize(n); dfs(0); if (count(state.begin(), state.end(), 0) > 0 || cyc.size() == 0) { cout << "NO" << endl; return 0; } if (n != m) { cout << "NO" << endl; } else { cout << "FHTAGN!" << endl; } return 0; for (int i = 0; i < cyc.size(); ++i) { int j = (i + 1) % cyc.size(); forbid.insert(make_pair(cyc[i], cyc[j])); forbid.insert(make_pair(cyc[j], cyc[i])); } int tries = 0; state.assign(n, 0); for (int i = 0; i < n; ++i) if (state[i] == 0) { tries++; cyc.clear(); st.clear(); cnt = 0; dfs(i); if (cyc.size() > 0) { cout << "NO" << endl; return 0; } } if (tries < 3) { cout << "NO" << endl; return 0; } cout << "FHTAGN!" << endl; return 0; } ```
### Prompt Your task is to create a cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int gra[102][102], vis[102]; int n, m; void dfs(int u) { int i; vis[u] = 1; for (i = 1; i <= n; i++) if (0 == vis[i] && gra[u][i]) dfs(i); } int main() { int i, u, v; cin >> n >> m; for (i = 1; i <= m; i++) { cin >> u >> v; gra[u][v] = gra[v][u] = 1; } dfs(1); for (i = 1; i <= n; i++) if (0 == vis[i]) { cout << "NO" << endl; return 0; } if (m == n) cout << "FHTAGN!" << endl; else cout << "NO" << endl; return 0; } ```
### Prompt Generate a JAVA solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class sample { static int cnt = 0; static boolean[][] adj; static boolean[] v; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(in.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int x, y; adj = new boolean[n][n]; v = new boolean[n]; for (int i = 0; i < m; i++) { st = new StringTokenizer(in.readLine()); x = Integer.parseInt(st.nextToken()); y = Integer.parseInt(st.nextToken()); adj[x - 1][y - 1] = adj[y - 1][x - 1] = true; } boolean C = true; int cnt2 = 0; for (int i = 0; i < n; i++) { if (!v[i]) { cnt2++; if (cnt2 > 1) { C = false; break; } dfs(i); } } if (cnt != 1) C = false; if (C) System.out.println("FHTAGN!"); else System.out.println("NO"); } public static void dfs(int i) { v[i] = true; for (int j = 0; j < adj.length; j++) { if (adj[i][j]) { adj[i][j] = adj[j][i] = false; if (v[j]) { cnt++; continue; } dfs(j); } } } } ```
### Prompt Please create a solution in cpp to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m; int adj[102][102]; int a, b, i; int k = 0; void dfs(int i, int vis[]) { vis[i] = 1; int ii; for (ii = 1; ii <= n; ii++) { if (vis[ii] == 0 && adj[i][ii] == 1) { dfs(ii, vis); } } } int main() { cin >> n >> m; if (n != m) { cout << "NO\n"; return 0; } for (i = 0; i < m; i++) { cin >> a >> b; adj[a][b] = 1; adj[b][a] = 1; } int vis[n]; for (i = 1; i <= n; i++) vis[i] = 0; dfs(1, vis); for (i = 1; i <= n; i++) { if (vis[i] == 0) { cout << "NO\n"; return 0; } } cout << "FHTAGN!\n"; return 0; } ```
### Prompt Please provide a java coded solution to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class Y103bKtulhu { Scanner in; PrintWriter out; public static void main(String[] args) throws IOException { new Y103bKtulhu().run(); } void run() throws IOException { boolean online = System.getProperty("ONLINE_JUDGE") != null; in = online ? new Scanner(System.in) : new Scanner(new FileReader("input.txt")); out = online ? new PrintWriter(System.out) : new PrintWriter(new FileWriter("output.txt")); solve(); out.flush(); out.close(); } void solve() throws IOException { Graph graph = inputGraph(); // if n == m and graph is connected, it is Ktulhu if ((graph.getN() == graph.getM()) && graph.isConnected()) { out.print("FHTAGN!"); } else { out.print("NO"); } return; } Graph inputGraph() { int n = in.nextInt(); int m = in.nextInt(); Graph graph = new Graph(n, m); graph.fill(in); return graph; } } class Graph { private int n; private int m; // for adjacency list: class Node { private final int v; // v = vertex number private final Node next; Node(int v, Node next) { this.v = v; this.next = next; } } private Node [] adjacencyList; Graph(int n, int m) { this.n = n; this.m = m; adjacencyList = new Node[n]; } public void fill(Scanner in) { for (int k = 0; k < m; k++) { int i = in.nextInt() - 1; int j = in.nextInt() - 1; Node newNode = new Node(j, adjacencyList[i]); adjacencyList[i] = newNode; newNode = new Node(i, adjacencyList[j]); adjacencyList[j] = newNode; } } public boolean isConnected() { int startVertex = 0; // connectedToStart is array of indicators: // vertex number i is connected to start vertex // <=> connectedToStart[i] == true boolean [] connectedToStart = new boolean[n]; connectedToStart[startVertex] = true; // dfs fills connectedToStart dfs(startVertex, connectedToStart); for (int i = 0; i < n; i++) { if (!connectedToStart[i]) { return false; } } return true; } private void dfs(int vertex, boolean[] connectedToStart) { Node node = adjacencyList[vertex]; while (node != null) { if (!connectedToStart[node.v]) { connectedToStart[node.v] = true; dfs(node.v, connectedToStart); } node = node.next; } } public int getN() { return n; } public int getM() { return m; } } ```
### Prompt Generate a JAVA solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Cthulhu { static ArrayList<Integer>[]adjList; static int []dfs_num; //0-->UNVISITED 1-->VISITED 2-->EXPLORED static int []dfs_parent; static int cycles=0; public static void main(String []args) throws NumberFormatException, IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s=br.readLine(); //System.out.println(s); String []r= s.split(" "); //System.out.println(r[0]+" "+r[1]+" "); int n = Integer.parseInt(r[0]); int m= Integer.parseInt(r[1]); adjList=new ArrayList[n]; dfs_num=new int[n]; dfs_parent=new int[n]; for(int i=0;i<n;i++) adjList[i]=new ArrayList<Integer>(); for(int i=0;i<m;i++){ String []a=br.readLine().split(" "); int start = Integer.parseInt(a[0]); int end = Integer.parseInt(a[1]); adjList[start-1].add(end); adjList[end-1].add(start); } int count=0; for(int i=0;i<n;i++){ if(dfs_num[i]==0){ graph_check(i); count++; } } if(count==1&&cycles==1) System.out.println("FHTAGN!"); else System.out.println("NO"); } public static void graph_check(int s){ dfs_num[s]=2; for(int m:adjList[s]){ if(dfs_num[m-1]==0){//EXPLORED-->UNVISITED dfs_parent[m-1]=s+1; graph_check(m-1); } else if(dfs_num[m-1]==2){//EXPLORED-->EXPLORED if(dfs_parent[s]!=m) cycles++; } } dfs_num[s]=1; } } ```
### Prompt In PYTHON3, your task is to solve the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```python3 def main(): def dfs(u): avail[u] = False for v in l[u]: if avail[v]: dfs(v) n, m = map(int, input().split()) if m != n: return print("NO") l = [[] for _ in range(m + 1)] for _ in range(m): a, b = map(int, input().split()) l[a].append(b) l[b].append(a) avail = [True] * (n + 1) dfs(1) print(("NO", "FHTAGN!")[sum(avail) == 1]) if __name__ == '__main__': main() ```
### Prompt Please provide a Cpp coded solution to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1000; int n, m; vector<int> g[N]; bool used[N]; void dfs(int v) { used[v] = 1; int sz = g[v].size(); for (int i = 0; i < sz; ++i) { int u = g[v][i]; if (!used[u]) dfs(u); } } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { int x, y; scanf("%d%d", &x, &y); --x, --y; g[x].push_back(y); g[y].push_back(x); } dfs(0); int s = 0; for (int i = 0; i < n; ++i) { if (used[i]) s++; } if (s == n && n == m) { puts("FHTAGN!"); } else { puts("NO"); } } ```
### Prompt Construct a Java code solution to the problem outlined: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.*; import java.io.*; public class C80C{ static BufferedReader br; static ArrayList ar[]; static int path[]; static int colors[]; static int c=0; static int cross=0; public static void main(String args[])throws Exception{ br=new BufferedReader(new InputStreamReader(System.in)); int nm[]=toIntArray(); int n=nm[0]; int m=nm[1]; /* int con[][]=new int[n][n]; for(int i=0;i<n;i++){ Arrays.fill(con[i],0); } for(int i=0;i<m;i++){ con[nm[0]][nm[1]]=1; con[nm[1]][nm[0]]=1; } */ ar=new ArrayList[n+1]; for(int i=0;i<ar.length;i++){ ar[i]=new ArrayList(); } for(int i=0;i<m;i++){ nm=toIntArray(); ar[nm[0]].add(nm[1]); ar[nm[1]].add(nm[0]); } colors=new int[n+1]; path=new int[n+1]; Arrays.fill(colors,0); Arrays.fill(path,0); int root=1; colors[root]=1; for(int i=0;i<ar[root].size();i++){ int nT=Integer.parseInt(String.valueOf(ar[root].get(i))); if(nT!=root){ if(colors[nT]==0){ colors[nT]=1; go(nT,root); } else{ cross++; } } } if(cross>=3){ System.out.println("NO"); return; } else if(cross == 2){ for(int i=1;i<=n;i++){ if(colors[i]==0){ System.out.println("NO"); return; } } System.out.println("FHTAGN!"); return; } System.out.println("NO"); } public static void go(int node, int f){ for(int i=0;i<ar[node].size();i++){ int nT=Integer.parseInt(String.valueOf(ar[node].get(i))); if(nT!=f){ if(colors[nT]==0){ colors[nT]=1; go(nT,node); } else{ cross++; } } } } /****************************************************************/ public static int[] toIntArray()throws Exception{ String str[]=br.readLine().split(" "); int k[]=new int[str.length]; for(int i=0;i<str.length;i++){ k[i]=Integer.parseInt(str[i]); } return k; } public static int toInt()throws Exception{ return Integer.parseInt(br.readLine()); } public static long[] toLongArray()throws Exception{ String str[]=br.readLine().split(" "); long k[]=new long[str.length]; for(int i=0;i<str.length;i++){ k[i]=Long.parseLong(str[i]); } return k; } public static long toLong()throws Exception{ return Long.parseLong(br.readLine()); } public static double[] toDoubleArray()throws Exception{ String str[]=br.readLine().split(" "); double k[]=new double[str.length]; for(int i=0;i<str.length;i++){ k[i]=Double.parseDouble(str[i]); } return k; } public static double toDouble()throws Exception{ return Double.parseDouble(br.readLine()); } public static String toStr()throws Exception{ return br.readLine(); } public static String[] toStrArray()throws Exception{ String str[]=br.readLine().split(" "); return str; } /****************************************************************/ } ```
### Prompt Please create a solution in Cpp to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> edges[105]; bool visited[105]; int cnt; int cycle; void dfs(int cur, int pre) { visited[cur] = 1; ++cnt; int nxt; for (int i = 0; i < edges[cur].size(); ++i) { nxt = edges[cur][i]; if (nxt == pre) continue; if (visited[nxt]) { ++cycle; } else dfs(nxt, cur); } } int main() { int n, m; int u, v; while (cin >> n >> m) { for (int i = 0; i < m; ++i) { cin >> u >> v; edges[u].push_back(v); edges[v].push_back(u); } cnt = 0; cycle = 0; dfs(1, -1); if (cnt != n || cycle != 2) cout << "NO\n"; else cout << "FHTAGN!\n"; for (int i = 1; i <= n; ++i) edges[i].clear(); } return 0; } ```
### Prompt In JAVA, your task is to solve the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.*; import java.io.*; public class pt{ static void dfs(LinkedList<Integer> l[],int s,boolean v[]){ v[s]=true; for(Integer i : l[s]) if(v[i]==false) dfs(l,i,v); } public static void main(String[] args)throws IOException { PrintWriter out = new PrintWriter(System.out); int n = ni(); int m = ni(); LinkedList<Integer> l[] = new LinkedList[n]; boolean v[] = new boolean[n]; for(int i=0;i<n;i++) l[i] = new LinkedList(); for(int i=1;i<=m;i++){ int u = ni()-1; int v1 = ni()-1; l[u].add(v1); l[v1].add(u); } int cc=0; for(int i=0;i<n;i++) if(v[i]==false) { dfs(l,i,v); cc++;} if(cc>1||m!=n) out.println("NO"); else out.println("FHTAGN!"); out.flush(); } static int mod=1000000007; static long pow(long a,long b){ long ans=1; while(b>0){ if(b%2==1) ans=(ans*a); a=(a*a)%mod; b=b/2; } return(ans); } static long gcd(long a,long b){ if(b%a==0) return(a); return(gcd(b%a,a)); } static long lcm(long a,long b){ return ((a*b)/gcd(a,b)); } static void Fact(long a[],long m){ a[0]=1; for(int i=1;i<a.length;i++) a[i]=(i*a[i-1])%m; } /* static void Fact_Inv(long a[],long F[],long m){ int n =a.length; a[n-1]=Fermat(F[n-1],m); for(int i=n-2;i>=0;i--) a[i]=((i+1)*a[i+1])%m; static long d, x, y; static void extendedEuclid(long A, long B) { if(B == 0) { d = A; x = 1; y = 0; } else { extendedEuclid(B, A%B); long temp = x; x = y; y = temp - (A/B)*y; } } */ /*static class pair implements Comparable<pair>{ int x,y; pair(int a,int b){ x=a; y=b; } public int compareTo(pair p1) { return((p1.x-p1.y)-(this.x-this.y));} public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; pair other = (pair) obj; if (x != other.x) return false; if (y != other.y) return false; return true; } @Override public int hashCode() { return(x-y); } } */ static void addEdge(LinkedList<Integer> adjList[],int s,int d){ adjList[s].add(d); adjList[d].add(s); } static int abs(char x,char y) { return(Math.abs(x-y));} static long min(long x,long y){ return(Math.min(x,y)); } static long max(long a,long b){ return Math.max(a,b); } static FastReader sc=new FastReader(); static int ni(){ int x = sc.nextInt(); return(x); } static long nl(){ long x = sc.nextLong(); return(x); } static String n(){ String str = sc.next(); return(str); } static String ns(){ String str = sc.nextLine(); return(str); } static double nd(){ double d = sc.nextDouble(); return(d); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } ```
### Prompt Generate a cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a, b, c, d, e, f, g, n, m, k, t, p, x, h, w, q; long long curr = 1; long long mod = 998244353; int dsu[109]; int find_parent(int v1) { if (dsu[v1] == v1) return v1; return dsu[v1] = find_parent(dsu[v1]); } void solve() { cin >> n >> m; int cycles = 0; for (int i = 1; i <= n; i++) dsu[i] = i; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a = find_parent(a); b = find_parent(b); if (a == b) { cycles++; } else { dsu[b] = dsu[a]; } } set<int> comps; for (int i = 1; i <= n; i++) { comps.insert(find_parent(i)); } if (cycles == 1 && comps.size() == 1) { cout << "FHTAGN!"; } else { cout << "NO"; } } int main() { ios_base::sync_with_stdio(false); int cases = 1; while (cases--) { solve(); } return 0; } ```
### Prompt Please create a solution in Java to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java //package taz; import java.util.*; import java.lang.StringBuilder; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; public class Taz { static int maxn=105; static int[][] Gg = new int[maxn][maxn]; static boolean[] vis = new boolean[maxn]; static String S; static ArrayList <Integer> [] G = new ArrayList[maxn]; static int N,M,cnt,cnt1,ind,ind1,ans,sum,summ,maxx,minn,mx=999999999,mn,x,y,z,x1,y2,z3,u,v,r; public static void main(String[] args) { Scanner scanf = new Scanner(System.in); N = scanf.nextInt(); M = scanf.nextInt(); for(int i=1; i<=N; i++){ G[i] = new ArrayList<>(); } for(int i=1; i<=M; i++){ v = scanf.nextInt(); u=scanf.nextInt(); G[v].add(u); G[u].add(v); } for(int i=1; i<=N; i++){ if(vis[i]==false){ cnt++; DFS(i); } } System.out.println((cnt==1 && N==M) ? "FHTAGN!" : "NO"); } public static void DFS(int v){ vis[v]=true; for(int i=0; i<G[v].size(); i++){ int j=G[v].get(i); if(vis[j]==false){ DFS(j); } } } public static void close(){ System.exit(0); } public static int MAX(int a, int b){ return a>b? a : b ; } public static int ABS(int a){ return a>0? a : -a; } public static int GCD(int a, int b){ return b==0? a : GCD(b,b%a); } public static boolean isPal(StringBuilder SS){ for(int i=0; i<SS.length()/2; i++){ if(SS.charAt(i)!=SS.charAt(SS.length()-1-i)){ return false; } } return true; } public static int funcX(String SS){ int X=0; for(int i=0; i<SS.length(); i++){ X=X*10+(SS.charAt(i)-'0'); } return X; } } ```
### Prompt In Java, your task is to solve the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.*; public class B103 { static int status[]; static ArrayList<Integer>[]adjList; static int c=0; public static void dfs(int i) { status[i]=1; for(int x:adjList[i]) { if(status[x]==0) { adjList[x].remove(adjList[x].indexOf(i)); dfs(x); } else if(status[x]==1)c++; } status[i]=2; } public static void main(String[] args) { Scanner sc = new Scanner (System.in); int n=sc.nextInt(); int m=sc.nextInt(); status=new int[n]; adjList=new ArrayList[n]; for(int i=0;i<n;i++)adjList[i]=new ArrayList<Integer>(); for(int i=0;i<m;i++) { int x=sc.nextInt(); int y=sc.nextInt(); adjList[x-1].add(y-1); adjList[y-1].add(x-1); } dfs(0); for(int i=0;i<n;i++) { if(status[i]!=2)c=0; } if(c==1)System.out.println("FHTAGN!"); else System.out.println("NO"); } } ```
### Prompt Construct a Java code solution to the problem outlined: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Set; public class P103B { public static void main(String[] args) { Scanner inScanner = new Scanner(System.in); int n = inScanner.nextInt(); int m = inScanner.nextInt(); Node[] nodes = new Node[n]; for (int i = 0; i < n; i++) nodes[i] = new Node(); for (int i = 0; i < m; i++) { Node first = nodes[inScanner.nextInt() - 1]; Node second = nodes[inScanner.nextInt() - 1]; first.neighbors.add(second); second.neighbors.add(first); } Queue<Node> queue = new LinkedList<Node>(); queue.add(nodes[0]); Node cycleStartNode = null; int cycles = 0; int seen = 0; while (!queue.isEmpty()) { Node current = queue.remove(); if (current.visited) continue; seen++; current.visited = true; for (Node neighbor : current.neighbors) { if (neighbor.visited && !neighbor.equals(current.visitedFrom)) { cycleStartNode = neighbor; cycles++; } else { neighbor.visitedFrom = current; queue.add(neighbor); } } } if (seen != n || cycles != 1) { System.out.println("NO"); return; } for (Node node : nodes) node.visited = false; Set<Node> cycleNodes = findCycleNodes(cycleStartNode, cycleStartNode); /* for (Node cycleNode : cycleNodes) { if (cycleNode.neighbors.size() > 3) { System.out.println("NO"); return; } } */ System.out.println("FHTAGN!"); } private static Set<Node> findCycleNodes(Node currentNode, Node cycleStartNode) { if (currentNode.visited) return null; currentNode.visited = true; Set<Node> returned = new HashSet<Node>(); returned.add(currentNode); for (Node neighbor : currentNode.neighbors) { if (currentNode.equals(cycleStartNode)) return returned; Set<Node> neighborReturned = findCycleNodes(neighbor, cycleStartNode); if (neighborReturned != null) { returned.addAll(neighborReturned); return returned; } } return null; } private static class Node { Set<Node> neighbors; boolean visited; Node visitedFrom; Node() { neighbors = new HashSet<Node>(); visited = false; visitedFrom = null; } } } ```
### Prompt Generate a Java solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Cthulhu implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); private class DisjointSet { private int[] root, size; private DisjointSet(int n) { root = new int[n + 1]; size = new int[n + 1]; for (int i = 0; i <= n; i++) { root[i] = i; size[i] = 1; } } private int root(int x) { return (x == root[x]) ? x : (root[x] = root(root[x])); } private void join(int a, int b) { int x = root(a), y = root(b); if (x != y) { if (size[x] < size[y]) y = (x + y) - (x = y); root[y] = x; size[x] += size[y]; } } } public void solve() { int n = in.ni(), m = in.ni(); if (n != m) { out.println("NO"); } else { DisjointSet dsu = new DisjointSet(n); for (int i = 0; i < m; i++) { dsu.join(in.ni(), in.ni()); } int cnt = 0; for (int i = 1; i <= n; i++) { if (dsu.root(i) == i) cnt++; } if (cnt == 1) { out.println("FHTAGN!"); } else { out.println("NO"); } } } @Override public void close() throws IOException { in.close(); out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (Cthulhu instance = new Cthulhu()) { instance.solve(); } } } ```
### Prompt Construct a cpp code solution to the problem outlined: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void connected(vector<vector<bool> > chu, vector<bool>& v, int s) { v[s] = 1; for (int i = 0; i < chu.size(); i++) { if (chu[s][i] && !v[i]) { connected(chu, v, i); } } } int main() { int n, m; while (cin >> n >> m) { if (n != m) { cout << "NO" << endl; break; } vector<vector<bool> > chu(n, vector<bool>(n, 0)); for (int i = 0; i < m; i++) { int e1, e2; cin >> e1 >> e2; chu[e1 - 1][e2 - 1] = 1; chu[e2 - 1][e1 - 1] = 1; } vector<bool> visisted(n, 0); connected(chu, visisted, 0); bool allseen = true; for (int i = 0; i < visisted.size(); i++) { if (!visisted[i]) allseen = false; } if (allseen) { cout << "FHTAGN!" << endl; } else { cout << "NO" << endl; } } return 0; } ```
### Prompt Generate a Python3 solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```python3 lectura=lambda:map(int, input().split()) nVertices, nEdges= lectura() array1= [[] for i in range(nVertices + 1)] array2=[] for i in range(0, nEdges): x, y= lectura() array1[x].append(y) array1[y].append(x) def DFS(x): array2.append(x) for y in array1[x]: if (y not in array2): DFS(y) DFS(1) if(nVertices!=nEdges): conclusion="NO" else: if(len(array2)==nVertices): conclusion="FHTAGN!" else: conclusion="NO" print(conclusion) ```
### Prompt In Java, your task is to solve the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.*; import java.util.*; public class Cthulhu { static boolean isCthulhu(int n, int m, List<Integer>[] edges) { if (n < 3 || n != m) { return false; } boolean[] visited = new boolean[edges.length]; dfs(0, visited, edges); for (boolean b : visited) { if (!b) { return false; } } return true; } private static void dfs(int i, boolean[] visited, List<Integer>[] edges) { visited[i] = true; for (int v : edges[i]) { if (!visited[v]) { dfs(v, visited, edges); } } } public static void main(String[] args) throws IOException { int n, m; try (Scanner scanner = new Scanner(System.in)) { n = scanner.nextInt(); m = scanner.nextInt(); @SuppressWarnings("unchecked") List<Integer>[] edges = new List[n]; for (int i = 0; i < edges.length; i++) { edges[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = scanner.nextInt(); int v = scanner.nextInt(); edges[u - 1].add(v - 1); edges[v - 1].add(u - 1); } System.out.println(isCthulhu(n, m, edges) ? "FHTAGN!" : "NO"); } } } ```
### Prompt Please formulate a CPP solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; #pragma GCC optimize("-O3") using ld = long double; const long long mod = 1000000007; const long long inf = 1000000000000000000; const long long rk = 256; const ld PI = 3.141592653589793; ostream& operator<<(ostream& os, pair<long long, long long> const& x) { os << x.first << "," << x.second; return os; } template <class T> ostream& operator<<(ostream& os, vector<T> const& x) { os << "{ "; for (auto& y : x) os << y << " "; return os << "}"; } template <class T> ostream& operator<<(ostream& os, set<T> const& x) { os << "{ "; for (auto& y : x) os << y << " "; return os << "}"; } template <class Ch, class Tr, class Container> basic_ostream<Ch, Tr>& operator<<(basic_ostream<Ch, Tr>& os, Container const& x) { os << "{ "; for (auto& y : x) os << y << " "; return os << "}"; } template <class X, class Y> ostream& operator<<(ostream& os, pair<X, Y> const& p) { return os << "[ " << p.first << ", " << p.second << "]"; } const int mx = 1e6 + 10; vector<vector<long long> > graph(mx); vector<vector<long long> > cycles(mx); void dfs_cycle(long long u, long long p, vector<long long>& color, vector<long long>& mark, vector<long long>& par, long long& cyclenumber, vector<bool>& visited) { visited[u] = true; if (color[u] == 2) return; if (color[u] == 1) { cyclenumber++; long long cur = p; mark[cur] = cyclenumber; while (cur != u) { cur = par[cur]; mark[cur] = cyclenumber; } return; } par[u] = p; color[u] = 1; for (long long v : graph[u]) { if (v == par[u]) continue; dfs_cycle(v, u, color, mark, par, cyclenumber, visited); } color[u] = 2; } bool getCycles(long long edges, vector<long long> mark, long long& cycleNumber) { map<long long, long long> make_pair; for (int i = 1; i <= edges; i++) { make_pair[mark[i]]++; } bool has_one_cycle = false; bool has_3_node = false; long long cnt = 0; for (map<long long, long long>::iterator it = make_pair.begin(); it != make_pair.end(); it++) { if (it->first != 0) cnt++; if (it->second > 2) has_3_node = true; } has_one_cycle = (cnt == 1); return has_one_cycle && has_3_node; } int32_t main() { long long n, m; cin >> n >> m; if (n != m) { cout << "NO\n"; return 0; } for (int i = 0; i < m; i++) { long long a, b; cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } vector<long long> color(mx, 0); vector<long long> parent(mx, 0); vector<long long> mark(mx, 0); long long cyclenumber = 0; vector<bool> visited(n + 1, false); for (int i = 1; i <= n; i++) { if (!visited[i]) { visited[i] = true; dfs_cycle(i, 0, color, mark, parent, cyclenumber, visited); } } if (getCycles(m, mark, cyclenumber)) cout << "FHTAGN!\n"; else cout << "NO\n"; return 0; } ```
### Prompt Your challenge is to write a python solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```python inp = lambda: map(int,raw_input().split()) n,m = inp() par = [i for i in range (0,n+1)] def findPar(x): if (x == par[x]): return x par[x] = findPar(par[x]) return par[x] def Union(x,y): x = findPar(x) y = findPar(y) if (x!=y): par[x] = y return 0 return 1 cnt = 0 for i in range (0,m): x,y = inp() cnt += Union(x,y) comp = 0 for i in range (1,n+1): if (par[i] == i): comp += 1 if (comp == cnt and cnt == 1): print "FHTAGN!" else : print "NO" ```
### Prompt Generate a Java solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.awt.*; import java.util.*; public class Cthulhu { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(), m = s.nextInt(),circle=0; ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); a.add(0); for (int i = 1; i < n + 1; i++) { a.add(i); } for (int i = 0; i < m; i++) { int l = s.nextInt(); int r = s.nextInt(); if (findPar(l, a) == findPar(r, a)) { circle++; continue; } union(l, r, a); } /*Set<Integer> r = new HashSet<>(); for (int i = 1; i < n + 1; i++) { r.add(findPar(i,a)); }*/ if(circle==1 && n<=m){ System.out.print("FHTAGN!"); }else{ System.out.print("NO"); } } public static int findPar(int x, ArrayList<Integer> Par) { if (x == Par.get(x)) { return x; } Par.set(x, findPar(Par.get(x), Par)); return Par.get(x); } public Boolean find(int x, int y, ArrayList<Integer> Par) { return findPar(x, Par) == findPar(y, Par); } public static void union(int x, int y, ArrayList<Integer> Par) { int Gparx = findPar(x, Par); int Gpary = findPar(y, Par); Par.set(Gparx, Gpary); } } ```
### Prompt Generate a python3 solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```python3 def solve(): n,m = map(int,input().split()) color = [0 for _ in range(n+1)] edges = [[] for _ in range(n+1)] visited = [False for _ in range(n+1)] for _ in range(m): u,v = map(int,input().split()) edges[u].append(v) edges[v].append(u) stack = list() stack.append(1) while stack: cur = stack.pop() visited[cur] = True for child in edges[cur]: if not visited[child]: stack.append(child) visited[child] = True for i in range(1,n+1): if not visited[i] or n!=m: print('NO') return print('FHTAGN!') solve() ```
### Prompt Create a solution in java for the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.Iterator; import java.util.PriorityQueue; public class acm_practice { static ArrayList<Integer>[]adj; static boolean visited[]; public static void dfs(int k) { visited [k]=true; for(int i=0;i<adj[k].size();i++) { int j=adj[k].get(i); if(!visited[j]) { dfs(j); } } } public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(bf.readLine(), " "); int n=Integer.parseInt(st.nextToken()); int m=Integer.parseInt(st.nextToken()); adj=new ArrayList[n+1]; visited= new boolean [n+1]; for(int i=0;i<=n;i++) { adj[i]=new ArrayList<>(); } long ans=0; if(n!=m) { System.out.println("NO"); return; } for(int i=0;i<m;i++) { st = new StringTokenizer(bf.readLine(), " "); int u=Integer.parseInt(st.nextToken()); int v=Integer.parseInt(st.nextToken()); adj[u].add(v); adj[v].add(u); } dfs(1); for(int i=1;i<=n;i++) { if(!visited[i]) { System.out.println("NO"); return; } } System.out.println("FHTAGN!"); } static class pair { int city; int u; public pair(int city ,int u) { this.city=city; this.u=u; } } static class bugs implements Comparable<bugs>{ int com; int idx; public bugs(int com, int idx) { this.com=com; this.idx=idx; } public int compareTo(bugs o) { return com - o.com; } } // static class Scanner // { // StringTokenizer st; // BufferedReader br; // // public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} // // public String next() throws IOException // { // while (st == null || !st.hasMoreTokens()) // st = new StringTokenizer(br.readLine()); // return st.nextToken(); // } // // public int nextInt() throws IOException {return Integer.parseInt(next());} // // public long nextLong() throws IOException {return Long.parseLong(next());} // // public String nextLine() throws IOException {return br.readLine();} // // public boolean ready() throws IOException {return br.ready();} // // public double nextDouble(String x) // { // // StringBuilder sb = new StringBuilder("0"); // double res = 0, f = 1; // boolean dec = false, neg = false; // int start = 0; // if(x.charAt(0) == '-') // { // neg = true; // start++; // } // for(int i = start; i < x.length(); i++) // if(x.charAt(i) == '.') // { // res = Long.parseLong(sb.toString()); // sb = new StringBuilder("0"); // dec = true; // } // else // { // sb.append(x.charAt(i)); // if(dec) // f *= 10; // } // res += Long.parseLong(sb.toString()) / f; // return res * (neg?-1:1); // } // // // } } ```
### Prompt Please formulate a cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long v, p[10005], ans, ans1, x, y, z, n, m; pair<int, pair<int, int> > a[100005]; map<int, int> mp; int get_col(int v) { if (v == p[v]) return v; p[v] = get_col(p[v]); return p[v]; } void col(int a, int b) { p[get_col(a)] = get_col(b); } int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> x >> y; mp[x]++; mp[y]++; z = 1; a[i].first = z; a[i].second.first = x; a[i].second.second = y; } for (int i = 1; i <= n; i++) if (mp[i] == 0) { cout << "NO"; return 0; } sort(a + 1, a + m + 1); for (int i = 1; i <= n; i++) p[i] = i; for (int i = 1; i <= m; i++) { x = a[i].second.first; y = a[i].second.second; if (get_col(x) != get_col(y)) { col(get_col(x), get_col(y)); ans += a[i].first; } else ans1++; } if (ans1 == 1) cout << "FHTAGN!" << endl; else cout << "NO"; } ```
### Prompt Construct a java code solution to the problem outlined: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.StringTokenizer; public class Main { private static BufferedReader br; private static PrintWriter pw; private static StringTokenizer stk = new StringTokenizer(""); public static void main(String[] args) { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); (new Main()).solve(); pw.close(); } private void nline() { while (!stk.hasMoreElements()) { stk = new StringTokenizer(rline()); } } private String rline() { try { return br.readLine(); } catch (IOException e) { throw new RuntimeException("rline"); } } private boolean isready() { try { return br.ready(); } catch (IOException e) { throw new RuntimeException("isready"); } } private String ntok() { nline(); return stk.nextToken(); } private Integer ni() { return Integer.valueOf(ntok()); } private Double nd() { return Double.valueOf(ntok()); } private Long nl() { return Long.valueOf(ntok()); } private BigInteger nBI() { return new BigInteger(ntok()); } int[][] a = null; int n = 0; int m = 0; int[] cyc = new int[1000]; int dfs(int w, int ff, int[] u) { for (int i=0;i<n;i++) if (i!=ff&&i!=w&&a[w][i]>0) { if (u[i]==1) { cyc[i]=1; cyc[w]=1; return i; } u[i]=1; int xx = dfs(i,w,u); if (xx!=-1) { if (xx==-2) return -2; if (xx==w) return -2; cyc[w]=1; return xx; } } return -1; } void dfs2(int w, int[] u) { for (int i=0;i<n;i++) if (u[i]==0&&a[w][i]!=0) { u[i]=1; dfs2(i,u); } } private void solve() { n = ni(); m = ni(); if (m!=n||n<=2) { pw.println("NO"); return; } a = new int[n][n]; for (int i=0;i<m;i++) { int f = ni()-1; int t = ni()-1; a[f][t] = 1; a[t][f] = 1; } int c = 0; int[] cn = new int[n]; for (int i=0;i<n;i++){ int cc = 0; for (int j=0;j<n;j++) if (a[i][j]>0) cc++; cn[i]=cc; } int[] ar = new int[n]; ar[0]=1; dfs(0,-1,ar); for (int i=0;i<n;i++) if (cyc[i]!=0) c+=1; int[] ddd = new int[n]; ddd[0]=1; dfs2(0,ddd); int oo = 0; for (int i=0;i<n;i++) if (ddd[i]!=0) oo++; if (oo==n&&c>=3) pw.println("FHTAGN!"); else pw.println("NO"); } } ```
### Prompt Please provide a java coded solution to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.StringTokenizer; public class Cthulhu { static ArrayList<Integer>[] g; static boolean[] vis; static void dfs(int u) { vis[u] = true; for (int v : g[u]) if (!vis[v]) dfs(v); } public static void main(String[] args) throws IOException { MyScanner sc = new MyScanner(System.in); int n = sc.nextInt(), m = sc.nextInt(); if (m != n) { System.out.println("NO"); } else { g = new ArrayList[n]; for(int i = 0;i < n;i++) g[i] = new ArrayList<>(); for (int i = 0; i < m; i++) { int u = sc.nextInt() - 1, v = sc.nextInt() - 1; g[u].add(v); g[v].add(u); } vis = new boolean[n]; int cc = 0; for (int i = 0; i < n; i++) if (!vis[i]) { dfs(0); cc++; } if(cc != 1) System.out.println("NO"); else System.out.println("FHTAGN!"); } } static class MyScanner { BufferedReader br; StringTokenizer st; MyScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } boolean ready() throws IOException { return br.ready(); } } } ```
### Prompt Develop a solution in JAVA to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.*; public class C { Scanner sc = new Scanner(System.in); int n; ArrayList<ArrayList<Integer> > edges; boolean [] visited; int [] hist; boolean found; ArrayList<Integer> spheric; boolean solve() { n = sc.nextInt(); edges = new ArrayList<ArrayList<Integer>>(n); for(int i = 0; i < n; i++) edges.add(new ArrayList<Integer>()); int m = sc.nextInt(); for(int i = 0; i < m; i++) { int from = sc.nextInt() - 1, to = sc.nextInt() - 1; edges.get(from).add(to); edges.get(to).add(from); } // ���[�v��T�� found = false; visited = new boolean[n]; hist = new int[n]; spheric_found(0, 0); if(found == false) return false; // �Ǘ����Ă���m�[�h�������false for(int i = 0; i < n; i++) if(visited[i] == false) return false; // ���[�v���X�^�[�g�ɂ��Ė؂ɂȂ��Ă��邩�m�F for(int i = 1; i < spheric.size(); i++) { edges.get(spheric.get(i)).remove(new Integer(spheric.get(i-1))); edges.get(spheric.get(i-1)).remove(new Integer(spheric.get(i))); } /* System.out.println(spheric); for(int i = 0; i < n; i++) { System.out.println(i + " " + edges.get(i)); }*/ for(int i = 1; i < spheric.size(); i++) { found = false; visited = new boolean[n]; for(Integer j: spheric) if(j != spheric.get(i)) visited[j] = true; hist = new int[n]; loop_found(0, spheric.get(i)); if(found) { //for(int j = 0; j < n; j++) System.out.print(hist[j]); //System.out.println(); return false; } } return true; } void spheric_found(int depth, int cur) { if(visited[cur] ) { // ���[�v�����‚����� if(found == false) { // ��–ڂ̃��[�v�̂Ƃ� spheric = new ArrayList<Integer>(); spheric.add(cur); for(int i = depth-1; i >= 0; i--) { spheric.add(hist[i]); if(hist[i] == cur) break; } } found = true; } else { visited[cur] = true; hist[depth] = cur; for(Integer nxt: edges.get(cur)) { if(depth > 0 && hist[depth-1] == nxt) continue; // �����ɖ߂�̂̓_�� spheric_found(depth+1, nxt); } } } void loop_found(int depth, int cur) { //if(depth == 0) System.out.println((depth+1) + " " + (cur+1) + edges.get(cur)); if(found) return; // ���[�v�����‚����Ă��� if(visited[cur]) { // ���[�v�����‚����� found = true; return; } else { visited[cur] = true; hist[depth] = cur; for(Integer nxt: edges.get(cur)) { if(depth > 0 && hist[depth-1] == nxt) continue; // �����ɖ߂�̂̓_�� loop_found(depth+1, nxt); } } } void doIt() { boolean ans = solve(); System.out.println(ans ? "FHTAGN!" : "NO"); } public static void main(String[] args) { new C().doIt(); } } ```
### Prompt Please create a solution in Java to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.*; import java.awt.List; import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; public class Main { static boolean [] visited; static ArrayList<ArrayList<Integer>> adjlist; static int cycles; static void dfs (int v,int father) { visited[v] = true; for (int u : adjlist.get(v)) { if (!visited[u]) dfs (u,v); else if (u != father) cycles ++; } } public static void main (String [] args) { Scanner sc = new Scanner(System.in); int V = sc.nextInt(); int E = sc.nextInt(); cycles = 0; visited = new boolean [V]; adjlist = new ArrayList<ArrayList<Integer>> (V); for (int i = 0; i < V; i++) { adjlist.add(new ArrayList<Integer>()); } for (int i = 0;i < E; i ++) { int v = sc.nextInt(); int u = sc.nextInt(); adjlist.get(u-1).add(v-1); adjlist.get(v-1).add(u-1); } int ConnectedComponents = 0; for (int i = 0 ; i < V ; i++) if (!visited[i]) { dfs(i,-1); ConnectedComponents++; } System.out.println(ConnectedComponents == 1 && cycles == 2 ? "FHTAGN!" : "NO"); } } ```
### Prompt Please formulate a java solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java //package project; import java.io.FileInputStream; import java.util.Scanner; public class Algorithm { static int N; static int M; static int[][] graphs; static int countV; public static void main(String[] args) throws Exception { //System.setIn(new FileInputStream("src/project/input.txt")); Scanner sc = new Scanner(System.in); N = sc.nextInt(); M = sc.nextInt(); graphs = new int[N + 1][N + 1]; for (int i = 0; i < M; i++) { int u = sc.nextInt(); int v = sc.nextInt(); graphs[u][v] = 1; graphs[v][u] = 1; } countV = 0; bfs(1); if (countV == N && N == M) { System.out.println("FHTAGN!"); }else{ System.out.println("NO"); } sc.close(); } private static void bfs(int index) { int front = -1; int end = 0; int[] queues = new int[N]; boolean[] visited = new boolean[N + 1]; queues[end] = index; visited[index] = true; countV++; while (front != end) { int u = queues[++front]; for (int v = 1; v <= N; v++) { if (graphs[u][v] == 1 && !visited[v]) { visited[v] = true; queues[++end] = v; countV++; } } } } } ```
### Prompt Develop a solution in CPP to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m, a, b; cin >> n >> m; if (m == n) { vector<bool> rev(n, false); vector<vector<int> > mat(n + 1); for (int i = 0; i < m; i++) { cin >> a >> b; mat[a].push_back(b); mat[b].push_back(a); } int cont = 0; for (int i = 1; i <= m; i++) { if (!rev[i]) { cont++; queue<int> COLA; COLA.push(i); rev[i] = true; while (!COLA.empty()) { int j = COLA.front(); COLA.pop(); for (int k = 0; k < mat[j].size(); k++) { if (!rev[mat[j][k]]) { rev[mat[j][k]] = 1; COLA.push(mat[j][k]); } } } } } if (cont == 1) cout << "FHTAGN!\n"; else cout << "NO\n"; } else cout << "NO\n"; return 0; } ```
### Prompt Generate a cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m; int cycles = 0; vector<vector<int> > tree(101); vector<int> parents(101); vector<int> vis(101); vector<int> color(101); void dfs(int node, int parent) { if (vis[node] == 2) return; if (vis[node] == 1) { int p = parent; cycles++; color[p] = cycles; while (p != node) { p = parents[p]; color[p] = cycles; } return; } parents[node] = parent; vis[node] = 1; int child; for (int i = 0; i < tree[node].size(); i++) { child = tree[node][i]; if (child == parent) continue; dfs(child, node); } vis[node] = 2; return; } int main() { int a, b; int connect = 0; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b; tree[a].push_back(b); tree[b].push_back(a); } if (m != n) { cout << "NO"; return 0; } for (int i = 1; i <= n; i++) { if (vis[i] == 0) { dfs(i, -1); connect++; } } if (connect != 1) { cout << "NO"; return 0; } if (cycles != 1) { cout << "NO" << endl; return 0; } cout << "FHTAGN!"; return 0; } ```
### Prompt Please formulate a Java solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; public class B implements Runnable { boolean[][] e; boolean[] g; private static final String NO = "NO"; public void solve() throws IOException { int n = nextInt(); int m = nextInt(); e = new boolean[n][n]; for ( int i = 0; i < m; i ++ ) { int a = nextInt() - 1; int b = nextInt() - 1; e[a][b] = true; e[b][a] = true; } g = new boolean[n]; go( 0 ); for ( int i = 0; i < n; i ++ ) { if ( ! g[i] ) { out.println( NO ); return; } } Arrays.fill( g, false ); if ( ! cycle( 0, -1 ) ) { out.println( NO ); return; } Arrays.fill( g, false ); for ( int i = 0; i < n; i ++ ) { if ( ! g[i] && cycle( i, -1 ) ) { out.println( NO ); return; } } out.println( "FHTAGN!" ); } private boolean cycle( int v, int p ) { if ( g[v] ) { e[v][p] = false; e[p][v] = false; return true; } g[v] = true; for ( int i = 0; i < e[v].length; i ++ ) { if ( e[v][i] && p != i && cycle( i, v ) ) { return true; } } return false; } private void go( int v ) { if ( g[v] ) { return; } g[v] = true; for ( int i = 0; i < e[v].length; i ++ ) { if ( e[v][i] ) { go( i ); } } } public StreamTokenizer in; public PrintWriter out; B() throws IOException { in = new StreamTokenizer( new InputStreamReader( System.in ) ); out = new PrintWriter(System.out); } int nextInt() throws IOException { in.nextToken(); return ( int ) in.nval; } void check(boolean f, String msg) { if (!f) { out.close(); throw new RuntimeException(msg); } } void close() throws IOException { out.close(); } public void run() { try { solve(); close(); } catch (Exception e) { e.printStackTrace(out); out.flush(); throw new RuntimeException(e); } } public static void main(String[] args) throws IOException { new Thread(new B()).start(); } } ```
### Prompt Please formulate a JAVA solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.*; import java.util.*; public class B103 { static int n, m; static List<Integer>[] graph; static boolean[] visited; public static void main(String[] args) throws IOException { PrintWriter w = new PrintWriter(System.out); InputReader in = new InputReader(System.in); n = in.nextInt(); m = in.nextInt(); if (n != m) { w.println("NO"); } else { graph = new ArrayList[n+1]; visited = new boolean[n+1]; Arrays.setAll(graph, i -> new ArrayList<>()); for (int i=0; i<m; i++) { int a = in.nextInt(); int b = in.nextInt(); graph[a].add(b); graph[b].add(a); } dfs(1); boolean flag = true; for (int i=1; i<=n; i++) { if (!visited[i]) { flag = false; break; } } w.println(flag ? "FHTAGN!" : "NO"); } w.close(); } static void dfs(int u) { visited[u] = true; for (int i : graph[u]) { if (!visited[i]) { dfs(i); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } } ```
### Prompt Please create a solution in JAVA to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.*; import java.util.*; import java.util.stream.*; import java.lang.*; public class cf { public static void main(String[] args) { InputStream inputStream=System.in; OutputStream outputStream=System.out; InputReader in=new InputReader(inputStream); PrintWriter out=new PrintWriter(outputStream); Task task=new Task(); task.run(in,out); out.close(); } } class InputReader { public BufferedReader br; public StringTokenizer st; public InputReader(InputStream stream) { br=new BufferedReader(new InputStreamReader(stream),32768); st=null; } public String next() { while (st==null || !st.hasMoreTokens()) { try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() {return Integer.parseInt(next());} public long nextLong() {return Long.parseLong(next());} public double nextDouble() {return Double.parseDouble(next());} } class Task { public void run(InputReader in,PrintWriter out) { int n=in.nextInt(),m=in.nextInt(); if (n!=m) {out.print("NO");return;} int[] marked=new int[n+1]; List<List<Integer>> e=new ArrayList<List<Integer>>(); for (int i=0;i<=n;i++) {e.add(new ArrayList<Integer>());} for (int i=0;i<m;i++) { int a=in.nextInt(),b=in.nextInt(); e.get(a).add(b); e.get(b).add(a); } Deque<Integer> st=new ArrayDeque<Integer>(); st.addLast(1); while (st.size()>0) { int v=st.pollLast(); if (marked[v]==0) { marked[v]++; for (int j=0;j<e.get(v).size();j++){ st.addLast(e.get(v).get(j)); } } } if (IntStream.of(marked).sum()!=n) {out.print("NO");} else {out.print("FHTAGN!");} } } ```
### Prompt Please provide a java coded solution to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.*; import java.util.*; public class b{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); boolean isK = true; int[][] g = new int[n][n]; for(int i = 0; i < m; i++){ int a = sc.nextInt()-1; int b = sc.nextInt()-1; g[a][b] = 1; g[b][a] = 1; } visited = new boolean[n]; isRoot = new boolean[n]; int r = dfsFindRoot(0,-1,g,0); if(r==-1) isK = false; visited = new boolean[n]; for(int i = 0; i < n; i++) if(isRoot[i] && dfsFromRoot(i,-1,g) == -2) isK = false; for(int i = 0; i < n; i++) if(!visited[i]) isK = false; System.out.println((isK?"FHTAGN!":"NO")); } public static int dfsFindRoot(int s, int f, int[][] g, int l){ if(visited[s]){ return s; } visited[s] = true; for(int i = 0; i < g[s].length; i++){ if(i!=s && i!=f && g[s][i] > 0){ int r = dfsFindRoot(i,s,g,l+1); if(r==-2){ return -2; } else if(r != -1){ g[s][i] = 0; g[i][s] = 0; isRoot[s] = true; if(r==s) return -2; return r; } } } return -1; } public static int dfsFromRoot(int s, int f, int[][] g){ if(isRoot[s] && f!=-1){ return -2; } if(visited[s]){ return -2; } visited[s] = true; for(int i = 0; i < g[s].length; i++){ if(i!=s && i!=f && g[s][i] > 0){ int r = dfsFromRoot(i,s,g); if(r==-2){ return -2; } } } return -1; } public static boolean[] visited; public static boolean[] isRoot; } ```
### Prompt Construct a cpp code solution to the problem outlined: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n = 0; int m = 0; bool *visited; bool **ADJ; int ind = 0; int speicial[] = {6, 6, 6, 3, 6, 4, 5, 1, 2, 5, 1, 4, 5, 4}; int input[1000]; void DFS(int begin) { stack<int> search; int curr = begin; visited[curr] = true; search.push(curr); while (!search.empty()) { curr = search.top(); visited[curr] = true; search.pop(); for (int var = 1; var < n + 1; var++) { if (ADJ[curr][var] && !visited[var]) { search.push(var); } } } } int connected_component() { int num = 0; for (int i = 1; i < n + 1; i++) if (!visited[i]) { DFS(i); num++; } return num; } int main() { cin >> n >> m; input[ind++] = n; input[ind++] = m; visited = new bool[n + 1]; for (int i = 0; i < n + 1; i++) visited[i] = false; ADJ = new bool *[n + 1]; for (int i = 0; i < n + 1; i++) ADJ[i] = new bool[n + 1]; for (int i = 0; i < n + 1; i++) for (int j = 0; j < n + 1; j++) ADJ[i][j] = false; for (int i = 0; i < m; i++) { int a = 0; int b = 0; cin >> a >> b; ADJ[a][b] = true; ADJ[b][a] = true; input[ind++] = a; input[ind++] = b; } int x = connected_component(); if (x == 1 && m == n) { printf("FHTAGN!\n"); } else { printf("NO\n"); } return 0; } ```
### Prompt Develop a solution in Cpp to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<vector<int>> g(110); int vis[110]; int dfs(int v, int pai) { int nciclos = 0; for (int i = 0; i < g[v].size(); i++) { int u = g[v][i]; if (u == pai) continue; if (vis[u] != 0) nciclos++; else { vis[u] = 1; nciclos += dfs(u, v); } } return nciclos; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } for (int i = 0; i < n; i++) vis[i] = 0; vis[0] = 1; int nciclos = dfs(0, -1); bool conexo = true; for (int i = 0; i < n; i++) if (vis[i] == 0) conexo = false; if (nciclos / 2 != 1 or !conexo) cout << "NO" << endl; else cout << "FHTAGN!" << endl; } ```
### Prompt Develop a solution in CPP to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int map[110][110]; int n, m; int pre[110]; int find(int t) { if (t != pre[t]) { return pre[t] = find(pre[t]); } return t; } int main() { int i, j; int u, v; cin >> n >> m; for (i = 1; i <= n; i++) pre[i] = i; int cnt = 0; for (i = 0; i < m; i++) { cin >> u >> v; int x = find(u); int y = find(v); if (x == y) cnt++; else pre[x] = y; } for (i = 1; i < n; i++) if (find(i) != find(i + 1)) { cout << "NO" << endl; return 0; } if (cnt == 1) cout << "FHTAGN!" << endl; else cout << "NO" << endl; return 0; } ```
### Prompt Please formulate a Cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 110; const int mod = 1e9 + 7; int n, m; int pre[maxn]; int found(int x) { return pre[x] == x ? x : (pre[x] = found(pre[x])); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i <= n; i++) pre[i] = i; int ans = 1; int cnt = 0; int x, y; for (int i = 0; i < m; i++) { scanf("%d%d", &x, &y); int px = found(x); int py = found(y); if (px != py) pre[px] = py; else { cnt++; } if (cnt >= 2) { ans = 0; break; } } int tmp = 0; for (int i = 1; i <= n; i++) { if (pre[i] == i) tmp++; } if (cnt != 1) ans = 0; if (tmp >= 2) ans = 0; if (ans) puts("FHTAGN!"); else puts("NO"); } ```
### Prompt Please provide a Java coded solution to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.*; import java.util.*; /** * * @author Sourav Kumar Paul */ public class SolveD { public static ArrayList<Integer> adj[]; public static boolean marked[], inStack[]; public static int con[], count =0; public static void main(String[] args) throws IOException{ Reader in = new Reader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int n = in.nextInt(); int m = in.nextInt(); adj = new ArrayList[n]; for(int i=0; i<n; i++) adj[i]= new ArrayList<>(); for(int i=0; i<m; i++) { int x = in.nextInt()-1; int y = in.nextInt()-1; adj[x].add(y); adj[y].add(x); } marked = new boolean[n]; con = new int[n]; Arrays.fill(con,-1); boolean flag = true; int k =0; for(int i=0; i<n; i++) { if(con[i] == -1) { dfs(i,k); k++; } if(k>1) { flag = false; break; } } //out.println(k); if(flag) { //out.println("NOdfsf"); count =0; inStack = new boolean[n]; findDfs(0,-1); if(count ==1) out.println("FHTAGN!"); else out.println("NO"); } else out.println("NO"); out.flush(); out.close(); } private static void dfs(int start, int k) { con[start] = k; for(int v: adj[start]) if(con[v] == -1) dfs(v, k); } private static void findDfs(int start, int parent) { marked[start] = true; inStack[start] = true; for(int v: adj[start]) { if(parent == v) continue; if(marked[v] && inStack[v]) { count++; //System.out.println(v+"fdsf"+start); } else if(!marked[v]) findDfs(v,start); } inStack[start] = false; } public static class Reader { public BufferedReader reader; public StringTokenizer st; public Reader(InputStreamReader stream) { reader = new BufferedReader(stream); st = null; } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public String nextLine() throws IOException{ return reader.readLine(); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } } } ```
### Prompt Develop a solution in Java to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.Scanner; public class p3b { public static void main(String[]args) { Scanner sc = new Scanner(System.in); String yes="FHTAGN!"; String no="NO"; int N=sc.nextInt(); int M=sc.nextInt(); int adj[][]=new int[N][N]; for(int i=0;i<M;i++) { int x=sc.nextInt()-1; int y=sc.nextInt()-1; adj[x][y]=adj[y][x]=1; } boolean marked[] = new boolean[N]; dfs(adj,marked,0); for(int i=0;i<N;i++) { if(!marked[i]) { System.out.println(no); return; } } if(N==M && N > 2) { System.out.println(yes); } else { System.out.println(no); } } private static void dfs(int[][] adj, boolean[] marked, int start) { for(int i=0;i<adj[start].length;i++) { if(adj[start][i]==1) { if(marked[i]==false) { marked[i]=true; dfs(adj,marked,i); } } } } } ```
### Prompt Please provide a Java coded solution to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { static int arr[],size[]; static int n,count,m; public static int root(int k) { while(k!=arr[k]) { arr[k]=arr[arr[k]]; k=arr[k]; } return k; } public static void union(int i,int j) { int p=root(i); int q=root(j); if(p==q); else if(size[p]<size[q]) { arr[p]=q; size[q]+=size[p]; count--; } else { arr[q]=p; size[p]+=size[q]; count--; } } public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; st=new StringTokenizer(br.readLine()); n=Integer.parseInt(st.nextToken()); m=Integer.parseInt(st.nextToken()); arr=new int[n]; size=new int[n]; count=n; for(int i=0;i<n;i++) { arr[i]=i; size[i]=1; } if(n!=m) { System.out.print("NO"); return ; } for(int i=0;i<m;i++) { st=new StringTokenizer(br.readLine()); union(Integer.parseInt(st.nextToken())-1,Integer.parseInt(st.nextToken())-1); } if(count!=1) System.out.print("NO"); else System.out.print("FHTAGN!"); } } ```
### Prompt Your task is to create a cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, ff, nn; bool vis[105] = {0}; vector<int> eg[105]; void dfs(int u, int pr, int st) { vis[u] = 1; for (int i = 0; i < eg[u].size(); i++) { int v = eg[u][i]; if (!vis[v]) dfs(v, u, st); else if (v != u && v != pr && v == st) ff++; } } void jg(int u) { vis[u] = 1; nn++; for (int i = 0; i < eg[u].size(); i++) { int v = eg[u][i]; if (!vis[v]) jg(v); } } int main() { scanf("%d%d", &n, &m); for (int i = 1, a, b; i <= m; i++) { scanf("%d%d", &a, &b); eg[a].push_back(b); eg[b].push_back(a); } nn = 0; jg(1); if (nn != n) { puts("NO"); return 0; } int ff2 = 0; for (int i = 1; i <= n; i++) { memset(vis, 0, sizeof(vis)); ff = 0; dfs(i, i, i); if (ff == 1) ff2 = 1; else if (ff > 1 && ff2) ff2 = 2; } if (ff2 == 1) puts("FHTAGN!"); else puts("NO"); return 0; } ```
### Prompt Please create a solution in JAVA to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.LinkedList; import java.util.Scanner; public class C_120 { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int m=in.nextInt(); boolean[][] mas=new boolean[n][n]; for(int i=0;i<m;i++){ int x=in.nextInt(); int y=in.nextInt(); mas[x-1][y-1]=true; mas[y-1][x-1]=true; } boolean[] link=new boolean[n]; LinkedList<Integer> list=new LinkedList<Integer>(); list.addLast(0); link[0]=true; int count=0; while(!list.isEmpty()){ int i=list.removeFirst(); for(int j=0;j<n;j++) if(mas[i][j]){ if(link[j]) count++; else{ list.addLast(j); link[j]=true; } mas[i][j]=false; mas[j][i]=false; } } boolean isConn=true; for(int i=0;i<n;i++) if(!link[i]){ isConn=false; break; } if(count==1 && isConn) System.out.println("FHTAGN!"); else System.out.println("NO"); } } ```
### Prompt Generate a Python3 solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```python3 n, m = [int(i) for i in input().split()] adj = [[] for i in range(n+1)] seen = [False for i in range(n+1)] pai = [0 for i in range(n+1)] ciclos = 0 def dfs (u): seen[u] = True global ciclos for v in adj[u]: if not seen[v]: pai[v] = u dfs(v) elif v != pai[u]: ciclos += 1 for i in range(m): x, y = [int(i) for i in input().split()] adj[x].append(y) adj[y].append(x) dfs(1) conexo = True for i in range(1, n+1, 1): if not seen[i]: conexo = False if conexo and ciclos/2 == 1: print('FHTAGN!') else: print('NO') exit(0) ```
### Prompt In Java, your task is to solve the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.StringTokenizer; public class Main { private BufferedReader input; private StringTokenizer stoken = new StringTokenizer(""); public static void main(String[] args) throws IOException { new Main(); } Main() throws IOException{ /* input = new BufferedReader( new FileReader("input.txt") ); */ input = new BufferedReader( new InputStreamReader(System.in) ); int n = nextInt(); int m = nextInt(); int mas [][] = new int [n][n]; for (int i=0; i<m; i++){ int x = nextInt()-1; int y = nextInt()-1; mas[x][y] = 1; mas[y][x] = 1; } boolean key = bfs(mas, n); System.out.print( (n==m) && (key) ? "FHTAGN!" : "NO"); input.close(); } private boolean bfs(int[][] mas, int n) { LinkedList<Integer> queue = new LinkedList<Integer>(); boolean visited [] = new boolean [n]; visited[0] = true; queue.add(0); int count_visited = 1; while(!queue.isEmpty()){ int k = queue.pop(); for (int i=0; i<n; i++){ if ( (mas[k][i]==1) && (!visited[i]) ){ visited[i] = true; queue.add(i); count_visited++; } } } return( count_visited==n ? true : false); } private int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextString()); } private String nextString() throws IOException { while (!stoken.hasMoreTokens()){ String st = input.readLine(); stoken = new StringTokenizer(st); } return stoken.nextToken(); } } ```
### Prompt In Cpp, your task is to solve the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct DSU { DSU() = default; DSU(int n) { v.resize(n); for (int i = 0; i < (int)n; i++) { v[i] = make_pair(i, 1); } } int getLeader(int a) { while (v[a].first != a) { a = v[a].first; } return a; } void merge(int a, int b) { int A = getLeader(a); int B = getLeader(b); if (A == B) return; int cntA = v[A].second; int cntB = v[B].second; if (cntA > cntB) { v[B].first = A; v[A].second += v[B].second; } else { v[A].first = B; v[B].second += v[A].second; } } bool isConnected() { int commonLeader = getLeader(0); for (int i = 1; i < (int)v.size(); i++) { if (getLeader(i) != commonLeader) return false; } return true; } vector<pair<int, int>> v; }; int main() { int n, m; cin >> n >> m; DSU dsu(n); int u, v; for (int i = 0; i < (int)m; i++) { cin >> u >> v; u--; v--; dsu.merge(u, v); } string ans = (n == m && dsu.isConnected()) ? "FHTAGN!" : "NO"; cout << ans << endl; return 0; } ```
### Prompt Please provide a Java coded solution to the problem described below: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java //103B //Cthulhu import java.util.*; public class Cthulhu { static int nodes; static boolean[] visited; static ArrayList<Integer>[] adjList; public static void dfs(int cur) { nodes++; visited[cur] = true; for(int nxt : adjList[cur]) if(!visited[nxt]) dfs(nxt); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); visited = new boolean[n + 1]; adjList = new ArrayList[n + 1]; if(n < 3 || n != m) System.out.println("NO"); else { for(int i = 1; i < n + 1; i++) adjList[i] = new ArrayList<>(); while(m-->0) { int x = sc.nextInt(); int y = sc.nextInt(); adjList[x].add(y); adjList[y].add(x); } dfs(1); if(nodes == n) System.out.println("FHTAGN!"); else System.out.println("NO"); } } } ```
### Prompt Your challenge is to write a Python3 solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```python3 n, m = map(int, input().split()) if n < 3 or n != m: print('NO') exit() e, f = [[] for i in range(n + 1)], set() for j in range(m): x, y = map(int, input().split()) e[x].append(y) e[y].append(x) def dfs(x): f.add(x) for y in e[x]: if not y in f: dfs(y) dfs(1) print('FHTAGN!' if len(f) == n else 'NO') # Made By Mostafa_Khaled ```
### Prompt Your challenge is to write a PYTHON3 solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```python3 import sys def getroot(lab, u): if lab[u] == -1: return u lab[u] = getroot(lab, lab[u]) return lab[u] def union(lab, cou, a, b): if cou[a] > cou[b]: cou[a] += cou[b] lab[b] = a else: cou[b] += cou[a] lab[a] = b def inp(): return map(int, input().split()) def solve(): n, m = inp() lab = [-1 for i in range(n)] cou = [1 for i in range(n)] if n != m: print("NO") #impossible return for i in range(m): u, v = inp() u = getroot(lab, u-1) v = getroot(lab, v-1) if u != v: union(lab, cou, u, v) if lab.count(-1) > 1: #not connected print("NO") return print("FHTAGN!") solve() ```
### Prompt Your task is to create a Cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long inf = 1e8; const double pi = 3.1415926535897; const double EPS = (1e-7); int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; } struct edge { int from, to, w; edge(int u, int v, int w) : from(u), to(v), w(w) {} bool operator<(const edge &e) const { return w > e.w; } }; const int N = 105; int par[N]; int get(int u) { return par[u] == u ? u : get(par[u]); } void unite(int u, int v) { int par1 = get(u); int par2 = get(v); if (par1 != par2) { par[par2] = par1; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) par[i] = i; int x, y; for (int i = 0; i < m; i++) { cin >> x >> y; x--; y--; unite(x, y); } int ans = 0; for (int i = 0; i < n; i++) { if (par[i] == i) ans++; } if (ans == 1 && n == m) { cout << "FHTAGN!"; } else cout << "NO"; } ```
### Prompt Create a solution in cpp for the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; long long gcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long x1, y1; long long g = gcd(b, a % b, x1, y1); x = y1; y1 = x1 - y1 * (a / b); return g; } long long qpow(long long a, long long n) { long long res = 1; while (n) { if (n % 2) { res = (res * a) % MOD; n--; } else { n = n / 2; a = (a * a) % MOD; } } return res; } bool cmp(pair<pair<int, int>, int> &p1, pair<pair<int, int>, int> &p2) { if (p1.first.second != p2.first.second) return p1.first.second > p2.first.second; if (p1.first.first != p2.first.first) return p1.first.first < p2.first.first; return p1.second < p2.second; } bool prime(long long n) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } void dfs(vector<vector<int>> &list, vector<int> &vis, int node) { int n = list.size() - 1; vis[node] = 1; for (int j = 0; j < list[node].size(); j++) { if (vis[list[node][j]] == 0) dfs(list, vis, list[node][j]); } } void solve() { int n, m; cin >> n >> m; vector<vector<int>> list(n + 1); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; list[u].push_back(v); list[v].push_back(u); } vector<int> vis(n + 1); int cnt = 0; for (int i = 1; i <= n; i++) { if (vis[i] == 0) { dfs(list, vis, i); cnt++; } } if (cnt == 1 && n == m) cout << "FHTAGN!" << '\n'; else cout << "NO" << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t = 1; while (t--) { solve(); } cerr << "Time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << '\n'; return 0; } ```
### Prompt In CPP, your task is to solve the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m; vector<long long> G[110]; bool visited[110]; int visitededge; void dfs(int x) { if (visited[x]) return; visitededge++; visited[x] = true; for (int i = 0; i < G[x].size(); i++) { dfs(G[x][i]); } } int main() { cin >> n >> m; if (n != m) { cout << "NO"; return 0; } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } dfs(1); if (visitededge < n) cout << "NO"; else cout << "FHTAGN!"; return 0; } ```
### Prompt Your challenge is to write a Cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> a[105]; int n, m, v[105]; void dfs(int s, int l) { v[s] = true; for (int i = 0; i < (int)a[s].size(); i++) if (v[a[s][i]] == 0 && a[s][i] != l) dfs(a[s][i], s); } int main() { cin >> n >> m; int x, y; for (int i = 0; i < m; i++) { cin >> x >> y; a[x].push_back(y); a[y].push_back(x); } memset(v, 0, sizeof(v)); if (m == n) { int cnt = 0; dfs(x, 0); for (int i = 0; i < 105; i++) if (v[i] != 0) cnt++; if (cnt == n) cout << "FHTAGN!" << endl; else cout << "NO" << endl; } else cout << "NO" << endl; } ```
### Prompt Your challenge is to write a Python solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```python from collections import defaultdict #def gcd(a,b): # while b > 0: a,b = b, a%b # return a #def lcm(a, b): # return a*b/gcd(a,b) n, m = map(int, raw_input().split()) if n != m: print "NO" exit() else: pol = defaultdict(list) for i in xrange(m): x, y = map(int, raw_input().split()) pol[x].append(y) pol[y].append(x) curin = set(pol[x]) wasin = set([x]) while curin: n_curin = set() for i in curin: for j in pol[i]: if j not in wasin: n_curin.add(j) wasin.add(i) curin = n_curin if len(wasin) == n: print "FHTAGN!" else: print "NO" ```
### Prompt Your challenge is to write a java solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.*; import java.io.*; import java.math.BigInteger; public class Tests{ static Scanner in = new Scanner (System.in); //static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter (System.out); public static void main (String []args) throws IOException { int n = in.nextInt(); int m = in.nextInt(); if(n!=m){ out.println("NO"); out.flush(); System.exit(0); } DUS dus = new DUS(n); for(int i = 0 ; i<m ; i++){ int u = in.nextInt()-1; int v = in.nextInt()-1 ; dus.union(u, v); } for(int i = 1 ; i<n ;i++){ if(dus.find(i) != dus.find(i-1)){ out.println("NO"); out.close(); System.exit(0); } } out.println("FHTAGN!"); out.flush(); out.close(); } static class DUS { final int[] data; public DUS(int size) { data = new int[size]; for (int i = 0; i < size; i++) { data[i] = i; } } int find(int x) { if (data[x] == x) return x; else return data[x] = find(data[x]); } void union(int x, int y) { data[find(x)] = data[find(y)]; } } } ```
### Prompt Generate a Cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dfs(int a[100][100], int n) { int v[n]; int x[n]; memset(v, 0, sizeof(v)); memset(x, 0, sizeof(x)); stack<int> s; int c; int cnt = 0; c = 0; s.push(c); while (s.size() > 0) { c = s.top(); s.pop(); x[c] = 1; v[c] = 1; for (int i = 0; i < n; i++) { if (a[c][i] == 1) { if (v[i] == 1 && x[i] == 0) { cnt++; } if (v[i] == 0) { s.push(i); v[i] = 1; } } } } for (int i = 0; i < n; i++) { if (x[i] == 0) { return 0; } } return cnt; } int main() { int n, m; int u, w; cin >> n >> m; int a[100][100]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) a[i][j] = 0; } for (int i = 0; i < m; i++) { cin >> u >> w; a[u - 1][w - 1] = 1; a[w - 1][u - 1] = 1; } int cnt = dfs(a, n); if (cnt == 1) cout << "FHTAGN!\n"; else cout << "NO\n"; return 0; } ```
### Prompt Please formulate a JAVA solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.Scanner; public class C { //AC static ArrayList<ArrayList<Integer>> graph; static LinkedList<Integer> cycle; static int[] vis, parent; public static void dfs(int cur, int pnt) { if (cycle.size() > 0) return; if (vis[cur] != 0) { while (pnt != cur) { cycle.add(pnt); pnt = parent[pnt]; } cycle.add(cur); return; } vis[cur] = 1; parent[cur] = pnt; for (Integer Int : graph.get(cur)) { if (Int == pnt) continue; dfs(Int, cur); } } public static boolean isTreeRoot(int cur, int parent) { if (vis[cur] > 1 || vis[cur] == 1 && parent != -1 && vis[parent] == 2) { return false; } else if (vis[cur] == 1 && parent != -1) { return true; } if (vis[cur] == 0) vis[cur] = 2; boolean can = true; for (Integer Int : graph.get(cur)) { if (Int == parent) continue; can = can & isTreeRoot(Int, cur); } return can; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int nodes = sc.nextInt(); int edges = sc.nextInt(); graph = new ArrayList<ArrayList<Integer>>(); for (int i = 0; i < nodes; i++) { graph.add(new ArrayList<Integer>()); } for (int i = 0; i < edges; i++) { int source = sc.nextInt() - 1; int dest = sc.nextInt() - 1; graph.get(source).add(dest); graph.get(dest).add(source); } cycle = new LinkedList<Integer>(); vis = new int[nodes]; parent = new int[nodes]; dfs(0, -1); if (cycle.size() < 3) { System.out.println("NO"); return; } vis = new int[nodes]; Arrays.fill(parent, -1); for (Integer e : cycle) { vis[e] = 1; } boolean valid = true; while (!cycle.isEmpty()) { if (!isTreeRoot(cycle.remove(), -1)) { valid = false; break; } } for (int i = 0; i < nodes; i++) { if (vis[i] == 0) valid = false; } if (valid) { System.out.println("FHTAGN!"); } else { System.out.println("NO"); } } } ```
### Prompt In CPP, your task is to solve the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long int; using ld = long double; inline int in() { int n; scanf("%d", &n); return n; } const ll maxn = 101; vector<int> adj[maxn]; bool visited[maxn]; void dfs(int v) { visited[v] = true; for (int u : adj[v]) if (!visited[u]) dfs(u); } int main() { int n = in(), m = in(); for (int i = 0; i < m; i++) { int u = in() - 1, v = in() - 1; adj[u].push_back(v); adj[v].push_back(u); } int cnt = 0; for (int v = 0; v < n; v++) if (!visited[v]) { cnt++; dfs(v); } cnt == 1 and n == m ? cout << "FHTAGN!\n" : cout << "NO\n"; } ```
### Prompt Your task is to create a cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int vis[1000] = {0}; vector<int> v[1000]; void paltu(int x) { if (vis[x] == 1) { return; } vis[x] = 1; for (int i = 0; i < v[x].size(); i++) { paltu(v[x][i]); } } int main() { int n, m, i, a, b; cin >> n >> m; for (i = 0; i < m; i++) { cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } if (n != m) { cout << "NO"; return 0; } paltu(1); for (i = 1; i <= n; i++) { if (vis[i] == 0) { cout << "NO"; return 0; } } cout << "FHTAGN!"; return 0; } ```
### Prompt In Java, your task is to solve the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java /** * JUDGE_ID : 104262PN * User : Денис * Date : 07.08.11 * Time : 13:31 * ICQ : 785625 * Email : popokus@gmail.com */ import java.io.*; import java.util.Stack; public class s80_c { public static void main(String[] args) throws IOException { new s80_c().run(); } int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } String nextString() throws IOException { in.nextToken(); return (String) in.sval; } StreamTokenizer in; Writer writer; Reader reader; int n, m; int[][] graf; int k = 0; int cycleCnt = 0; boolean[][] was; void run() throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; reader = oj ? new InputStreamReader(System.in, "ISO-8859-1") : new FileReader("input/is80_c.txt"); writer = new OutputStreamWriter(System.out, "ISO-8859-1"); in = new StreamTokenizer(new BufferedReader(reader)); PrintWriter out = new PrintWriter(writer); n = nextInt(); m = nextInt(); graf = new int[n + 1][n + 1]; was = new boolean[n+1][n+1]; boolean[] visited = new boolean[n + 1]; Stack<Integer> vertexes = new Stack<Integer>(); int visitedCount = 0; boolean is = false; for (int i = 1; i <= m; i++) { int l = nextInt(); int o = nextInt(); graf[l][o] = 1; graf[o][l] = 1; } vertexes.push(1); while (!vertexes.isEmpty()) { int current = vertexes.pop(); if (visited[current]) { if (!is) { is = true; } else { out.println("NO"); out.flush(); out.close(); return; } } else { visitedCount++; visited[current] = true; } boolean has = false; for (int i = 1; i <= n; i++) { if (graf[i][current] == 1) has = true; if (graf[i][current] == 1 && !was[i][current]) { was[i][current] = true; was[current][i] = true; if (!visited[i]) { vertexes.push(i); } } } if (!has) { out.println("NO"); out.flush(); out.close(); return; } if (vertexes.isEmpty() && visitedCount < n) { for (int i = 1; i <= n; i++) { if (!visited[i]) { vertexes.push(i); break; } } } } if (is) out.println("FHTAGN!"); else out.println("NO"); out.flush(); out.close(); } } ```
### Prompt Your task is to create a Cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int pole[105][105]; int stav[105]; int pathFrom[105]; int main() { int N, M; cin >> N >> M; for (int i = 0; i < M; i++) { int x, y; cin >> x >> y; pole[x][y] = 1; pole[y][x] = 1; } queue<int> OPEN; OPEN.push(1); stav[1] = 1; int Cycle = 0; int cnt = 0; while (!OPEN.empty()) { int current = OPEN.front(); OPEN.pop(); for (int i = 1; i <= N; i++) { if (pole[current][i] == 1 && pole[current][i] != i) { int nei = i; if (Cycle == 2 && stav[nei] > 1 && pathFrom[current] != nei) { cout << "NO" << endl; return 0; } if (Cycle < 2 && stav[nei] > 1 && pathFrom[current] != nei) { Cycle++; } if (stav[nei] == 0) { OPEN.push(nei); stav[nei] = 1; pathFrom[nei] = current; } } } stav[current] = 2; cnt++; } if (cnt != N || Cycle < 1) { cout << "NO" << endl; return 0; } cout << "FHTAGN!" << endl; return 0; } ```
### Prompt Please formulate a Cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<long long> graph[105]; bool vis[105]; void dfs(long long node) { vis[node] = true; for (auto child : graph[node]) { if (vis[child] == false) dfs(child); } } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(NULL); clock_t clk = clock(); long long n, m; cin >> n >> m; for (long long i = 1; i <= m; i++) { long long a, b; cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } memset(vis, false, sizeof(vis)); dfs(1); long long cnt = 0; for (long long i = 1; i <= n; i++) { if (!vis[i]) cnt++; } if (cnt > 0 || n != m) cout << "NO" << '\n'; else cout << "FHTAGN!" << '\n'; cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n'; } ```
### Prompt Create a solution in JAVA for the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.StringTokenizer; public class ktulhu implements Runnable { StringTokenizer st; BufferedReader in; PrintWriter out; public static void main(String[] args) { new Thread(new ktulhu()).start(); } public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); } catch (Exception e) { out.println(e); System.exit(1); } finally { out.flush(); out.close(); } } String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } void solve() throws NumberFormatException, IOException { int v = nextInt(); int e = nextInt(); int[][] a = new int[v + 1][v + 1]; int[] p = new int[v + 1]; int cycles = 0; boolean[] u = new boolean[v + 1]; for(int i = 0; i < e; ++i) { int f = nextInt(); int s = nextInt(); a[f][s] = 1; a[s][f] = 1; } LinkedList<Integer> q = new LinkedList<Integer>(); q.add(1); u[1] = true; while(!q.isEmpty()) { int ver = q.poll(); u[ver] = true; for(int i = 1; i <= v; ++i) { if(a[ver][i] == 1) { if(!u[i]) { p[i] = ver; u[i] = true; q.addLast(i); } else { if(p[ver] != i) { cycles++; } } } } } String result = "FHTAGN!"; for(int i = 1; i <= v; ++i) { if(!u[i]) { result = "NO"; break; } } if(cycles != 2) { result = "NO"; } out.println(result); } } ```
### Prompt Your task is to create a cpp solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<vector<int> > g(105); int v[101]; int dfs(int root) { v[root] = 1; for (int i = 0; i < g[root].size(); i++) { if (v[g[root][i]] == 0) dfs(g[root][i]); } } int main() { int n, m; cin >> n >> m; int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } if (n != m) { cout << "NO"; return 0; } dfs(1); for (int i = 1; i <= n; i++) { if (v[i] == 0) { cout << "NO"; return 0; } } cout << "FHTAGN!"; return 0; } ```
### Prompt Please formulate a java solution to the following problem: ...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. <image> Input The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ <image>). 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. Examples 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 and 2, 2 and 3, ..., v - 1 and v, v 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. ### Response ```java import java.util.ArrayList; import java.util.BitSet; import java.util.Scanner; import java.io.IOException; public class Task2 { static int calls; static BitSet visi; static ArrayList<Integer>[] adj; static void dfs(int node){ ++calls; visi.set(node); for (int child :adj[node]) { if (!visi.get(child))dfs(child); } } public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); if (m!=n) System.out.printf("%s","NO"); else{ adj= new ArrayList[n]; visi= new BitSet(n); for (int i =0;i<n;++i)adj[i]=new ArrayList(); for (int i = 0; i < m; i++) { int x= in.nextInt()-1,y=in.nextInt()-1; adj[x].add(y); adj[y].add(x); } dfs(0); if (calls==n)System.out.printf("%s","FHTAGN!"); else System.out.printf("%s","NO"); } } } ```