답안 #756248

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
756248 2023-06-11T11:03:11 Z sugartheanh Svjetlo (COCI20_svjetlo) C++14
0 / 110
15 ms 23924 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 5e5 + 7;
const int INF = 1e9 + 7;

int n, dp[N][2][3], nxtDp[2][3], cnt[N];
string s;
vector<int> adj[N];

void Dfs(int u, int p){
    int state = s[u] - '0';
    cnt[u] = (state == 0);
    for(int v : adj[u]){
        if (v == p)
            continue;
        Dfs(v, u);
        cnt[u] += cnt[v];
    }
    if (cnt[u] == 0)
        return;

    dp[u][state ^ 1][0] = 1;
    dp[u][state ^ 1][1] = 1;
    dp[u][state ^ 1][2] = 1;
    for(int v : adj[u]){
        if (v == p || cnt[v] == 0)
            continue;
        memset(nxtDp, 0x3f, sizeof(nxtDp));

        for(int s = 0; s < 2; s++){
            nxtDp[s][0] = min(nxtDp[s][0], dp[u][s ^ 1][0] + dp[v][1][0] + 1);
            nxtDp[s][0] = min(nxtDp[s][0], dp[u][s][0] + dp[v][0][0] + 3);

            nxtDp[s][1] = min(nxtDp[s][1], dp[u][s ^ 1][1] + dp[v][1][0] + 1);
            nxtDp[s][1] = min(nxtDp[s][1], dp[u][s][1] + dp[v][0][0] + 3);
            nxtDp[s][1] = min(nxtDp[s][1], dp[u][s][0] + dp[v][1][1]);
            nxtDp[s][1] = min(nxtDp[s][1], dp[u][s ^ 1][0] + dp[v][0][1] + 2);

            nxtDp[s][2] = min(nxtDp[s][2], dp[u][s][1] + dp[v][1][1]);
            nxtDp[s][2] = min(nxtDp[s][2], dp[u][s ^ 1][1] + dp[v][0][1] + 2);
            nxtDp[s][2] = min(nxtDp[s][2], dp[u][s][0] + dp[v][0][2] + 1);
            nxtDp[s][2] = min(nxtDp[s][2], dp[u][s ^ 1][0] + dp[v][1][2] + 3);
            nxtDp[s][2] = min(nxtDp[s][2], dp[u][s ^ 1][2] + dp[v][1][0] + 1);
            nxtDp[s][2] = min(nxtDp[s][2], dp[u][s][2] + dp[v][0][0] + 3);

        }
        for(int s = 0; s < 2; s++)
            for(int i = 0; i < 3; i++)
                dp[u][s][i] = nxtDp[s][i];
    }
}

int main(){
    freopen("lightroom.inp","r",stdin);
    freopen("lightroom.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> s;
    s = '*' + s;
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    memset(dp, 0x3f, sizeof(dp));
    int root = 1;
    Dfs(root, -1);
    cout << dp[root][1][2];

    return 0;
}

Compilation message

svjetlo.cpp: In function 'int main()':
svjetlo.cpp:57:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |     freopen("lightroom.inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
svjetlo.cpp:58:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |     freopen("lightroom.out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 23924 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -