标签:== std dfs else main 思维 合数 节点 problem
题目链接:https://ac.nowcoder.com/acm/problem/20823
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 100005;
int n;
struct node{
int x,z;
};
vector<node> v[maxn];
ll cnt1 = 0,cnt2 = 0;
void dfs(int x,int fa,int z)
{
if(z%2)cnt1++;
else cnt2++;
for(int i=0;i<v[x].size();i++){
if(v[x][i].x == fa)
continue ;
dfs(v[x][i].x , x , (z+v[x][i].z)%2);
}
}
int main()
{
cin>>n;
for(int i=1;i<n;i++){
int x,y,z;
cin>>x>>y>>z;
v[x].push_back(node{y , z});
v[y].push_back(node{x , z});
}
dfs(1, 0 , 0);
cout<<cnt1*cnt1*cnt1 + cnt2*cnt2*cnt2<<endl;
return 0;
}
标签:== std dfs else main 思维 合数 节点 problem
原文地址:https://www.cnblogs.com/QFNU-ACM/p/12536460.html