답안 #956146

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
956146 2024-04-01T07:42:35 Z caterpillow Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 5728 KB
#include <bits/stdc++.h>
#include "catdog.h"

using namespace std;

using ll = long long;
using pl = pair<ll, ll>;
#define vt vector
#define f first
#define s second
#define all(x) x.begin(), x.end() 
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
#define debug(x) do{auto _x = x; cerr << #x << " = " << _x << endl;} while(0)
const ll INF = 1e18;

int n;
vt<vt<int>> adj;
vt<int> state;

pl dfs(int u, int par = -1) {
    ll cat = 0, dog = 0;
    for (int v : adj[u]) {
        if (v == par) continue;
        pl res = dfs(v, u);
        cat += res.f;
        dog += res.s;
    }
    pl res;
    if (state[u] == 1) dog = INF;
    if (state[u] == 2) cat = INF;
    res = {min(cat, dog + 1), min(cat + 1, dog)};
    return res;
}

int solve() {
    pl res = dfs(0);
    return min(res.f, res.s);
}

void initialize(int N, vt<int> A, vt<int> B) {
    n = N;
    adj.resize(n);
    state.resize(n);
    F0R (i, n - 1) {
        adj[A[i] - 1].pb(B[i] - 1);
        adj[B[i] - 1].pb(A[i] - 1);
    }
}

int cat(int v) {
    v--;
    state[v] = 1;
    return solve();
}

int dog(int v) {
    v--;
    state[v] = 2;
    return solve();
}

int neighbor(int v) {
    v--;
    state[v] = 0;
    return solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 432 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 432 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 5 ms 348 KB Output is correct
18 Correct 6 ms 348 KB Output is correct
19 Correct 3 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 7 ms 348 KB Output is correct
24 Correct 5 ms 348 KB Output is correct
25 Correct 3 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 2 ms 348 KB Output is correct
29 Correct 8 ms 564 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 432 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 5 ms 348 KB Output is correct
18 Correct 6 ms 348 KB Output is correct
19 Correct 3 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 7 ms 348 KB Output is correct
24 Correct 5 ms 348 KB Output is correct
25 Correct 3 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 2 ms 348 KB Output is correct
29 Correct 8 ms 564 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Execution timed out 3054 ms 5728 KB Time limit exceeded
34 Halted 0 ms 0 KB -