답안 #124023

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
124023 2019-07-02T11:24:21 Z ksmzzang2003 Cats or Dogs (JOI18_catdog) C++14
0 / 100
4 ms 2680 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 ;
    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;
            ^
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2680 KB Output isn't correct
2 Halted 0 ms 0 KB -