結果

問題 No.1002 Twotone
ユーザー risujirohrisujiroh
提出日時 2020-02-28 22:20:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3,707 ms / 5,000 ms
コード長 3,007 bytes
コンパイル時間 2,304 ms
コンパイル使用メモリ 197,948 KB
実行使用メモリ 79,676 KB
最終ジャッジ日時 2023-08-03 20:44:11
合計ジャッジ時間 39,921 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 633 ms
33,056 KB
testcase_04 AC 889 ms
41,764 KB
testcase_05 AC 864 ms
41,884 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 656 ms
26,900 KB
testcase_08 AC 1,218 ms
41,788 KB
testcase_09 AC 1,224 ms
41,876 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1,504 ms
33,016 KB
testcase_12 AC 2,127 ms
41,716 KB
testcase_13 AC 2,155 ms
41,688 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1,117 ms
25,300 KB
testcase_16 AC 2,289 ms
41,808 KB
testcase_17 AC 2,327 ms
41,548 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1,399 ms
41,136 KB
testcase_20 AC 1,654 ms
51,348 KB
testcase_21 AC 1,651 ms
50,928 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 1,953 ms
50,784 KB
testcase_24 AC 3,620 ms
79,676 KB
testcase_25 AC 3,707 ms
79,656 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 257 ms
29,876 KB
testcase_28 AC 553 ms
42,916 KB
testcase_29 AC 422 ms
42,804 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 412 ms
42,860 KB
testcase_32 AC 602 ms
43,044 KB
testcase_33 AC 448 ms
42,880 KB
testcase_34 AC 1,658 ms
63,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<vector<int>> g(n);
  map<pair<int, int>, int> col;
  for (int _ = n - 1; _--; ) {
    int u, v, c;
    cin >> u >> v >> c;
    --u, --v, --c;
    g[u].push_back(v);
    g[v].push_back(u);
    col[{u, v}] = col[{v, u}] = c;
  }
  vector<bool> be(n, true);
  vector<int> sz(n), par(n);
  auto centroid = [&](int r) {
    vector<int> vs;
    auto dfs = [&](auto&& self, int v, int p) -> void {
      vs.push_back(v);
      par[v] = p;
      sz[v] = 1;
      for (int u : g[v]) {
        if (be[u] and u != p) {
          self(self, u, v);
          sz[v] += sz[u];
        }
      }
    };
    dfs(dfs, r, -1);
    for (int v : vs) {
      bool ok = vs.size() - sz[v] <= vs.size() / 2;
      for (int u : g[v]) {
        if (be[u] and u != par[v]) {
          if (sz[u] > (int)vs.size() / 2) {
            ok = false;
            break;
          }
        }
      }
      if (ok) {
        return v;
      }
    }
    return -1;
  };
  long long res = 0;
  map<int, int> mp1;
  map<pair<int, int>, int> mp2;
  auto dfs = [&](auto&& self, int v, int p, set<int> se) -> void {
    if (se.size() == 1) {
      ++mp1[*begin(se)];
    } else if (se.size() == 2) {
      ++mp2[{*begin(se), *next(begin(se))}];
    } else {
      assert(false);
    }
    for (int u : g[v]) {
      if (be[u] and u != p) {
        auto nse = se;
        nse.insert(col[{v, u}]);
        if (nse.size() >= 3) {
          continue;
        }
        self(self, u, v, nse);
      }
    }
  };
  auto rec = [&](auto&& self, int c) -> void {
    c = centroid(c);
    map<int, int> smp1;
    map<pair<int, int>, int> smp2;
    for (int v : g[c]) {
      if (be[v]) {
        mp1.clear(), mp2.clear();
        dfs(dfs, v, c, {col[{c, v}]});
        long long s1 = 0;
        for (auto e : mp1) {
          res += (long long)e.second * e.second;
          s1 += e.second;
        }
        res -= s1 * s1;
        for (auto e : mp2) {
          res += 2 * e.second;
          res -= 2 * (long long)((mp1.count(e.first.first) ? mp1[e.first.first] : 0) + (mp1.count(e.first.second) ? mp1[e.first.second] : 0)) * e.second;
          res -= (long long)e.second * e.second;
        }
        for (auto e : mp1) {
          smp1[e.first] += e.second;
        }
        for (auto e : mp2) {
          smp2[e.first] += e.second;
        }
      }
    }
    long long s1 = 0;
    for (auto e : smp1) {
      res -= (long long)e.second * e.second;
      s1 += e.second;
    }
    res += s1 * s1;
    for (auto e : smp2) {
      res += 2 * (long long)((smp1.count(e.first.first) ? smp1[e.first.first] : 0) + (smp1.count(e.first.second) ? smp1[e.first.second] : 0)) * e.second;
      res += (long long)e.second * e.second;
    }
    be[c] = false;
    for (int u : g[c]) {
      if (be[u]) {
        self(self, u);
      }
    }
    be[c] = true;
  };
  rec(rec, 0);
  res /= 2;
  cout << res << '\n';
}
0