結果

問題 No.1002 Twotone
ユーザー pekempeypekempey
提出日時 2020-02-28 22:32:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,010 bytes
コンパイル時間 2,099 ms
コンパイル使用メモリ 190,092 KB
実行使用メモリ 47,616 KB
最終ジャッジ日時 2024-04-21 19:38:35
合計ジャッジ時間 14,374 ms
ジャッジサーバーID
(参考情報)
judge3 / 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 317 ms
13,056 KB
testcase_04 AC 421 ms
16,256 KB
testcase_05 AC 435 ms
16,384 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 205 ms
11,264 KB
testcase_08 AC 369 ms
16,256 KB
testcase_09 AC 372 ms
16,128 KB
testcase_10 AC 2 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 462 ms
18,432 KB
testcase_20 AC 578 ms
24,960 KB
testcase_21 AC 574 ms
24,576 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 151 ms
12,524 KB
testcase_28 AC 245 ms
16,792 KB
testcase_29 AC 241 ms
16,796 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 230 ms
16,652 KB
testcase_32 AC 234 ms
16,540 KB
testcase_33 AC 218 ms
16,672 KB
testcase_34 AC 1,001 ms
47,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define range(a) a.begin(), a.end()

int main() {
  int N, K; cin >> N >> K;
  vector<vector<pair<int, int>>> G(N);
  rep(i, N - 1) {
    int u, v, c; cin >> u >> v >> c; u--; v--;
    G[u].emplace_back(v, c);
    G[v].emplace_back(u, c);
  }
  vector<bool> used(N);
  vector<int> sz(N);
  auto dfs2 = [&](auto dfs2, int u, int p) -> void {
    sz[u] = 1;
    for (auto e : G[u]) if (e.first != p && not used[e.first]) {
      dfs2(dfs2, e.first, u);
      sz[u] += sz[e.first];
    }
  };
  auto dfs3 = [&](auto dfs3, int u, int p, int n) -> int { 
    for (auto e : G[u]) if (e.first != p && not used[e.first]) {
      int v = e.first;
      if (sz[v] > n / 2) return dfs3(dfs3, v, u, n);
    }
    return u;
  };
  auto dfs4 = [&](auto dfs4, int u, int p, int now1, int now2, map<pair<int, int>, ll> &res) -> void {
    res[minmax(now1, now2)]++;
    for (auto e : G[u]) if (e.first != p && not used[e.first]) {
      int v = e.first;
      int next1 = now1;
      int next2 = now2;
      if (next1 == -1) {
        next1 = e.second;
      } else if (next1 == e.second) {

      } else if (next2 == -1) {
        next2 = e.second;
      } else if (next2 == e.second) {

      } else {
        continue;
      }
      dfs4(dfs4, v, u, next1, next2, res);
    }
  };
  ll ans = 0;
  auto dfs = [&](auto dfs, int u) -> void {
    dfs2(dfs2, u, -1);
    u = dfs3(dfs3, u, -1, sz[u]);
    map<pair<int, int>, ll> mp;
    map<int, ll> two;
    map<int, ll> one;
    ll single = 0;
    used[u] = true;
    for (auto e : G[u]) if (not used[e.first]) {
      int v = e.first;
      map<pair<int, int>, ll> np;
      dfs4(dfs4, v, u, e.second, -1, np);
      for (auto kv : np) {
        if (kv.first.first != -1) { // nisyoku
          ans += kv.second;
          if (mp.count(kv.first)) {
            ans += mp[kv.first];
          }
          if (one.count(kv.first.first)) {
            ans += kv.second * one[kv.first.first];
          }
          if (one.count(kv.first.second)) {
            ans += kv.second * one[kv.first.second];
          }
        } else if (kv.first.second != -1) { // issyoku
          ans += single * kv.second;
          if (mp.count(kv.first)) {
            ans -= mp[kv.first] * kv.second;
          }
          if (two.count(kv.first.second)) {
            ans += kv.second * two[kv.first.second];
          }
        }
      }
      for (auto kv : np) {
        mp[kv.first] += kv.second;
        if (kv.first.first != -1) {
          two[kv.first.first] += kv.second;
          two[kv.first.second] += kv.second;
        }
        if (kv.first.first == -1 && kv.first.second != -1) {
          single += kv.second;
          one[kv.first.second] += kv.second;
        }
      }
    }
    for (auto e : G[u]) if (not used[e.first]) {
      dfs(dfs, e.first);
    }
  };
  dfs(dfs, 0);
  cout << ans << endl;
}
0