Submission #124027

# Submission time Handle Problem Language Result Execution time Memory
124027 2019-07-02T11:35:15 Z ksmzzang2003 Cats or Dogs (JOI18_catdog) C++14
38 / 100
3000 ms 12392 KB
#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;
const int MAXQ = 1e5;
const int INF = 987654321;

int N, A[MAXN+10];
int dp[MAXN+10][3];
vector<int> adj[MAXN+10];
int parents[MAXN+10];
void dfs(int,int);
void initialize(int n, vector<int> A, vector<int> B)
{
    int i, j;

    N=n;
    for(i=0; i<N-1; i++)
    {
        adj[A[i]].push_back(B[i]);
        adj[B[i]].push_back(A[i]);
    }
    dfs(1,-1);
}

void dfs(int now, int par)
{
    int i, j;
    if(parents[now]==0) parents[now] = par;
    for(int nxt : adj[now])
    {
        if(nxt==par) continue;
        dfs(nxt, now);

        if(A[now]!=2) dp[now][1]+=min(dp[nxt][1], dp[nxt][2]+1);
        if(A[now]!=1) dp[now][2]+=min(dp[nxt][1]+1, dp[nxt][2]);
    }

    if(A[now]==1) dp[now][2]=INF;
    if(A[now]==2) dp[now][1]=INF;
}

void updfs(int now)
{
    if(now==-1) return ;
    dp[now][1] = dp[now][2] =0;
    for(int nxt : adj[now])
    {
        if(nxt==parents[now]) continue;
        if(A[now]!=2) dp[now][1]+=min(dp[nxt][1], dp[nxt][2]+1);
        if(A[now]!=1) dp[now][2]+=min(dp[nxt][1]+1, dp[nxt][2]);
    }
    if(A[now]==1) dp[now][2]=INF;
    if(A[now]==2) dp[now][1]=INF;

    updfs(parents[now]);
}

int solve(int v)
{
    updfs(v);
    return min(dp[1][2], dp[1][1]);
}

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

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

int neighbor(int v)
{
    A[v]=0;
    return solve(v);
}

Compilation message

catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:20:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j;
            ^
catdog.cpp: In function 'void dfs(int, int)':
catdog.cpp:33:9: warning: unused variable 'i' [-Wunused-variable]
     int i, j;
         ^
catdog.cpp:33:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j;
            ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 4 ms 2680 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Correct 4 ms 2680 KB Output is correct
8 Correct 4 ms 2680 KB Output is correct
9 Correct 4 ms 2684 KB Output is correct
10 Correct 4 ms 2680 KB Output is correct
11 Correct 4 ms 2808 KB Output is correct
12 Correct 4 ms 2680 KB Output is correct
13 Correct 4 ms 2684 KB Output is correct
14 Correct 4 ms 2680 KB Output is correct
15 Correct 4 ms 2680 KB Output is correct
16 Correct 4 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 4 ms 2680 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Correct 4 ms 2680 KB Output is correct
8 Correct 4 ms 2680 KB Output is correct
9 Correct 4 ms 2684 KB Output is correct
10 Correct 4 ms 2680 KB Output is correct
11 Correct 4 ms 2808 KB Output is correct
12 Correct 4 ms 2680 KB Output is correct
13 Correct 4 ms 2684 KB Output is correct
14 Correct 4 ms 2680 KB Output is correct
15 Correct 4 ms 2680 KB Output is correct
16 Correct 4 ms 2680 KB Output is correct
17 Correct 4 ms 2680 KB Output is correct
18 Correct 5 ms 2808 KB Output is correct
19 Correct 5 ms 2680 KB Output is correct
20 Correct 4 ms 2680 KB Output is correct
21 Correct 4 ms 2680 KB Output is correct
22 Correct 4 ms 2684 KB Output is correct
23 Correct 5 ms 2812 KB Output is correct
24 Correct 5 ms 2808 KB Output is correct
25 Correct 5 ms 2680 KB Output is correct
26 Correct 4 ms 2680 KB Output is correct
27 Correct 4 ms 2680 KB Output is correct
28 Correct 5 ms 2808 KB Output is correct
29 Correct 8 ms 2808 KB Output is correct
30 Correct 5 ms 2680 KB Output is correct
31 Correct 5 ms 2808 KB Output is correct
32 Correct 5 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 4 ms 2680 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Correct 4 ms 2680 KB Output is correct
8 Correct 4 ms 2680 KB Output is correct
9 Correct 4 ms 2684 KB Output is correct
10 Correct 4 ms 2680 KB Output is correct
11 Correct 4 ms 2808 KB Output is correct
12 Correct 4 ms 2680 KB Output is correct
13 Correct 4 ms 2684 KB Output is correct
14 Correct 4 ms 2680 KB Output is correct
15 Correct 4 ms 2680 KB Output is correct
16 Correct 4 ms 2680 KB Output is correct
17 Correct 4 ms 2680 KB Output is correct
18 Correct 5 ms 2808 KB Output is correct
19 Correct 5 ms 2680 KB Output is correct
20 Correct 4 ms 2680 KB Output is correct
21 Correct 4 ms 2680 KB Output is correct
22 Correct 4 ms 2684 KB Output is correct
23 Correct 5 ms 2812 KB Output is correct
24 Correct 5 ms 2808 KB Output is correct
25 Correct 5 ms 2680 KB Output is correct
26 Correct 4 ms 2680 KB Output is correct
27 Correct 4 ms 2680 KB Output is correct
28 Correct 5 ms 2808 KB Output is correct
29 Correct 8 ms 2808 KB Output is correct
30 Correct 5 ms 2680 KB Output is correct
31 Correct 5 ms 2808 KB Output is correct
32 Correct 5 ms 2680 KB Output is correct
33 Correct 263 ms 6888 KB Output is correct
34 Correct 112 ms 7800 KB Output is correct
35 Correct 351 ms 6996 KB Output is correct
36 Correct 491 ms 11552 KB Output is correct
37 Correct 23 ms 5112 KB Output is correct
38 Correct 517 ms 12392 KB Output is correct
39 Correct 525 ms 12260 KB Output is correct
40 Correct 633 ms 12312 KB Output is correct
41 Correct 639 ms 12264 KB Output is correct
42 Correct 727 ms 12392 KB Output is correct
43 Correct 562 ms 12388 KB Output is correct
44 Correct 637 ms 12336 KB Output is correct
45 Correct 885 ms 12316 KB Output is correct
46 Correct 596 ms 12388 KB Output is correct
47 Correct 513 ms 12264 KB Output is correct
48 Execution timed out 3039 ms 9488 KB Time limit exceeded
49 Halted 0 ms 0 KB -