-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathe.cpp
58 lines (54 loc) · 922 Bytes
/
e.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
#define endl '\n'
#define eat cin
#define moo cout
#define int long long
using namespace std;
int N, M, W[200000], S;
vector<int> graph[200000];
int de[200000], vis[200000];
int mxd = 0;
void dfs(int i, int p){
if(vis[i]){
return;
}
vis[i] = true;
int cde = 0;
bool all_de = 1;
for(int c : graph[i]){
if(c == p) continue;
dfs(c, i);
if(~de[c]){
cde = max(cde, de[c]);
}else{
all_de = 0;
}
}
if(all_de){
de[i] = W[i] + cde;
}
}
int32_t main(){
eat.tie(0) -> sync_with_stdio(0);
eat >> N >> M;
for(int i = 0; i < N; i++){
eat >> W[i];
}
for(int i = 0; i < M; i++){
int u, v; eat >> u >> v;
graph[u-1].push_back(v-1);
graph[v-1].push_back(u-1);
}
fill(de, de+N, -1);
eat >> S;
dfs(S-1, -1);
int ans1 = 0, ans2 = 0;
for(int i = 0; i < N; i++){
if(~de[i]){
ans2 = max(ans2, de[i]);
}else{
ans1 += W[i];
}
}
moo << ans1 + ans2 << endl;
}