結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー Ricky_ponRicky_pon
提出日時 2023-02-03 22:01:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 3,821 bytes
コンパイル時間 1,331 ms
コンパイル使用メモリ 134,372 KB
実行使用メモリ 24,640 KB
最終ジャッジ日時 2023-09-15 17:50:31
合計ジャッジ時間 5,443 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,148 KB
testcase_01 AC 4 ms
8,288 KB
testcase_02 AC 4 ms
8,228 KB
testcase_03 AC 4 ms
8,176 KB
testcase_04 AC 4 ms
8,232 KB
testcase_05 AC 4 ms
8,168 KB
testcase_06 AC 4 ms
8,296 KB
testcase_07 AC 4 ms
8,224 KB
testcase_08 AC 4 ms
8,244 KB
testcase_09 AC 4 ms
8,452 KB
testcase_10 AC 4 ms
8,272 KB
testcase_11 AC 74 ms
15,760 KB
testcase_12 AC 75 ms
15,636 KB
testcase_13 AC 73 ms
15,616 KB
testcase_14 AC 68 ms
15,736 KB
testcase_15 AC 87 ms
24,640 KB
testcase_16 AC 56 ms
12,708 KB
testcase_17 AC 55 ms
12,852 KB
testcase_18 AC 4 ms
8,444 KB
testcase_19 AC 82 ms
15,792 KB
testcase_20 AC 84 ms
15,132 KB
testcase_21 AC 83 ms
15,332 KB
testcase_22 AC 73 ms
16,168 KB
testcase_23 AC 82 ms
15,316 KB
testcase_24 AC 81 ms
15,580 KB
testcase_25 AC 85 ms
15,196 KB
testcase_26 AC 100 ms
15,208 KB
testcase_27 AC 82 ms
15,880 KB
testcase_28 AC 72 ms
16,336 KB
testcase_29 AC 74 ms
16,328 KB
testcase_30 AC 99 ms
15,252 KB
testcase_31 AC 81 ms
15,952 KB
testcase_32 AC 82 ms
15,516 KB
testcase_33 AC 82 ms
15,712 KB
testcase_34 AC 81 ms
15,196 KB
testcase_35 AC 94 ms
15,220 KB
testcase_36 AC 94 ms
15,556 KB
testcase_37 AC 82 ms
15,264 KB
testcase_38 AC 98 ms
15,260 KB
testcase_39 AC 81 ms
15,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #include <bits/stdc++.h>
#include <string.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <ciso646>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define For(i, a, b) for (int i = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For(i, 0, n)
#define rrep(i, n) rFor(i, n, 0)
#define fi first
#define se second


#include <string>
#include <vector>

namespace rklib {

struct RollingHash {
   private:
    using ulint = unsigned long long;
    const ulint hash_mod = (1ULL << 61ULL) - 1, hash_base = 1'000'000'007;
    const ulint mask_30bit = (1ULL << 30ULL) - 1,
                mask_31bit = (mask_30bit << 1ULL) + 1;
    const ulint num = hash_mod << 2LL;
    std::vector<ulint> pow_table, hash;
    int n;

    ulint calc_mul31(ulint a, ulint b) {
        ulint au = a >> 31ULL, ad = a & mask_31bit;
        ulint mid = au * b, midu = mid >> 30ULL, midd = mid & mask_30bit;
        return midu + (midd << 31ULL) + ad * b;
    }

    ulint calc_mul61(ulint a, ulint b) {
        ulint au = a >> 31ULL, ad = a & mask_31bit;
        ulint bu = b >> 31ULL, bd = b & mask_31bit;
        ulint mid = au * bd + ad * bu, midu = mid >> 30ULL,
              midd = mid & mask_30bit;
        return 2 * au * bu + midu + (midd << 31ULL) + ad * bd;
    }

    ulint calc_mod(ulint x) {
        x = (x & hash_mod) + (x >> 61ULL);
        if (x > hash_mod) x -= hash_mod;
        return x;
    }

   public:
    RollingHash(std::string &s) {
        n = s.size();
        pow_table.resize(n + 1);
        hash.resize(n + 1);
        pow_table[0] = 1ULL;
        for (int i = 0; i < n; ++i)
            pow_table[i + 1] = calc_mod(calc_mul61(pow_table[i], hash_base));
        hash[0] = 0ULL;
        for (int i = 0; i < n; ++i)
            hash[i + 1] = calc_mod(calc_mul61(hash[i], hash_base) + s[i]);
    }

    ulint slice(int l, int r) {
        return calc_mod(hash[r] + num - calc_mul61(hash[l], pow_table[r - l]));
    }
};

}  // namespace rklib


#include <algorithm>
#include <cassert>
#include <vector>

namespace rklib {

template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
bool chmin_non_negative(T &a, const T &b) {
    if (a < 0 || a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}

template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

}  // namespace rklib


using namespace std;
using namespace rklib;

using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;

constexpr int MAX = 200010;
vector<int> gr[MAX];
int col[MAX], ans = 0;

int dfs(int v, int pv) {
    for (auto nv : gr[v]) {
        if (nv == pv) continue;
        if (dfs(nv, v) == 0) {
            col[v] ^= 1;
            ++ans;
        }
    }
    return col[v];
}

int main() {
    int n;
    scanf("%d", &n);
    rep(i, n - 1) {
        int a, b;
        scanf("%d%d", &a, &b);
        --a;
        --b;
        gr[a].push_back(b);
        gr[b].push_back(a);
    }
    rep(i, n) scanf("%d", &col[i]);

    printf("%d\n", dfs(0, -1) == 1 ? ans : -1);
}
0