結果
| 問題 |
No.1002 Twotone
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2020-03-01 05:02:39 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,529 ms / 5,000 ms |
| コード長 | 4,630 bytes |
| コンパイル時間 | 4,058 ms |
| コンパイル使用メモリ | 235,732 KB |
| 最終ジャッジ日時 | 2025-01-09 03:38:29 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 33 |
ソースコード
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} iosetup;
using CostType = int;
struct Edge {
int src, dst; CostType cost;
Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
inline bool operator<(const Edge &rhs) const {
return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
}
inline bool operator<=(const Edge &rhs) const { return !(rhs < *this); }
inline bool operator>(const Edge &rhs) const { return rhs < *this; }
inline bool operator>=(const Edge &rhs) const { return !(*this < rhs); }
};
struct CentroidDecomposition {
int root;
vector<vector<int> > comp;
vector<int> par;
CentroidDecomposition(const vector<vector<Edge> > &graph) : graph(graph) {
int n = graph.size();
alive.assign(n, true);
subtree.resize(n);
comp.resize(n);
par.assign(n, -1);
root = build(0);
}
private:
const vector<vector<Edge> > graph;
vector<bool> alive;
vector<int> subtree;
int build(int s) {
int centroid = search_centroid(-1, s, calc_subtree(-1, s) / 2);
alive[centroid] = false;
for (const Edge &e : graph[centroid]) {
if (alive[e.dst]) {
comp[centroid].emplace_back(build(e.dst));
par[e.dst] = centroid;
}
}
alive[centroid] = true;
return centroid;
}
int calc_subtree(int par, int ver) {
subtree[ver] = 1;
for (const Edge &e : graph[ver]) {
if (e.dst != par && alive[e.dst]) subtree[ver] += calc_subtree(ver, e.dst);
}
return subtree[ver];
}
int search_centroid(int par, int ver, int half) {
for (const Edge &e : graph[ver]) {
if (e.dst != par && alive[e.dst]) {
if (subtree[e.dst] > half) return search_centroid(ver, e.dst, half);
}
}
return ver;
}
};
int main() {
int n, k; cin >> n >> k;
vector<vector<Edge> > graph(n);
REP(_, n - 1) {
int u, v, c; cin >> u >> v >> c; --u; --v; --c;
graph[u].emplace_back(u, v, c);
graph[v].emplace_back(v, u, c);
}
CentroidDecomposition cd(graph);
vector<bool> visited(n, false);
ll ans = 0;
function<void(int)> dfs = [&](int ver) {
visited[ver] = true;
map<int, ll> mp1, two;
ll one = 0;
map<pair<int, int>, ll> mp2;
for (const Edge &e : graph[ver]) {
if (visited[e.dst]) continue;
map<int, ll> nx_mp1;
map<pair<int, int>, ll> nx_mp2;
map<int, int> now;
now[e.cost] = 1;
ans += one - (mp1.count(e.cost) == 1 ? mp1[e.cost] : 0) + two[e.cost];
// cout << e.dst << ' ' << one << ' ' << ans << '\n';
nx_mp1[e.cost] = 1;
function<void(int, int)> dfs2 = [&](int p, int v) {
for (const Edge &d : graph[v]) {
if (d.dst == p || visited[d.dst]) continue;
++now[d.cost];
if (now.size() == 1) {
int x = now.begin()->first;
ans += one - (mp1.count(x) == 1 ? mp1[x] : 0) + two[x];
++nx_mp1[x];
} else if (now.size() == 2) {
int x = now.begin()->first, y = next(now.begin())->first;
ans += (mp1.count(x) == 1 ? mp1[x] : 0) + (mp1.count(y) == 1 ? mp1[y] : 0) + (mp2.count({x, y}) == 1 ? mp2[{x, y}] : 0) + 1;
++nx_mp2[{x, y}];
}
// cout << d.dst << ' ' << ans << '\n';
if (now.size() <= 2) dfs2(v, d.dst);
--now[d.cost];
if (now[d.cost] == 0) now.erase(d.cost);
}
};
dfs2(ver, e.dst);
for (auto pr : nx_mp1) {
mp1[pr.first] += pr.second;
one += pr.second;
}
for (auto pr : nx_mp2) {
mp2[pr.first] += pr.second;
two[pr.first.first] += pr.second;
two[pr.first.second] += pr.second;
}
}
// cout << ver << ' ' << ans << '\n';
for (int e : cd.comp[ver]) dfs(e);
};
dfs(cd.root);
cout << ans << '\n';
return 0;
}
emthrm