結果

問題 No.1002 Twotone
ユーザー _____TAB__________TAB_____
提出日時 2020-02-28 23:15:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,360 bytes
コンパイル時間 1,190 ms
コンパイル使用メモリ 91,424 KB
実行使用メモリ 81,432 KB
最終ジャッジ日時 2023-08-03 21:55:12
合計ジャッジ時間 9,653 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,388 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 147 ms
11,928 KB
testcase_08 AC 257 ms
18,220 KB
testcase_09 AC 256 ms
17,932 KB
testcase_10 AC 2 ms
4,384 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 1 ms
4,384 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 147 ms
20,772 KB
testcase_28 AC 242 ms
24,624 KB
testcase_29 AC 236 ms
24,676 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 224 ms
24,936 KB
testcase_32 AC 240 ms
23,888 KB
testcase_33 AC 218 ms
23,536 KB
testcase_34 AC 246 ms
53,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <iostream>
#include <vector>
#include <map>
using namespace std;

long long ans = 0;

pair<long long,long long> dfs(const vector<vector<pair<long long,long long>>> &G, long long v, long long p, long long c){
  map<long long,long long> C;
  map<long long,long long> D;
  vector<pair<long long,long long>> Q;
  long long s = 1, d = 0, f = 0;
  for(auto e : G[v]){
    if(e.first == p){
      Q.emplace_back(-10,-10);
      continue;
    }
    auto q = dfs(G,e.first,v,e.second);
    Q.push_back(q);
    C[e.second] += q.first;
    D[e.second] += q.second;
    f += q.first;
    if(e.second == c) s += q.first, d += q.second;
    else d += q.first;
    ans += q.second;
    // diff += q.second;
  }
  long long diff = 0;
  for(size_t i = 0; i < G[v].size(); ++i){
    if(G[v][i].first == p) continue;
    // assert(Q[i] >= 0);
    // ans += Q[i].first*(f-C[G[v][i].second]);
    diff += Q[i].first*(f-C[G[v][i].second]);
    ans += Q[i].first*(D[G[v][i].second]-Q[i].second);
  }
  assert(diff%2 == 0);
  ans += diff/2;
  return {s,d};
}

int main(){
  long long N, K;
  cin >> N >> K;
  vector<vector<pair<long long,long long>>> G(N);
  for(int i = 1; i < N; ++i){
    long long u, v, c;
    cin >> u >> v >> c;
    --u,--v,--c;
    G[u].emplace_back(v,c);
    G[v].emplace_back(u,c);
  }
  dfs(G,0,-1,-100);
  cout << ans << endl;
}
0