結果

問題 No.1002 Twotone
ユーザー risujirohrisujiroh
提出日時 2020-02-28 23:15:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,973 bytes
コンパイル時間 2,374 ms
コンパイル使用メモリ 189,668 KB
実行使用メモリ 67,616 KB
最終ジャッジ日時 2024-04-21 20:42:04
合計ジャッジ時間 37,967 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 652 ms
26,728 KB
testcase_08 AC 1,217 ms
42,204 KB
testcase_09 AC 1,187 ms
42,076 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1,382 ms
41,248 KB
testcase_20 AC 1,658 ms
51,380 KB
testcase_21 AC 1,673 ms
51,036 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 1,575 ms
33,108 KB
testcase_24 AC 3,043 ms
51,028 KB
testcase_25 AC 3,044 ms
51,008 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 280 ms
29,952 KB
testcase_28 AC 645 ms
42,932 KB
testcase_29 AC 473 ms
42,928 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 456 ms
42,788 KB
testcase_32 AC 692 ms
43,188 KB
testcase_33 AC 505 ms
42,932 KB
testcase_34 AC 1,873 ms
67,616 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, int c0, int c1 = -1) -> void {
    assert(c0 != c1);
    if (c1 == -1) {
      ++mp1[c0];
    } else {
      ++mp2[{c0, c1}];
    }
    for (int u : g[v]) {
      if (be[u] and u != p) {
        int c = col[{v, u}];
        if (c1 == -1) {
          if (c != c0) {
            c1 = c;
          }
        } else {
          if (c != c0 and c != c1) {
            continue;
          }
        }
        if (c1 != -1 and c0 > c1) {
          swap(c0, c1);
        }
        self(self, u, v, c0, c1);
      }
    }
  };
  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[e.first.first] + mp1[e.first.second]) * 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[e.first.first] + smp1[e.first.second]) * 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