結果

問題 No.1002 Twotone
ユーザー pekempeypekempey
提出日時 2020-02-28 22:24:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,015 bytes
コンパイル時間 2,422 ms
コンパイル使用メモリ 190,524 KB
実行使用メモリ 29,968 KB
最終ジャッジ日時 2024-04-21 19:11:12
合計ジャッジ時間 16,465 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 408 ms
13,324 KB
testcase_04 AC 560 ms
16,404 KB
testcase_05 AC 536 ms
16,252 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 274 ms
11,136 KB
testcase_08 AC 520 ms
16,368 KB
testcase_09 AC 513 ms
16,384 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 TLE -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

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] * 2 >= sz[u]) 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