-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsi17c3p5.cpp
More file actions
67 lines (60 loc) · 1.27 KB
/
si17c3p5.cpp
File metadata and controls
67 lines (60 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <vector>
#include <queue>
#include <unordered_map>
#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;
#define MAXN 100001
#define ll long long
using namespace std;
int N, par[MAXN];
ll val[MAXN];
bool set[MAXN];
vector<pair<int, ll>> adj[MAXN];
queue<int> q;
unordered_map<ll, ll> freq;
int main() {
scan(N);
int a, b;
char c;
for (int i = 1; i < N; i++) {
scan(a); scan(b);
c = getchar();
a--;b--;
adj[a].push_back({ b,c == 'r' ? 1 : -1 });
adj[b].push_back({ a,c == 'r' ? 1 : -1 });
}
q.push(0);
freq[0] = 1;
ll res = 0;
while (!q.empty()) {
int idx = q.front();
q.pop();
for (pair<int, int> p : adj[idx]) {
if (!set[p.first]) {
par[p.first] = idx;
ll *v = &val[p.first];
*v = val[idx] + p.second;
if (freq.find(-*v + 1) != freq.end()) {
res += freq[-*v + 1];
if (val[par[p.first]] == -*v + 1)
res--;
}
if (freq.find(-*v - 1) != freq.end()) {
res += freq[-*v - 1];
if (val[par[p.first]] == -*v - 1)
res--;
}
auto point = freq.find(*v);
if (point != freq.end())
point->second++;
else
freq[*v] = 1;
q.push(p.first);
}
}
set[idx] = true;
}
printf("%lld", res);
return 0;
}