結果
問題 | No.2892 Lime and Karin |
ユーザー | hitonanode |
提出日時 | 2024-09-25 00:32:17 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 293 ms / 8,000 ms |
コード長 | 12,011 bytes |
コンパイル時間 | 2,390 ms |
コンパイル使用メモリ | 194,272 KB |
実行使用メモリ | 23,376 KB |
最終ジャッジ日時 | 2024-09-25 00:32:56 |
合計ジャッジ時間 | 11,593 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 2 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 2 ms
6,944 KB |
testcase_14 | AC | 100 ms
7,808 KB |
testcase_15 | AC | 123 ms
7,680 KB |
testcase_16 | AC | 174 ms
10,388 KB |
testcase_17 | AC | 33 ms
6,940 KB |
testcase_18 | AC | 176 ms
10,392 KB |
testcase_19 | AC | 270 ms
12,608 KB |
testcase_20 | AC | 11 ms
6,940 KB |
testcase_21 | AC | 123 ms
8,448 KB |
testcase_22 | AC | 160 ms
10,180 KB |
testcase_23 | AC | 59 ms
6,940 KB |
testcase_24 | AC | 289 ms
12,656 KB |
testcase_25 | AC | 276 ms
12,776 KB |
testcase_26 | AC | 278 ms
12,772 KB |
testcase_27 | AC | 264 ms
12,660 KB |
testcase_28 | AC | 282 ms
12,644 KB |
testcase_29 | AC | 277 ms
12,812 KB |
testcase_30 | AC | 293 ms
12,780 KB |
testcase_31 | AC | 287 ms
12,764 KB |
testcase_32 | AC | 278 ms
12,768 KB |
testcase_33 | AC | 263 ms
12,776 KB |
testcase_34 | AC | 47 ms
12,832 KB |
testcase_35 | AC | 48 ms
12,960 KB |
testcase_36 | AC | 53 ms
12,836 KB |
testcase_37 | AC | 49 ms
12,832 KB |
testcase_38 | AC | 49 ms
12,832 KB |
testcase_39 | AC | 276 ms
18,064 KB |
testcase_40 | AC | 277 ms
22,852 KB |
testcase_41 | AC | 271 ms
18,896 KB |
testcase_42 | AC | 271 ms
23,376 KB |
testcase_43 | AC | 278 ms
20,388 KB |
testcase_44 | AC | 77 ms
6,940 KB |
testcase_45 | AC | 214 ms
11,620 KB |
testcase_46 | AC | 93 ms
7,424 KB |
testcase_47 | AC | 124 ms
8,832 KB |
testcase_48 | AC | 48 ms
6,944 KB |
testcase_49 | AC | 162 ms
9,728 KB |
testcase_50 | AC | 138 ms
12,896 KB |
testcase_51 | AC | 140 ms
12,936 KB |
testcase_52 | AC | 136 ms
12,832 KB |
testcase_53 | AC | 143 ms
12,964 KB |
testcase_54 | AC | 136 ms
12,924 KB |
ソースコード
#include <cassert> #include <utility> #include <vector> // Centroid Decomposition // Verification: Codeforces #190 Div.1 C https://codeforces.com/contest/321/submission/59093583 // find_current_centroids(int r): Enumerate centroid(s) of the subtree which `r` belongs to. struct CentroidDecomposition { int V; std::vector<std::vector<int>> to; private: std::vector<int> is_alive; std::vector<int> subtree_size; void decompose(int r, int conn_size, auto callback) { const int c = find_current_centroids(r, conn_size).first; is_alive.at(c) = 0; callback(c); for (int nxt : to.at(c)) { if (!is_alive.at(nxt)) continue; int next_size = subtree_size.at(nxt); if (subtree_size.at(nxt) > subtree_size.at(c)) next_size = subtree_size.at(r) - subtree_size.at(c); decompose(nxt, next_size, callback); } } public: CentroidDecomposition(int v = 0) : V(v), to(v), is_alive(v, 1), subtree_size(v) {} CentroidDecomposition(int v, const std::vector<std::pair<int, int>> &tree_edges) : CentroidDecomposition(v) { for (auto e : tree_edges) add_edge(e.first, e.second); } void add_edge(int v1, int v2) { assert(0 <= v1 and v1 < V and 0 <= v2 and v2 < V); assert(v1 != v2); to.at(v1).push_back(v2), to.at(v2).emplace_back(v1); } std::pair<int, int> find_current_centroids(int r, int conn_size) { assert(is_alive.at(r)); const int thres = conn_size / 2; int c1 = -1, c2 = -1; auto rec_search = [&](auto &&self, int now, int prv) -> void { bool is_centroid = true; subtree_size.at(now) = 1; for (int nxt : to.at(now)) { if (nxt == prv or !is_alive.at(nxt)) continue; self(self, nxt, now); subtree_size.at(now) += subtree_size.at(nxt); if (subtree_size.at(nxt) > thres) is_centroid = false; } if (conn_size - subtree_size.at(now) > thres) is_centroid = false; if (is_centroid) (c1 < 0 ? c1 : c2) = now; }; rec_search(rec_search, r, -1); return {c1, c2}; } void run(int r, auto callback) { int conn_size = 0; auto rec = [&](auto &&self, int now, int prv) -> void { ++conn_size; is_alive.at(now) = 1; for (int nxt : to.at(now)) { if (nxt == prv) continue; self(self, nxt, now); } }; rec(rec, r, -1); decompose(r, conn_size, callback); } std::vector<int> centroid_decomposition(int r) { std::vector<int> res; run(r, [&](int v) { res.push_back(v); }); return res; } }; #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <deque> #include <forward_list> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iostream> #include <limits> #include <list> #include <map> #include <memory> #include <numeric> #include <optional> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using lint = long long; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; } const std::vector<std::pair<int, int>> grid_dxs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); } template <class T1, class T2> T1 floor_div(T1 num, T2 den) { return (num > 0 ? num / den : -((-num + den - 1) / den)); } template <class T1, class T2> std::pair<T1, T2> operator+(const std::pair<T1, T2> &l, const std::pair<T1, T2> &r) { return std::make_pair(l.first + r.first, l.second + r.second); } template <class T1, class T2> std::pair<T1, T2> operator-(const std::pair<T1, T2> &l, const std::pair<T1, T2> &r) { return std::make_pair(l.first - r.first, l.second - r.second); } template <class T> std::vector<T> sort_unique(std::vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template <class T> int arglb(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template <class T> int argub(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } template <class IStream, class T> IStream &operator>>(IStream &is, std::vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec); template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr); template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec); template <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa); template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec); template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec); template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec); template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec); template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa); template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp); template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp); template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl); template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } template <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; } template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; } template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl #define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr) #else #define dbg(x) ((void)0) #define dbgif(cond, x) ((void)0) #endif #include <algorithm> #include <vector> // 0-indexed BIT (binary indexed tree / Fenwick tree) (i : [0, len)) template <class T> struct BIT { int n; std::vector<T> data; BIT(int len = 0) : n(len), data(len) {} void reset() { std::fill(data.begin(), data.end(), T(0)); } void add(int pos, T v) { // a[pos] += v pos++; while (pos > 0 and pos <= n) data[pos - 1] += v, pos += pos & -pos; } T sum(int k) const { // a[0] + ... + a[k - 1] T res = 0; while (k > 0) res += data[k - 1], k -= k & -k; return res; } T sum(int l, int r) const { return sum(r) - sum(l); } // a[l] + ... + a[r - 1] template <class OStream> friend OStream &operator<<(OStream &os, const BIT &bit) { T prv = 0; os << '['; for (int i = 1; i <= bit.n; i++) { T now = bit.sum(i); os << now - prv << ',', prv = now; } return os << ']'; } }; int main() { int N; cin >> N; CentroidDecomposition cd(N); for (int e = 0; e < N - 1; ++e) { int u, v; cin >> u >> v; --u, --v; cd.add_edge(u, v); } vector<int> V(N); { std::string S; cin >> S; for (int i = 0; i < N; ++i) { V.at(i) = (S.at(i) - '0') * 2 - 1; } } std::vector<int> is_alive(N, 1); lint ret = 0; std::vector<int> cnt(N * 2 + 1); BIT<int> bit(N * 2 + 1); for (int c : cd.centroid_decomposition(0)) { is_alive.at(c) = 0; int lo = 0, hi = 0; auto addbit = [&](int w) -> void { chmin(lo, w), chmax(hi, w); cnt.at(N + w)++; bit.add(N + w, 1); }; addbit(V.at(c)); if (V.at(c) > 0) ret++; for (int nxt : cd.to.at(c)) { if (!is_alive.at(nxt)) continue; std::vector<int> ws; auto dfs = [&](auto &&self, int now, int prv, int w) -> void { ws.push_back(w); // addbit(w); ret += bit.sum(-w + N + 1, N * 2 + 1); for (int nxt : cd.to.at(now)) { if (nxt == prv or !is_alive.at(nxt)) continue; self(self, nxt, now, w + V.at(nxt)); } }; dfs(dfs, nxt, c, V.at(nxt)); for (int w : ws) { w += V.at(c); addbit(w); } } for (int i = lo; i <= hi; ++i) { int j = i + N; bit.add(j, -cnt.at(j)); cnt.at(j) = 0; } } cout << ret << '\n'; }