# USACO 2020 February Contest, Silver Problem 3. Clock Tree

Apr 16th, 2021
670
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <bits/stdc++.h>
2.
3. using namespace std;
4.
5. ifstream fin("clocktree.in");
6. ofstream fout("clocktree.out");
7.
8. const int NMAX = 2505;
9. int a[NMAX], n[2], s[2];
10. vector<int> G[NMAX];
11.
12. void dfs(int u, int parent, int depth) {
13.   ++n[depth & 1];
14.   s[depth & 1] += a[u];
15.   for (int v : G[u])
16.     if (v != parent)
17.       dfs(v, u, depth + 1);
18. }
19.
20. void solve() {
21.   int N;
22.   fin >> N;
23.   for (int i = 1; i <= N; ++i)
24.     fin >> a[i];
25.   for (int i = 1; i < N; ++i) {
26.     int u, v;
27.     fin >> u >> v;
28.     G[u].emplace_back(v);
29.     G[v].emplace_back(u);
30.   }
31.   dfs(1, 0, 0);
32.   if (s[0] % 12 == s[1] % 12)
33.     fout << N << '\n';
34.   else {
35.     if ((s[0] + 1) % 12 == s[1] % 12)
36.       fout << n[1] << '\n';
37.     else {
38.       if ((s[1] + 1) % 12 == s[0] % 12)
39.         fout << n[0] << '\n';
40.       else fout << "0\n";
41.     }
42.   }
43. }
44.
45. void close_files() {
46.   fin.close();
47.   fout.close();
48. }
49.
50. int main() {
51.   solve();
52.   close_files();
53.   return 0;
54. }
55.
RAW Paste Data