結果
問題 | No.2892 Lime and Karin |
ユーザー | tottoripaper |
提出日時 | 2024-09-17 17:06:06 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,982 bytes |
コンパイル時間 | 2,724 ms |
コンパイル使用メモリ | 224,748 KB |
実行使用メモリ | 546,584 KB |
最終ジャッジ日時 | 2024-09-17 17:08:33 |
合計ジャッジ時間 | 145,572 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2,993 ms
7,424 KB |
testcase_15 | AC | 3,003 ms
6,940 KB |
testcase_16 | AC | 5,700 ms
9,216 KB |
testcase_17 | AC | 697 ms
6,940 KB |
testcase_18 | AC | 5,943 ms
9,344 KB |
testcase_19 | TLE | - |
testcase_20 | AC | 127 ms
6,940 KB |
testcase_21 | AC | 2,818 ms
8,064 KB |
testcase_22 | AC | 4,869 ms
8,960 KB |
testcase_23 | AC | 1,140 ms
6,944 KB |
testcase_24 | TLE | - |
testcase_25 | TLE | - |
testcase_26 | TLE | - |
testcase_27 | TLE | - |
testcase_28 | TLE | - |
testcase_29 | TLE | - |
testcase_30 | TLE | - |
testcase_31 | TLE | - |
testcase_32 | TLE | - |
testcase_33 | TLE | - |
testcase_34 | AC | 2,132 ms
11,664 KB |
testcase_35 | AC | 1,944 ms
11,660 KB |
testcase_36 | AC | 2,801 ms
11,536 KB |
testcase_37 | AC | 2,820 ms
11,668 KB |
testcase_38 | AC | 2,784 ms
11,660 KB |
testcase_39 | MLE | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
testcase_45 | -- | - |
testcase_46 | -- | - |
testcase_47 | -- | - |
testcase_48 | -- | - |
testcase_49 | -- | - |
testcase_50 | -- | - |
testcase_51 | -- | - |
testcase_52 | -- | - |
testcase_53 | -- | - |
testcase_54 | -- | - |
ソースコード
#include <bits/stdc++.h> using ll = std::int64_t; void point_update( int x, int i, std::vector<int> &diff, std::vector<std::map<int, int>> &diff_group, std::vector<int> &incr, int &count, const int B ){ int ig = i / B; if(diff[i] + incr[ig] == 1 && x == -1){ count -= 1; }else if(diff[i] + incr[ig] == 0 && x == 1){ count += 1; } diff_group[ig][diff[i]] -= 1; diff[i] += x; diff_group[ig][diff[i]] += 1; } void add( int x, int l, int r, std::vector<int> &diff, std::vector<std::map<int, int>> &diff_group, std::vector<int> &incr, int &count, const int B ){ while(l < r && l % B > 0){ point_update(x, l, diff, diff_group, incr, count, B); l += 1; } while(l < r && r % B > 0){ point_update(x, r - 1, diff, diff_group, incr, count, B); r -= 1; } l /= B; r /= B; while(l < r){ if(x == 1){ count += diff_group[l][-incr[l]]; }else{ count -= diff_group[l][1 - incr[l]]; } incr[l] += x; l += 1; } } int main(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int N; std::cin >> N; std::vector<std::vector<int>> G(N + 1); for(int i=0;i<N-1;i++){ int u, v; std::cin >> u >> v; G[u].emplace_back(v); G[v].emplace_back(u); } std::string S; std::cin >> S; std::vector<int> in(N + 1), out(N + 1), diff(N, 0); { int counter = 0; std::stack<std::tuple<int, int, int>> stack; stack.emplace(0, 1, -1); while(!stack.empty()){ auto [t, v, p] = stack.top(); stack.pop(); if(t == 0){ in[v] = counter; counter += 1; diff[in[v]] = S[v - 1] == '1' ? 1 : -1; if(p != -1){ diff[in[v]] += diff[in[p]]; } stack.emplace(1, v, p); for(auto &w : G[v]){ if(w != p){ stack.emplace(0, w, v); } } }else{ out[v] = counter; } } } const int B = std::sqrt(N); int count = 0; std::vector<std::map<int, int>> diff_group((N + B - 1) / B); std::vector<int> incr((N + B - 1) / B, 0); for(int i=0;i<N;i++){ diff_group[i / B][diff[i]] += 1; if(diff[i] >= 1){ count += 1; } } ll res = 0; { std::stack<std::tuple<int, int, int>> stack; stack.emplace(0, 1, -1); while(!stack.empty()){ auto [t, v, p] = stack.top(); stack.pop(); if(t == 0){ stack.emplace(1, v, p); if(p != -1){ int xv = S[v - 1] == '1' ? 1 : -1; int xp = S[p - 1] == '1' ? 1 : -1; add(xv, 0, in[v], diff, diff_group, incr, count, B); add(-xp, in[v], out[v], diff, diff_group, incr, count, B); add(xv, out[v], diff.size(), diff, diff_group, incr, count, B); } res += count; for(auto &w : G[v]){ if(w != p){ stack.emplace(0, w, v); } } }else{ if(p != -1){ int xv = S[v - 1] == '1' ? 1 : -1; int xp = S[p - 1] == '1' ? 1 : -1; add(-xv, 0, in[v], diff, diff_group, incr, count, B); add(xp, in[v], out[v], diff, diff_group, incr, count, B); add(-xv, out[v], diff.size(), diff, diff_group, incr, count, B); } } } } int n = std::count(S.begin(), S.end(), '1'); res -= n; res = res / 2 + n; std::cout << res << std::endl; }