답안 #680301

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
680301 2023-01-10T14:27:32 Z mjhmjh1104 Wells (CEOI21_wells) C++17
0 / 100
20 ms 35540 KB
#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;
        }
        ds[i] = 0, dfs_depth(i);
        int cnt = 0;
        for (int j = 0; j < n; j++) if (depth[j] == k / 2) cnt++;
        if (cnt != (int)adj[i].size()) b[i] = 1;
        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;
    ds[k] = 0, dfs_depth(k);
    puts(dfs(k, -1) ? "YES\n1" : "NO\n0");
}

Compilation message

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);
      |         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Partially correct 19 ms 35540 KB Output is partially correct
2 Incorrect 20 ms 35540 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Partially correct 19 ms 35540 KB Output is partially correct
2 Incorrect 20 ms 35540 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Partially correct 19 ms 35540 KB Output is partially correct
2 Incorrect 20 ms 35540 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Partially correct 19 ms 35540 KB Output is partially correct
2 Incorrect 20 ms 35540 KB Output isn't correct
3 Halted 0 ms 0 KB -