結果

問題 No.1002 Twotone
ユーザー risujirohrisujiroh
提出日時 2020-03-02 10:07:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,014 ms / 5,000 ms
コード長 3,834 bytes
コンパイル時間 2,072 ms
コンパイル使用メモリ 194,844 KB
実行使用メモリ 45,308 KB
最終ジャッジ日時 2024-10-13 20:56:54
合計ジャッジ時間 14,971 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 2 ms
6,824 KB
testcase_03 AC 293 ms
21,556 KB
testcase_04 AC 422 ms
27,116 KB
testcase_05 AC 422 ms
27,120 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 240 ms
19,392 KB
testcase_08 AC 445 ms
29,208 KB
testcase_09 AC 443 ms
29,208 KB
testcase_10 AC 2 ms
6,820 KB
testcase_11 AC 437 ms
22,608 KB
testcase_12 AC 629 ms
27,872 KB
testcase_13 AC 608 ms
28,012 KB
testcase_14 AC 2 ms
6,816 KB
testcase_15 AC 353 ms
18,404 KB
testcase_16 AC 765 ms
30,440 KB
testcase_17 AC 688 ms
29,676 KB
testcase_18 AC 2 ms
6,816 KB
testcase_19 AC 464 ms
32,820 KB
testcase_20 AC 573 ms
45,308 KB
testcase_21 AC 613 ms
44,420 KB
testcase_22 AC 2 ms
6,816 KB
testcase_23 AC 515 ms
28,100 KB
testcase_24 AC 1,001 ms
45,016 KB
testcase_25 AC 1,014 ms
42,888 KB
testcase_26 AC 2 ms
6,816 KB
testcase_27 AC 94 ms
21,288 KB
testcase_28 AC 174 ms
29,736 KB
testcase_29 AC 151 ms
29,708 KB
testcase_30 AC 2 ms
6,816 KB
testcase_31 AC 146 ms
29,540 KB
testcase_32 AC 187 ms
29,708 KB
testcase_33 AC 154 ms
29,836 KB
testcase_34 AC 503 ms
41,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <class It> auto rle(It first, It last) {
  vector<pair<decltype(*first), int>> res;
  for (; first != last; ++first) {
    if (res.empty() or res.back().first != *first) {
      res.emplace_back(*first, 1);
    } else {
      ++res.back().second;
    }
  }
  return res;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<vector<int>> g(n), col(n);
  for (int _ = n - 1; _--; ) {
    int u, v, c;
    cin >> u >> v >> c;
    --u, --v;
    g[u].push_back(v), col[u].push_back(c);
    g[v].push_back(u), col[v].push_back(c);
  }
  vector<bool> be(n, true);
  vector<int> sz(n), par(n);
  auto centroids = [&](int r, int cur_n) {
    vector<int> res;
    auto dfs = [&](auto&& self, int v, int p) -> void {
      sz[v] = 1, par[v] = p;
      bool ok = true;
      for (int u : g[v]) {
        if (be[u] and u != p) {
          self(self, u, v);
          sz[v] += sz[u];
          ok &= 2 * sz[u] <= cur_n;
        }
      }
      if (ok and 2 * sz[v] >= cur_n) {
        res.push_back(v);
      }
    };
    dfs(dfs, r, -1);
    return res;
  };
  vector<int> c1;
  vector<pair<int, int>> c2;
  auto dfs = [&](auto&& self, int v, int p, const vector<int>& cs) -> void {
    if (cs.size() == 1) {
      c1.push_back(cs[0]);
    } else {
      c2.emplace_back(cs[0], cs[1]);
    }
    for (int id = 0; id < (int)g[v].size(); ++id) {
      int u = g[v][id], c = col[v][id];
      if (be[u] and u != p) {
        if (cs.size() == 1) {
          if (c < cs[0]) {
            self(self, u, v, {c, cs[0]});
          } else if (cs[0] < c) {
            self(self, u, v, {cs[0], c});
          } else {
            self(self, u, v, cs);
          }
        } else if (c == cs[0] or c == cs[1]) {
          self(self, u, v, cs);
        }
      }
    }
  };
  auto f11 = [&](int s, int t) {
    auto res = (long long)(t - s) * (t - s);
    for (auto e : rle(begin(c1) + s, begin(c1) + t)) {
      res -= (long long)e.second * e.second;
    }
    return res /= 2;
  };
  auto f12 = [&](int s1, int t1, int s2, int t2) {
    vector<int> cs(2 * (t2 - s2));
    for (int i = 0; i < (int)cs.size(); i += 2) {
      tie(cs[i], cs[i + 1]) = c2[s2 + i / 2];
    }
    sort(begin(cs), end(cs));
    auto v1 = rle(begin(c1) + s1, begin(c1) + t1);
    auto v2 = rle(begin(cs), end(cs));
    long long res = 0;
    for (int i1 = 0, i2 = 0; i1 < (int)v1.size() and i2 < (int)v2.size(); ) {
      if (v1[i1].first == v2[i2].first) {
        res += (long long)v1[i1].second * v2[i2].second;
        ++i1, ++i2;
      } else {
        if (v1[i1].first < v2[i2].first) {
          ++i1;
        } else {
          ++i2;
        }
      }
    }
    return res;
  };
  auto f22 = [&](int s, int t) {
    long long res = 0;
    for (auto e : rle(begin(c2) + s, begin(c2) + t)) {
      res += (long long)e.second * (e.second - 1) / 2;
    }
    return res;
  };
  long long res = 0;
  auto rec = [&](auto&& self, int c, int cur_n) -> void {
    c = centroids(c, cur_n)[0];
    be[c] = false;
    for (int v : g[c]) {
      if (be[v]) {
        self(self, v, par[v] == c ? sz[v] : cur_n - sz[c]);
      }
    }
    be[c] = true;
    c1.clear(), c2.clear();
    for (int id = 0; id < (int)g[c].size(); ++id) {
      int v = g[c][id];
      if (be[v]) {
        int n1 = c1.size(), n2 = c2.size();
        dfs(dfs, v, c, {col[c][id]});
        sort(begin(c1) + n1, end(c1));
        sort(begin(c2) + n2, end(c2));
        res -= f12(n1, c1.size(), n2, c2.size());
        res -= f22(n2, c2.size());
      }
    }
    sort(begin(c1), end(c1));
    sort(begin(c2), end(c2));
    int n1 = c1.size(), n2 = c2.size();
    res += c2.size();
    res += f11(0, n1);
    res += f12(0, n1, 0, n2);
    res += f22(0, n2);
  };
  rec(rec, 0, n);
  cout << res << '\n';
}
0