답안 #379814

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
379814 2021-03-19T11:08:30 Z Vimmer Svjetlo (COCI20_svjetlo) C++14
110 / 110
435 ms 83328 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")

#define N 500500
#define NN 10000050
#define PB push_back
#define M ll(1e9 + 7)
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define pri(x) cout << x << endl
#define endl '\n'
#define _ << " " <<
#define F first
#define S second

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
//typedef tree <ll, null_type, less_equal <ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

typedef long double ld;
typedef unsigned long long ull;
typedef short int si;

vector <int> g[N];

string s;

int f[N][2][3], n;

int dp[2][3];

void solve_0(int v, int u)
{
    for (int cur = 0; cur < 2; cur++)
    {
        /// he 0
        dp[cur][0] = min(dp[cur][0], f[v][cur][0] + f[u][0][0] + 3);

        /// he 1
        dp[cur ^ 1][0] = min(dp[cur ^ 1][0], f[v][cur][0] + f[u][1][0] + 1);
    }
}

void solve_1(int v, int u)
{
    for (int cur = 0; cur < 2; cur++)
    {
        dp[cur][1] = min(dp[cur][1], f[v][cur][1] + f[u][0][0] + 3);

        dp[cur ^ 1][1] = min(dp[cur ^ 1][1], f[v][cur][1] + f[u][1][0] + 1);

        dp[cur][1] = min(dp[cur][1], f[v][cur][0] + min(f[u][1][0], f[u][1][1]));
        dp[cur ^ 1][1] = min(dp[cur ^ 1][1], f[v][cur][0] + min(f[u][0][0], f[u][0][1]) + 2);
    }
}

void solve_2(int v, int u)
{
    for (int cur = 0; cur < 2; cur++)
    {
        dp[cur][2] = min(dp[cur][2], f[v][cur][2] + f[u][0][0] + 3);
        dp[cur ^ 1][2] = min(dp[cur ^ 1][2], f[v][cur][2] + f[u][1][0] + 1);

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

        dp[cur][2] = min(dp[cur][2], f[v][cur][1] + min(f[u][1][1], f[u][1][0]));
        dp[cur ^ 1][2] = min(dp[cur ^ 1][2], f[v][cur][1] + min(f[u][0][1], f[u][0][0]) + 2);
    }
}

void dfs(int v, int p)
{
    bool have = 0;

    for (auto it : g[v])
    {
        if (it == p) continue;

        dfs(it, v);

        if (f[it][1][0] == 0) continue;

        have = 1;
    }

    if (!have && s[v] == '1')
    {
        f[v][1][0] = 0;

        return;
    }

    f[v][1 ^ (s[v] == '1')][0] = 1;

    for (auto it : g[v])
    {
        if (it == p || f[it][1][0] == 0) continue;

        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 3; j++)
                dp[i][j] = 1e9;

        solve_0(v, it);
        solve_1(v, it);
        solve_2(v, it);

        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 3; j++)
                f[v][i][j] = dp[i][j];
    }
}

int main()
{
    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

//    freopen("1.in", "r", stdin);

    for (int i = 0; i < N; i++)
        for (int t = 0; t < 2; t++)
            for (int l = 0; l < 3; l++)
                f[i][t][l] = 1e9;

    cin >> n;

    cin >> s;

    for (int i = 1; i < n; i++)
    {
        int x, y;

        cin >> x >> y;

        x--; y--;

        g[x].PB(y);

        g[y].PB(x);
    }

    for (int i = 0; i < n; i++)
    {
        if (s[i] == '1') continue;

        dfs(i, -1);

        pri(min(min(f[i][1][0], f[i][1][1]), f[i][1][2]));

        exit(0);
    }

    pri(0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23788 KB Output is correct
2 Correct 15 ms 23788 KB Output is correct
3 Correct 15 ms 23788 KB Output is correct
4 Correct 15 ms 23788 KB Output is correct
5 Correct 15 ms 23788 KB Output is correct
6 Correct 15 ms 23788 KB Output is correct
7 Correct 15 ms 23788 KB Output is correct
8 Correct 15 ms 23788 KB Output is correct
9 Correct 16 ms 23788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 268 ms 59520 KB Output is correct
2 Correct 398 ms 79360 KB Output is correct
3 Correct 388 ms 83328 KB Output is correct
4 Correct 349 ms 63488 KB Output is correct
5 Correct 392 ms 70784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 321 ms 38912 KB Output is correct
2 Correct 410 ms 46592 KB Output is correct
3 Correct 435 ms 46976 KB Output is correct
4 Correct 317 ms 43136 KB Output is correct
5 Correct 302 ms 46720 KB Output is correct
6 Correct 268 ms 44288 KB Output is correct
7 Correct 328 ms 47348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23788 KB Output is correct
2 Correct 15 ms 23788 KB Output is correct
3 Correct 15 ms 23788 KB Output is correct
4 Correct 15 ms 23788 KB Output is correct
5 Correct 15 ms 23788 KB Output is correct
6 Correct 15 ms 23788 KB Output is correct
7 Correct 15 ms 23788 KB Output is correct
8 Correct 15 ms 23788 KB Output is correct
9 Correct 16 ms 23788 KB Output is correct
10 Correct 268 ms 59520 KB Output is correct
11 Correct 398 ms 79360 KB Output is correct
12 Correct 388 ms 83328 KB Output is correct
13 Correct 349 ms 63488 KB Output is correct
14 Correct 392 ms 70784 KB Output is correct
15 Correct 321 ms 38912 KB Output is correct
16 Correct 410 ms 46592 KB Output is correct
17 Correct 435 ms 46976 KB Output is correct
18 Correct 317 ms 43136 KB Output is correct
19 Correct 302 ms 46720 KB Output is correct
20 Correct 268 ms 44288 KB Output is correct
21 Correct 328 ms 47348 KB Output is correct
22 Correct 355 ms 43904 KB Output is correct
23 Correct 363 ms 45124 KB Output is correct
24 Correct 338 ms 44160 KB Output is correct
25 Correct 357 ms 43008 KB Output is correct
26 Correct 319 ms 48384 KB Output is correct
27 Correct 315 ms 48128 KB Output is correct
28 Correct 259 ms 44864 KB Output is correct
29 Correct 309 ms 47104 KB Output is correct
30 Correct 296 ms 45428 KB Output is correct