比赛 |
2025.6.2 |
评测结果 |
AAAAAAAAAA |
题目名称 |
0-1-Tree |
最终得分 |
100 |
用户昵称 |
健康铀 |
运行时间 |
0.464 s |
代码语言 |
C++ |
内存使用 |
5.42 MiB |
提交时间 |
2025-06-02 11:37:39 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 2e5 + 5;
const ll mod = 1e9 + 7;
ll n,z,res;
int f[M][2],siz[M][2];
int find(int x,int y){
if(!f[x][y])return x;
return f[x][y]=find(f[x][y],y);
}
int main()
{
freopen("0-1-Tree.in","r",stdin);
freopen("0-1-Tree.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n-1;i++){
int x,y,z;cin>>x>>y>>z;
int fx=find(x,z);
int fy=find(y,z);
if(fx!=fy){
f[fx][z]=fy;
}
}
for(int i=1;i<=n;i++){
siz[find(i,0)][0]++;
siz[find(i,1)][1]++;
}
for(int i=1;i<=n;i++){
res+=1ll* siz[find(i,1)][1]*siz[find(i,0)][0]-1;
}
cout<<res<<endl;
return 0;
}