-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubTreeSum.cpp
44 lines (34 loc) · 882 Bytes
/
subTreeSum.cpp
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
#include <bits/stdc++.h>
#define mx 1000000
using namespace std;
vector<int> graph[mx];
bool vis[mx];
int subTreeSum[mx];
void dfs(int vertex, int parent) {
for (int i = 0; i < graph[vertex].size(); i++) {
int next = graph[vertex][i];
if(next==parent){
continue;
}
dfs(next,vertex);
subTreeSum[vertex]+=subTreeSum[next];
}
subTreeSum[vertex]+=vertex;
}
void solve() {
int node,edge, source = 1;
cin >> node >> edge ;
for (int i = 0; i < edge; i++) {
int v1, v2;
cin >> v1 >> v2;
graph[v1].push_back(v2);
graph[v2].push_back(v1);
}
dfs(source,0);
for(int i=1;i<=node;i++){
cout<<subTreeSum[i]<<endl;
}
}
int main() {
solve();
}