답안 #1100978

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1100978 2024-10-15T07:08:19 Z lmToT27 Svjetlo (COCI20_svjetlo) C++17
110 / 110
235 ms 97924 KB
#include <bits/stdc++.h>
#define all(dataStructure) dataStructure.begin(),dataStructure.end()
#define ll long long

using namespace std;
namespace std {
        template <typename T, int D>
        struct _vector : public vector <_vector <T, D - 1>> {
                static_assert(D >= 1, "Dimension must be positive!");
                template <typename... Args>
                _vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
        };
        // _vector <int, 3> a(n, m, k);: int a[n][m][k].
        // _vector <int, 3> a(n, m, k, x);: int a[n][m][k] initialized with x.
        template <typename T>
        struct _vector <T, 1> : public vector <T> {
                _vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
        };

        template <typename T>
        T _min(T a) {
                return a;
        }

        template <typename T, typename... Args>
        T _min(T a, Args... args) {
                return min(a, _min(args...));
        }
}

const int MAX = 5e5 + 3;
const ll MOD[] = {1000000007, 998244353};

int n, sz[MAX];
string s;
vector <int> adj[MAX];
int dp[MAX][2][3];
int nDp[2][3];

void dfs(int u, int pre) {
        sz[u] = s[u] == '0';
        for (int &v : adj[u]) {
                if (v != pre) {
                        dfs(v, u);
                        sz[u] += sz[v];
                }
        }

        if (!sz[u]) return;

        int x = s[u] - '0';
        dp[u][x ^ 1][0] = dp[u][x ^ 1][1] = dp[u][x ^ 1][2] = 1;

        for (int &v : adj[u]) {
                if (v == pre || sz[v] == 0) continue;

                memset(nDp, 0x0f, sizeof nDp);
                auto &dp = ::dp[u];
                auto &f = ::dp[v];
                for (int i = 0; i < 2; i++) {
                        nDp[i][0] = _min(dp[i ^ 1][0] + f[1][0] + 1, dp[i][0] + f[0][0] + 3);
                        nDp[i][1] = _min(dp[i ^ 1][1] + f[1][0] + 1, dp[i ^ 1][0] + f[0][1] + 2,
                                        dp[i][1] + f[0][0] + 3, dp[i][0] + f[1][1]);
                        nDp[i][2] = _min(dp[i][1] + f[1][1], dp[i ^ 1][1] + f[0][1] + 2,
                                        dp[i ^ 1][2] + f[1][0] + 1, dp[i][2] + f[0][0] + 3,     
                                        dp[i ^ 1][0] + f[1][2] + 3, dp[i][0] + f[0][2] + 1);
                        nDp[i][1] = _min(nDp[i][1], nDp[i][0]);
                        nDp[i][2] = _min(nDp[i][2], nDp[i][1]);
                }
                swap(dp, nDp);
        }
}

void Solve() {
        cin >> n;
        cin >> 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, 0x0f, sizeof dp);
        
        for (int i = 1; i <= n; i++) {
                if (s[i] == '0') {
                        dfs(i, 0);
                        cout << dp[i][1][2];
                        return;
                }
        }

        cout << 0;
}

int32_t main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);

        #define TASK "TASK"
        if (fopen(TASK".INP", "r")) {
                freopen(TASK".INP", "r", stdin);
                freopen(TASK".OUT", "w", stdout);
        }
        
        /* int TEST = 1; cin >> TEST; while (TEST--) */ Solve();

        cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
        return 0;
}

Compilation message

svjetlo.cpp: In function 'int32_t main()':
svjetlo.cpp:104:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |                 freopen(TASK".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
svjetlo.cpp:105:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |                 freopen(TASK".OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 25308 KB Output is correct
2 Correct 4 ms 25172 KB Output is correct
3 Correct 4 ms 25076 KB Output is correct
4 Correct 4 ms 25336 KB Output is correct
5 Correct 5 ms 25172 KB Output is correct
6 Correct 6 ms 25172 KB Output is correct
7 Correct 5 ms 25172 KB Output is correct
8 Correct 4 ms 25068 KB Output is correct
9 Correct 4 ms 25172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 155 ms 68464 KB Output is correct
2 Correct 213 ms 92476 KB Output is correct
3 Correct 235 ms 97924 KB Output is correct
4 Correct 216 ms 72012 KB Output is correct
5 Correct 213 ms 81184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 135 ms 43344 KB Output is correct
2 Correct 217 ms 48344 KB Output is correct
3 Correct 175 ms 48892 KB Output is correct
4 Correct 113 ms 44872 KB Output is correct
5 Correct 124 ms 48576 KB Output is correct
6 Correct 106 ms 46160 KB Output is correct
7 Correct 119 ms 49424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 25308 KB Output is correct
2 Correct 4 ms 25172 KB Output is correct
3 Correct 4 ms 25076 KB Output is correct
4 Correct 4 ms 25336 KB Output is correct
5 Correct 5 ms 25172 KB Output is correct
6 Correct 6 ms 25172 KB Output is correct
7 Correct 5 ms 25172 KB Output is correct
8 Correct 4 ms 25068 KB Output is correct
9 Correct 4 ms 25172 KB Output is correct
10 Correct 155 ms 68464 KB Output is correct
11 Correct 213 ms 92476 KB Output is correct
12 Correct 235 ms 97924 KB Output is correct
13 Correct 216 ms 72012 KB Output is correct
14 Correct 213 ms 81184 KB Output is correct
15 Correct 135 ms 43344 KB Output is correct
16 Correct 217 ms 48344 KB Output is correct
17 Correct 175 ms 48892 KB Output is correct
18 Correct 113 ms 44872 KB Output is correct
19 Correct 124 ms 48576 KB Output is correct
20 Correct 106 ms 46160 KB Output is correct
21 Correct 119 ms 49424 KB Output is correct
22 Correct 154 ms 45464 KB Output is correct
23 Correct 157 ms 46968 KB Output is correct
24 Correct 149 ms 46144 KB Output is correct
25 Correct 147 ms 44512 KB Output is correct
26 Correct 143 ms 50192 KB Output is correct
27 Correct 128 ms 49952 KB Output is correct
28 Correct 100 ms 46436 KB Output is correct
29 Correct 140 ms 48928 KB Output is correct
30 Correct 115 ms 47616 KB Output is correct