-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubTree.cpp
More file actions
114 lines (95 loc) · 2.63 KB
/
SubTree.cpp
File metadata and controls
114 lines (95 loc) · 2.63 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <bits/stdc++.h>
using namespace std;
static auto _ = []() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 0;
}();
#define frw(start, end) for(ll i = start; i < end; i++)
#define frb(start, end) for(ll i = end; i >= start; i--)
#define YES cout << "Yes \n";
#define NOO cout << "No \n";
#define nl cout << "\n";
#define MAX_SIZE 10000
#define nptr nullptr
typedef stringstream strgm;
typedef long long int ll;
typedef const int c_int;
typedef unsigned unsg;
typedef double dbl;
typedef vector<vector<string>> vvstr;
typedef vector<vector<bool>> vvbl;
typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<string> vstr;
typedef vector<int> vint;
typedef vector<bool> vbl;
typedef vector<ll> vll;
typedef stack<string> sstr;
typedef stack<bool> sbl;
typedef stack<int> sint;
typedef stack<ll> sll;
typedef queue<string> qstr;
typedef queue<bool> qbl;
typedef queue<int> qint;
typedef queue<ll> qll;
c_int MOD = 1e9 + 7;
c_int Mx_row = 100;
c_int Mx_col = 100;
int size_stack = 0;
int InvrsnCnt = 0;
int size_arr = 0;
int size_ll = 0;
int top = -1;
c_int N = 1e5 + 10;
vint g[N];
vbl Prime(N + 1, true);
void isPrime() {
Prime[0] = Prime[1] = false;
for (int i = 2; i * i <= N; i++) {
if (Prime[i]) {
for (int j = i * i; j <= N; j += i) {
Prime[j] = false;
}
}
}
}
void dfs(int vrtx, int prnt, vint& SubTree, vint& OddCount, vint& EvenCount, vint& PrimeCount) {
SubTree[vrtx] = vrtx;
if (vrtx & 1) OddCount[vrtx] = 1;
else EvenCount[vrtx] = 1;
if (Prime[vrtx]) PrimeCount[vrtx] = 1;
for (auto child : g[vrtx]) {
if (child == prnt) continue;
dfs(child, vrtx, SubTree, OddCount, EvenCount, PrimeCount);
SubTree[vrtx] += SubTree[child];
OddCount[vrtx] += OddCount[child];
EvenCount[vrtx] += EvenCount[child];
PrimeCount[vrtx] += PrimeCount[child];
}
}
int main() {
int Nodes; cin >> Nodes;
frw(0, Nodes - 1) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
isPrime();
vint SubtreeSum(Nodes + 1, 0);
vint OddCount(Nodes + 1, 0);
vint EvenCount(Nodes + 1, 0);
vint PrimeCount(Nodes + 1, 0);
dfs(1, 0, SubtreeSum, OddCount, EvenCount, PrimeCount);
frw(1, Nodes + 1) {
cout << "Node " << i << ":\n";
cout << "Subtree sum: " << SubtreeSum[i] << "\n";
cout << "Odd count: " << OddCount[i] << "\n";
cout << "Even count: " << EvenCount[i] << "\n";
cout << "Prime count: " << PrimeCount[i] << "\n";
nl
}
return 0;
}