結果
問題 | No.1002 Twotone |
ユーザー | tempura_pp |
提出日時 | 2020-02-23 02:33:01 |
言語 | C++17 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,148 ms / 5,000 ms |
コード長 | 2,299 bytes |
コンパイル時間 | 1,500 ms |
コンパイル使用メモリ | 137,468 KB |
実行使用メモリ | 48,192 KB |
最終ジャッジ日時 | 2024-04-18 08:30:33 |
合計ジャッジ時間 | 15,471 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
8,372 KB |
testcase_01 | AC | 4 ms
8,388 KB |
testcase_02 | AC | 3 ms
8,284 KB |
testcase_03 | AC | 335 ms
14,916 KB |
testcase_04 | AC | 468 ms
16,780 KB |
testcase_05 | AC | 433 ms
16,776 KB |
testcase_06 | AC | 3 ms
8,384 KB |
testcase_07 | AC | 210 ms
13,372 KB |
testcase_08 | AC | 374 ms
16,748 KB |
testcase_09 | AC | 383 ms
16,732 KB |
testcase_10 | AC | 3 ms
8,068 KB |
testcase_11 | AC | 411 ms
15,680 KB |
testcase_12 | AC | 562 ms
17,956 KB |
testcase_13 | AC | 602 ms
17,988 KB |
testcase_14 | AC | 4 ms
8,308 KB |
testcase_15 | AC | 252 ms
13,748 KB |
testcase_16 | AC | 542 ms
17,344 KB |
testcase_17 | AC | 536 ms
17,368 KB |
testcase_18 | AC | 4 ms
8,644 KB |
testcase_19 | AC | 481 ms
22,244 KB |
testcase_20 | AC | 563 ms
31,084 KB |
testcase_21 | AC | 597 ms
30,260 KB |
testcase_22 | AC | 5 ms
8,176 KB |
testcase_23 | AC | 423 ms
19,528 KB |
testcase_24 | AC | 808 ms
27,460 KB |
testcase_25 | AC | 788 ms
25,028 KB |
testcase_26 | AC | 4 ms
8,212 KB |
testcase_27 | AC | 132 ms
14,288 KB |
testcase_28 | AC | 219 ms
16,720 KB |
testcase_29 | AC | 219 ms
16,916 KB |
testcase_30 | AC | 3 ms
8,388 KB |
testcase_31 | AC | 209 ms
16,592 KB |
testcase_32 | AC | 207 ms
16,720 KB |
testcase_33 | AC | 200 ms
16,516 KB |
testcase_34 | AC | 2,148 ms
48,192 KB |
ソースコード
#include<iostream> #include<string> #include<algorithm> #include<vector> #include<iomanip> #include<math.h> #include<complex> #include<queue> #include<deque> #include<stack> #include<map> #include<set> #include<bitset> #include<functional> #include<assert.h> #include<numeric> using namespace std; #define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i ) #define rep(i,n) REP(i,0,n) using ll = long long; const int inf=1e9+7; const ll longinf=1LL<<60 ; const ll mod=1e9+7 ; vector<pair<int,int>> v[202020]; int sz[202020]; bool used[202020]; void szdfs(int x,int p){ sz[x]=1; for(auto [to,color] : v[x]){ if(to==p||used[to])continue; szdfs(to,x); sz[x]+=sz[to]; } } int centroid(int x,int p,int total){ for(auto [to,color] : v[x]){ if(to==p||used[to])continue; if(2*sz[to]>total)return centroid(to,x,total); } return x; } map<pair<int,int>, int> MP, mp; map<int,int> ONE, one, TWO, two; int ONES, ones; ll ans; void dfs(int x,int p, int fi, int se){ if(fi && se){ ans += MP[minmax(fi,se)]++-mp[minmax(fi,se)]++; ans += ONE[fi]-one[fi]; ans += ONE[se]-one[se]; ++TWO[fi];++TWO[se]; ++two[fi];++two[se]; } else if(fi){ ans += TWO[fi]-two[fi]; ans += ONES++-ONE[fi]++; ans -= ones++-one[fi]++; } for(auto [to,color] : v[x]){ if(to==p||used[to])continue; if(!se){ if(color==fi)dfs(to, x, fi, se); else dfs(to,x,fi,color); } else if(fi==color||se==color)dfs(to, x, fi, se); } } void solve(int x,int p){ szdfs(x,-1); if(sz[x]==1)return ; x =centroid(x,-1,sz[x]); used[x]=1; MP.clear();ONE.clear();TWO.clear(); ONES = 0; for(auto [to,color]:v[x]){ mp.clear();one.clear();two.clear(); ones = 0; if(to==p||used[to])continue; dfs(to, x, color,0); } for(auto e:MP)ans += e.second; for(auto [to,color]:v[x]){ if(to==p||used[to])continue; solve(to,x); } } int main(){ int n,k; cin>>n>>k; rep(i,n-1){ int x,y,z; cin>>x>>y>>z; --x;--y; v[x].emplace_back(y,z); v[y].emplace_back(x,z); } solve(0,-1); cout<<ans<<endl; return 0; }