Submission #680306

#TimeUsernameProblemLanguageResultExecution timeMemory
680306mjhmjh1104Wells (CEOI21_wells)C++17
0 / 100
21 ms35540 KiB
#include <cstdio> #include <vector> #include <utility> #include <algorithm> using namespace std; int n, k, K, b[1500006]; vector<int> adj[1500006]; int depth[1500006], ds[1500006]; int dfs_depth(int x, int prev = -1) { for (auto &i: adj[x]) if (i != prev) { ds[i] = ds[x] + 1; depth[x] = max(depth[x], dfs_depth(i, x) + 1); } return depth[x]; } bool dfs(int x, int par, int prev = -1) { if (b[x]) { if (par == -1) par = x; else { int dist = ds[par] - ds[x]; if (dist % K) return false; if (k % 2 == 0) { int r = dist % k; if (b[x] == 3) { if (r) b[x] = 3 - b[par]; else b[x] = b[par]; } else if ((r ? 1 : 0) != (b[par] != b[x])) return false; } par = x; } } for (auto &i: adj[x]) if (i != prev && !dfs(i, par, x)) return false; return true; } int main() { scanf("%d%d", &n, &k); K = k; if (k % 2 == 0) K /= 2; for (int i = 0; i < n - 1; i++) { int u, v; scanf("%d%d", &u, &v); u--, v--; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 0; i < n; i++) if ((int)adj[i].size() > 2) { if (k % 2) { b[i] = 1; continue; } for (int j = 0; j < n; j++) depth[j] = 0; ds[i] = 0, dfs_depth(i); int cnt = 0; for (int j = 0; j < n; j++) if (ds[j] - ds[i] == k / 2) cnt++; if (cnt > (int)adj[i].size()) b[i] = 1; else if (cnt < (int)adj[i].size()) b[i] = 0; else b[i] = 3; } int k = -1; for (int i = 0; i < n; i++) if (b[i] == 1) k = i; if (k == -1) for (int i = 0; i < n; i++) if (b[i]) k = i; if (k == -1) return puts("YES\n1"), 0; b[k] = 1; for (int j = 0; j < n; j++) depth[j] = 0; ds[k] = 0, dfs_depth(k); puts(dfs(k, -1) ? "YES\n1" : "NO\n0"); }

Compilation message (stderr)

wells.cpp: In function 'int main()':
wells.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |     scanf("%d%d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~
wells.cpp:46:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |         scanf("%d%d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...