Submission #925386

# Submission time Handle Problem Language Result Execution time Memory
925386 2024-02-11T14:15:36 Z boris_mihov Cats or Dogs (JOI18_catdog) C++17
38 / 100
3000 ms 7688 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] = (a[node] == (type ^ 1));

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

    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));
}

/*
9
5 3
4 9
5 4
4 1
5 7
7 6
4 8
2 3
6
2 9
2 5
1 1
2 2
1 4
1 3
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4696 KB Output is correct
3 Correct 1 ms 4696 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4576 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4696 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4696 KB Output is correct
3 Correct 1 ms 4696 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4576 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4696 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 11 ms 4700 KB Output is correct
18 Correct 13 ms 4700 KB Output is correct
19 Correct 8 ms 4940 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 3 ms 4700 KB Output is correct
22 Correct 3 ms 4696 KB Output is correct
23 Correct 16 ms 4776 KB Output is correct
24 Correct 15 ms 4700 KB Output is correct
25 Correct 7 ms 4700 KB Output is correct
26 Correct 3 ms 4700 KB Output is correct
27 Correct 3 ms 4700 KB Output is correct
28 Correct 4 ms 4700 KB Output is correct
29 Correct 19 ms 4832 KB Output is correct
30 Correct 3 ms 4700 KB Output is correct
31 Correct 2 ms 4700 KB Output is correct
32 Correct 4 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4696 KB Output is correct
3 Correct 1 ms 4696 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4576 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4696 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 11 ms 4700 KB Output is correct
18 Correct 13 ms 4700 KB Output is correct
19 Correct 8 ms 4940 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 3 ms 4700 KB Output is correct
22 Correct 3 ms 4696 KB Output is correct
23 Correct 16 ms 4776 KB Output is correct
24 Correct 15 ms 4700 KB Output is correct
25 Correct 7 ms 4700 KB Output is correct
26 Correct 3 ms 4700 KB Output is correct
27 Correct 3 ms 4700 KB Output is correct
28 Correct 4 ms 4700 KB Output is correct
29 Correct 19 ms 4832 KB Output is correct
30 Correct 3 ms 4700 KB Output is correct
31 Correct 2 ms 4700 KB Output is correct
32 Correct 4 ms 4700 KB Output is correct
33 Execution timed out 3068 ms 7688 KB Time limit exceeded
34 Halted 0 ms 0 KB -