結果

問題 No.1002 Twotone
ユーザー risujirohrisujiroh
提出日時 2020-03-02 10:07:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,648 ms / 5,000 ms
コード長 3,834 bytes
コンパイル時間 2,601 ms
コンパイル使用メモリ 188,928 KB
実行使用メモリ 45,312 KB
最終ジャッジ日時 2024-04-21 22:31:17
合計ジャッジ時間 22,426 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 449 ms
21,504 KB
testcase_04 AC 597 ms
27,008 KB
testcase_05 AC 605 ms
27,136 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 387 ms
19,380 KB
testcase_08 AC 717 ms
29,212 KB
testcase_09 AC 699 ms
29,204 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 726 ms
22,744 KB
testcase_12 AC 1,009 ms
27,992 KB
testcase_13 AC 1,024 ms
28,012 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 558 ms
18,400 KB
testcase_16 AC 1,157 ms
30,308 KB
testcase_17 AC 1,150 ms
29,676 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 775 ms
32,768 KB
testcase_20 AC 924 ms
45,312 KB
testcase_21 AC 933 ms
44,544 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 924 ms
28,100 KB
testcase_24 AC 1,648 ms
45,012 KB
testcase_25 AC 1,617 ms
43,016 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 134 ms
21,156 KB
testcase_28 AC 279 ms
29,708 KB
testcase_29 AC 222 ms
29,708 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 213 ms
29,672 KB
testcase_32 AC 312 ms
29,832 KB
testcase_33 AC 236 ms
29,712 KB
testcase_34 AC 623 ms
41,732 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