From e4e338fdbff3d92c86abf0152011271471690032 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 12 Sep 2022 21:37:01 +0800 Subject: [PATCH] =?UTF-8?q?1987=20-=20=E5=B0=8F=E4=B8=91=E7=AB=9F=E5=9C=A8?= =?UTF-8?q?=E6=88=91=E8=BA=AB=E8=BE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 77475 --- bjtu/1987/1987.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 bjtu/1987/1987.cpp diff --git a/bjtu/1987/1987.cpp b/bjtu/1987/1987.cpp new file mode 100644 index 00000000..89d392ce --- /dev/null +++ b/bjtu/1987/1987.cpp @@ -0,0 +1,48 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e4 + 5; + +int n, m, cnt, cnt2; +std::vector g[N]; +bool vis[N], flag = true; + +void dfs(int u) { + cnt++; + vis[u] = true; + + for (int v : g[u]) { + if (!vis[v]) dfs(v); + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1, u, v; i <= m; i++) { + cin >> u >> v; + + g[u].emplace_back(v); + g[v].emplace_back(u); + } + + for (int i = 1; i <= n; i++) { + if (!vis[i]) { + int pre = cnt; + dfs(i); + + if (cnt - pre > 1) cnt2++; + } + } + + cout << (cnt2 <= 1 ? "YES" : "NO") << endl; + + return 0; +}