답안 #925376

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
925376 2024-02-11T13:56:32 Z boris_mihov Cats or Dogs (JOI18_catdog) C++17
0 / 100
1 ms 4700 KB
#include "catdog.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>

typedef long long llong;
const int MAXN = 100000 + 10;
const int INF  = 1e9;

int n;
int timer;
int a[MAXN];
int dp[MAXN][2];
int bl[MAXN][2];
int parent[MAXN];
std::vector <int> g[MAXN];

void buildDFS(int node, int par)
{
    parent[node] = par;
    for (int &u : g[node])
    {
        if (u == par)
        {
            std::swap(u, g[node].back());
            g[node].pop_back();
            break;
        }
    }

    for (const int &u : g[node])
    {
        buildDFS(u, node);
    }
}

int f(int node, int type)
{
    if (bl[node][type] == timer)
    {
        return dp[node][type];
    }

    bl[node][type] = timer;
    dp[node][type] = 0;

    if (a[node] != (type ^ 1))
    {
        for (const int &u : g[node])
        {
            dp[node][type] += f(u, type);
        }
    } else
    {
        dp[node][type] = 1;
        for (const int &u : g[node])
        {
            dp[node][type] += std::min(f(u, type) + 1, f(u, type ^ 1));
        }
    }

    // std::cout << "dp: " << node << ' ' << type << " = " << dp[nd]
    return dp[node][type];
}

void initialize(int N, std::vector <int> A, std::vector <int> B) 
{
    n = N;
    std::fill(a + 1, a + 1 + n, 2);
    for (int i = 0 ; i < n - 1 ; ++i)
    {
        g[A[i]].push_back(B[i]);
        g[B[i]].push_back(A[i]);
    }

    buildDFS(1, 0);
}

int cat(int v) 
{
    timer++;
    a[v] = 0;   
    return std::min(f(1, 0), f(1, 1));
}

int dog(int v) 
{
    timer++;
    a[v] = 1;
    return std::min(f(1, 0), f(1, 1));
}

int neighbor(int v) 
{
    timer++;
    a[v] = 2;
    return std::min(f(1, 0), f(1, 1));
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Incorrect 1 ms 4696 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Incorrect 1 ms 4696 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Incorrect 1 ms 4696 KB Output isn't correct
3 Halted 0 ms 0 KB -