답안 #922344

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
922344 2024-02-05T12:26:35 Z Shayan86 Svjetlo (COCI20_svjetlo) C++14
110 / 110
366 ms 105364 KB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv

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

#define Mp          make_pair
#define sep         ' '
#define endl        '\n'
#define F	        first
#define S	        second
#define pb          push_back
#define all(x)      (x).begin(),(x).end()
#define kill(res)	cout << res << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define fast_io     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io     freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll N = 5e5 + 50;
const ll inf = 1e9 + 7;

ll n, dp[N][3][2], help[3][2], sz[N];
string s;

vector<int> adj[N];

void dfs(int v, int p = 0){
    int x = s[v] - '0';
    if(!x) sz[v]++;

    for(int u: adj[v]){
        if(u == p) continue;
        dfs(u, v); sz[v] += sz[u];
    }
    if(!sz[v]) return;

    dp[v][0][1-x] = dp[v][1][1-x] = dp[v][2][1-x] = 1;

    for(int u: adj[v]){
        if(u == p || !sz[u]) continue;

        for(int i = 0; i < 3; i++) for(int j = 0; j < 2; j++) help[i][j] = inf;

        for(int i = 0; i < 2; i++){
            help[0][i] = min({dp[v][0][1-i] + dp[u][0][1] + 1, dp[v][0][i] + dp[u][0][0] + 3});
            help[1][i] = min({dp[v][1][1-i] + dp[u][0][1] + 1, dp[v][1][i] + dp[u][0][0] + 3, dp[v][0][i] + dp[u][1][1], dp[v][0][1-i] + dp[u][1][0] + 2});

            //help[2][i] = min({help[2][i], dp[v][1][i] + dp[u][1][1], dp[v][1][1-i] + dp[u][1][0] + 2});
            //help[2][i] = min({help[2][i], dp[v][2][i] + dp[u][0][1], dp[v][2][1-i] + dp[u][0][0] + 2});
            //help[2][i] = min({help[2][i], dp[v][0][i] + dp[u][2][1], dp[v][0][1-i] + dp[u][2][0] + 2});

            help[2][i] = min({help[2][i], dp[v][1][i] + dp[u][1][1], dp[v][1][1-i] + dp[u][1][0] + 2});
            help[2][i] = min({help[2][i], dp[v][2][1-i] + dp[u][0][1] + 1, dp[v][2][i] + dp[u][0][0] + 3});
            help[2][i] = min({help[2][i], dp[v][0][1-i] + dp[u][2][1] + 3, dp[v][0][i] + dp[u][2][0] + 1});
        }

        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 2; j++){
                dp[v][i][j] = help[i][j];
                if(i) dp[v][i][j] = min(dp[v][i][j], dp[v][i-1][j]);
            }
        }
    }
}

int main(){
    fast_io;

    cin >> n >> s;
    s = "$" + s;

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

    v = 1;
    for(int i = n; i >= 1; i--) if(s[i] == '0') v = i;

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

    dfs(v);

    cout << dp[v][2][1];

}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 16988 KB Output is correct
2 Correct 4 ms 16988 KB Output is correct
3 Correct 5 ms 17000 KB Output is correct
4 Correct 4 ms 17396 KB Output is correct
5 Correct 4 ms 16988 KB Output is correct
6 Correct 4 ms 16988 KB Output is correct
7 Correct 4 ms 16988 KB Output is correct
8 Correct 4 ms 16988 KB Output is correct
9 Correct 4 ms 16988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 226 ms 73028 KB Output is correct
2 Correct 366 ms 100904 KB Output is correct
3 Correct 306 ms 105364 KB Output is correct
4 Correct 298 ms 80628 KB Output is correct
5 Correct 327 ms 90776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 247 ms 55648 KB Output is correct
2 Correct 280 ms 61996 KB Output is correct
3 Correct 324 ms 62468 KB Output is correct
4 Correct 208 ms 58816 KB Output is correct
5 Correct 181 ms 62404 KB Output is correct
6 Correct 155 ms 57944 KB Output is correct
7 Correct 232 ms 63012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 16988 KB Output is correct
2 Correct 4 ms 16988 KB Output is correct
3 Correct 5 ms 17000 KB Output is correct
4 Correct 4 ms 17396 KB Output is correct
5 Correct 4 ms 16988 KB Output is correct
6 Correct 4 ms 16988 KB Output is correct
7 Correct 4 ms 16988 KB Output is correct
8 Correct 4 ms 16988 KB Output is correct
9 Correct 4 ms 16988 KB Output is correct
10 Correct 226 ms 73028 KB Output is correct
11 Correct 366 ms 100904 KB Output is correct
12 Correct 306 ms 105364 KB Output is correct
13 Correct 298 ms 80628 KB Output is correct
14 Correct 327 ms 90776 KB Output is correct
15 Correct 247 ms 55648 KB Output is correct
16 Correct 280 ms 61996 KB Output is correct
17 Correct 324 ms 62468 KB Output is correct
18 Correct 208 ms 58816 KB Output is correct
19 Correct 181 ms 62404 KB Output is correct
20 Correct 155 ms 57944 KB Output is correct
21 Correct 232 ms 63012 KB Output is correct
22 Correct 245 ms 59516 KB Output is correct
23 Correct 300 ms 60680 KB Output is correct
24 Correct 229 ms 59700 KB Output is correct
25 Correct 280 ms 56676 KB Output is correct
26 Correct 219 ms 63916 KB Output is correct
27 Correct 222 ms 63764 KB Output is correct
28 Correct 159 ms 58376 KB Output is correct
29 Correct 171 ms 62756 KB Output is correct
30 Correct 223 ms 60992 KB Output is correct