牛客训练赛30:C
https://ac.nowcoder.com/acm/contest/216/C
计算边权%2: (i, j) = (j, k) = (k, i)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
#define Pir pair<int, int>
typedef long long ll;
vector<Pir> G[maxn];
int cnt[2];
void dfs(int u, int fa = 0, int dep = 0) {
++cnt[dep];
for(Pir p:G[u]) if(p.first != fa) {
dfs(p.first, u, (dep ^ p.second) & 1);
}
}
int main()
{
int n;
scanf("%d", &n);
register int i;
for(i = 1; i < n; i++) {
int u, v, d;
scanf("%d%d%d", &u, &v, &d);
d &= 1;
G[u].emplace_back(Pir(v, d));
G[v].emplace_back(Pir(u, d));
}
dfs(n);
printf("%lld\n", (ll)cnt[0] * cnt[0] * cnt[0] + (ll)cnt[1] * cnt[1] * cnt[1]);
return 0;
}
vector的emplace_back的性能优于push_back
c++11的for(pair<int, int> p:G[u]) { }遍历