-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkom.cpp
More file actions
executable file
·81 lines (71 loc) · 1.05 KB
/
kom.cpp
File metadata and controls
executable file
·81 lines (71 loc) · 1.05 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
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<int>
#define ii pair<int, int>
#define vii vector<ii>
vi adj[100001];
bool visited[100001];
void initialise()
{
for(ll i=0; i<100001; i++)
adj[i].clear();
}
ll dfs(ll r, ll* array)
{
ll ans=0;
stack<ll> s;
s.push(r);
ans+=array[r];
visited[r]=true;
while(!s.empty())
{
ll v = s.top();
if(!visited[v])
ans+=array[v];
s.pop();
for(ll j=0; j<(ll)adj[v].size(); j++)
{
if(!visited[adj[v][j]])
s.push(adj[v][j]);
}
if(!visited[v])
{
visited[v]=true;
}
}
return ans;
}
int main(void)
{
int t;
cin>>t;
while(t--)
{
initialise();
memset(visited, 0, sizeof(visited));
ll n, m, x, y, score=0;
cin>>n>>m;
ll a[n];
for(ll i=0; i<m; i++)
{
cin>>x>>y;
adj[x-1].push_back(y-1);
adj[y-1].push_back(x-1);
}
for(ll i=0; i<n; i++)
cin>>a[i];
for(ll i=0; i<n; i++)
{
if(!visited[i])
{
ll temp;
temp=dfs(i, a);
if(temp>score)
score=temp;
}
}
cout<<score<<"\n";
}
return 0;
}