結果
| 問題 |
No.1002 Twotone
|
| コンテスト | |
| ユーザー |
kyort0n
|
| 提出日時 | 2020-02-28 23:55:39 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3,400 ms / 5,000 ms |
| コード長 | 4,139 bytes |
| コンパイル時間 | 2,357 ms |
| コンパイル使用メモリ | 198,608 KB |
| 実行使用メモリ | 143,232 KB |
| 最終ジャッジ日時 | 2024-10-13 19:28:21 |
| 合計ジャッジ時間 | 21,632 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 33 |
ソースコード
#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;
}
kyort0n