結果
| 問題 |
No.1002 Twotone
|
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2020-02-28 22:29:57 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,915 bytes |
| コンパイル時間 | 3,681 ms |
| コンパイル使用メモリ | 218,412 KB |
| 最終ジャッジ日時 | 2025-01-09 02:53:34 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 24 TLE * 9 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
struct Centroid{
vector<Int> sz,dead;
vector< vector<Int> > G;
Centroid(){}
Centroid(Int n):sz(n,1),dead(n,0),G(n){}
void add_edge(Int u,Int v){
G[u].emplace_back(v);
G[v].emplace_back(u);
}
Int dfs(Int v,Int p){
sz[v]=1;
for(Int u:G[v])
if(u!=p&&!dead[u]) sz[v]+=dfs(u,v);
return sz[v];
}
void find(Int v,Int p,Int tmp,vector<Int> &cs) {
Int ok=1;
for (Int u:G[v]){
if(u==p||dead[u]) continue;
find(u,v,tmp,cs);
ok&=(sz[u]<=tmp/2);
}
ok&=(tmp-sz[v]<=tmp/2);
if(ok) cs.push_back(v);
}
vector<Int> build(Int r) {
Int tmp=dfs(r,-1);
vector<Int> cs;
find(r,-1,tmp,cs);
return cs;
}
void disable(Int v){
dead[v]=1;
}
void enable(Int v){
dead[v]=0;
}
Int alive(Int v){
return !dead[v];
}
};
template<typename F>
struct FixPoint : F{
FixPoint(F&& f):F(forward<F>(f)){}
template<typename... Args>
decltype(auto) operator()(Args&&... args) const{
return F::operator()(*this,forward<Args>(args)...);
}
};
template<typename F>
inline decltype(auto) MFP(F&& f){
return FixPoint<F>{forward<F>(f)};
}
struct FastIO{
FastIO(){
cin.tie(0);
ios::sync_with_stdio(0);
}
}fastio_beet;
//INSERT ABOVE HERE
signed main(){
Int n,k;
cin>>n>>k;
using P = pair<Int, Int>;
map<P, Int> col;
Centroid G(n);
for(Int i=1;i<n;i++){
Int x,y,z;
cin>>x>>y>>z;
x--;y--;z--;
G.add_edge(x,y);
col[P(x,y)]=z;
col[P(y,x)]=z;
}
queue<Int> que;
que.emplace(G.build(0)[0]);
Int ans=0,all=0;
vector<Int> cnt(k,0),uku(k,0);
while(!que.empty()){
Int r=que.front();que.pop();
auto calc=
[&](P p,Int q)->P{
assert(~p.first);
if(p.first==q||p.second==q) return p;
if(p.second==-1) return minmax({p.first,q});
return P(-1,-1);
};
map<P, Int> num;
for(Int t:G.G[r]){
if(!G.alive(t)) continue;
// count
MFP([&](auto dfs,Int v,Int p,P st)->void{
if(st.first<0) return;
for(Int u:G.G[v]){
if(u==p) continue;
if(!G.alive(u)) continue;
dfs(u,v,calc(st,col[P(u,v)]));
}
if(st.second==-1){
ans+=all-cnt[st.first];
ans+=uku[st.first];
}else{
ans+=1;
ans+=cnt[st.first];
ans+=cnt[st.second];
ans+=num[st];
}
})(t,r,P(col[P(r,t)],-1));
// paint
MFP([&](auto dfs,Int v,Int p,P st)->void{
if(st.first<0) return;
for(Int u:G.G[v]){
if(u==p) continue;
if(!G.alive(u)) continue;
dfs(u,v,calc(st,col[P(u,v)]));
}
if(st.second==-1){
cnt[st.first]++;
all++;
}else{
num[st]++;
uku[st.first]++;
uku[st.second]++;
}
})(t,r,P(col[P(r,t)],-1));
}
for(Int t:G.G[r]){
if(!G.alive(t)) continue;
// clear
MFP([&](auto dfs,Int v,Int p,P st)->void{
if(st.first<0) return;
for(Int u:G.G[v]){
if(u==p) continue;
if(!G.alive(u)) continue;
dfs(u,v,calc(st,col[P(u,v)]));
}
if(st.second==-1){
cnt[st.first]--;
all--;
}else{
num[st]--;
uku[st.first]--;
uku[st.second]--;
}
})(t,r,P(col[P(r,t)],-1));
}
G.disable(r);
for(Int u:G.G[r])
if(G.alive(u))
que.emplace(G.build(u)[0]);
}
cout<<ans<<endl;
return 0;
}
beet