結果
問題 | No.1002 Twotone |
ユーザー | kyort0n |
提出日時 | 2020-02-28 22:58:22 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 3,376 ms / 5,000 ms |
コード長 | 4,139 bytes |
コンパイル時間 | 2,381 ms |
コンパイル使用メモリ | 198,544 KB |
実行使用メモリ | 143,232 KB |
最終ジャッジ日時 | 2024-10-13 18:51:46 |
合計ジャッジ時間 | 20,412 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
8,192 KB |
testcase_01 | AC | 5 ms
8,064 KB |
testcase_02 | AC | 4 ms
8,192 KB |
testcase_03 | AC | 424 ms
17,408 KB |
testcase_04 | AC | 567 ms
20,224 KB |
testcase_05 | AC | 577 ms
20,352 KB |
testcase_06 | AC | 6 ms
8,064 KB |
testcase_07 | AC | 250 ms
15,488 KB |
testcase_08 | AC | 449 ms
20,224 KB |
testcase_09 | AC | 467 ms
20,096 KB |
testcase_10 | AC | 5 ms
8,192 KB |
testcase_11 | AC | 540 ms
19,328 KB |
testcase_12 | AC | 740 ms
22,528 KB |
testcase_13 | AC | 773 ms
22,528 KB |
testcase_14 | AC | 6 ms
8,192 KB |
testcase_15 | AC | 421 ms
15,744 KB |
testcase_16 | AC | 849 ms
21,248 KB |
testcase_17 | AC | 816 ms
21,120 KB |
testcase_18 | AC | 5 ms
8,192 KB |
testcase_19 | AC | 566 ms
26,112 KB |
testcase_20 | AC | 699 ms
35,456 KB |
testcase_21 | AC | 675 ms
34,688 KB |
testcase_22 | AC | 5 ms
8,192 KB |
testcase_23 | AC | 687 ms
39,936 KB |
testcase_24 | AC | 1,306 ms
58,948 KB |
testcase_25 | AC | 1,366 ms
59,008 KB |
testcase_26 | AC | 3 ms
8,064 KB |
testcase_27 | AC | 120 ms
46,072 KB |
testcase_28 | AC | 210 ms
56,640 KB |
testcase_29 | AC | 191 ms
56,560 KB |
testcase_30 | AC | 5 ms
8,064 KB |
testcase_31 | AC | 184 ms
56,152 KB |
testcase_32 | AC | 205 ms
56,340 KB |
testcase_33 | AC | 183 ms
56,208 KB |
testcase_34 | AC | 3,376 ms
143,232 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template<class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } const long double EPS = 1e-10; const long long INF = 1e18; const long double PI = acos(-1.0L); //const ll mod = 1000000007; ll N, K; vector<l_l> paths[200100]; vector<bool> Del; ll SIZE[200100]; void print(set<ll> st) { cerr << "{"; for(auto val : st) { cerr << val << " "; } cerr << "}"; } void dfs(int from, int now) { SIZE[now] = 1; for(auto tmp : paths[now]) { int to = tmp.first; if(to == from) continue; if(Del[to]) continue; dfs(now, to); SIZE[now] += SIZE[to]; } } int centroid(int now, int sz) { for(auto tmp : paths[now]) { int to = tmp.first; if(Del[to]) continue; if(SIZE[to] > SIZE[now]) continue; if(SIZE[to] * 2 >= sz) return centroid(to, sz); } return now; } ll ans = 0; void g(int now, int from, map<set<ll>, ll> &mp, set<ll> st) { /* cerr << from << " -> " << now << " "; print(st); cerr << endl; */ mp[st]++; for(auto tmp : paths[now]) { int to = tmp.first; if(Del[to]) continue; if(to == from) continue; set<ll> st2 = st; st2.insert(tmp.second); if(st2.size() >= 3) continue; g(to, now, mp, st2); } } void f(int now) { dfs(-1, now); if(SIZE[now] == 1) return; int c = centroid(now, SIZE[now]); vector<map<set<ll>, ll>> V; map<set<ll>, ll> Total; for(auto tmp : paths[c]) { int to = tmp.first; if(Del[to]) continue; V.push_back({}); g(to, c, V.back(), {tmp.second}); for(auto tmp : V.back()) { Total[tmp.first] += tmp.second; } } ll OneNum = 0; ll OneAns = 0; map<set<ll>, ll> TwoSum; for(auto tmp : Total) { /* print(tmp.first); cerr << " " << tmp.second << endl; */ if(tmp.first.size() == 1) { OneNum += tmp.second; OneAns -= tmp.second * tmp.second; } else { //TwoSquare += tmp.second * tmp.second; TwoSum[tmp.first] += tmp.second * tmp.second; //ans += tmp.second * (tmp.second + 1); ans += tmp.second; for(auto tmp2 : tmp.first) { set<ll> st = {tmp2}; auto itr = Total.lower_bound(st); if((*itr).first == st) { ans += (*itr).second * tmp.second; } } } //cerr << ans << endl; } //cerr << "end: Total" << endl; for(auto now : V) { for(auto tmp : now) { if(tmp.first.size() == 1) { } else { //ans -= tmp.second * tmp.second; TwoSum[tmp.first] -= tmp.second * tmp.second; for(auto tmp2 : tmp.first) { set<ll> st = {tmp2}; auto itr = now.lower_bound(st); if((*itr).first == st) { ans -= (*itr).second * tmp.second; } } } } //cerr << ans << endl; } OneAns += OneNum * OneNum; ans += OneAns / 2; for(auto tmp : TwoSum) { ans += tmp.second / 2; } Del[c] = true; //cerr << "-------------" << c << " " << ans << "--------" << endl; for(auto tmp : paths[c]) { int to = tmp.first; if(Del[to]) continue; f(to); } } int main() { //cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); cin >> N >> K; Del.resize(N); for(int i = 0; i < N - 1; i++) { ll u, v, c; cin >> u >> v >> c; u--; v--; paths[u].push_back({v, c}); paths[v].push_back({u, c}); } f(0); cout << ans << endl; return 0; }