From efad306b867915e21eba3f37449575926ba8be4e Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 11 Dec 2021 20:48:04 +0800 Subject: [PATCH] D - Neighbors https://atcoder.jp/contests/abc231/submissions/27839591 --- AtCoder/ABC231/D/D.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 AtCoder/ABC231/D/D.cpp diff --git a/AtCoder/ABC231/D/D.cpp b/AtCoder/ABC231/D/D.cpp new file mode 100644 index 00000000..b2126328 --- /dev/null +++ b/AtCoder/ABC231/D/D.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +int n, m, a, b; +bool vis1[100005], vis2[100005]; +std::vector g[100005]; + +void dfs(int u, int f) { + vis1[u] = 1; + if (vis2[u]) { + printf("No\n"); + exit(0); + } + vis2[u] = true; + for (int v : g[u]) { + if (v == f) continue; + dfs(v, u); + } + vis2[u] = false; +} + +int main() { + scanf("%d%d", &n, &m); + if (m >= n) { + printf("No\n"); + exit(0); + } + for (int i = 1; i <= m; i++) { + scanf("%d%d", &a, &b); + g[a].push_back(b); + g[b].push_back(a); + } + for (int i = 1; i <= n; i++) { + if (!vis1[i]) + dfs(i, i); + } + for (int i = 1; i <= n; i++) { + if (g[i].size() > 2) { + printf("No\n"); + exit(0); + } + } + printf("Yes\n"); + return 0; +}