-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrumour.cpp
More file actions
90 lines (76 loc) · 1.4 KB
/
rumour.cpp
File metadata and controls
90 lines (76 loc) · 1.4 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
#include<bits/stdc++.h>
using namespace std;
map<int,vector<int> > Tree;
void getgraph()
{
int n,e;
cin>>n>>e;
int u,v;
for(int i=0;i<e;i++)
{
cin>>u>>v;
//if(u>v)
Tree[u].push_back(v);
//else
Tree[v].push_back(u);
}
}
int main()
{
//getgraph();
long long n,e;
cin>>n>>e;
long long A[n+1];
A[0] = -1;
for(int i=0;i<n;i++) cin>>A[i+1];
int u,v;
for(int i=0;i<e;i++)
{
cin>>u>>v;
//if(u>v)
Tree[u].push_back(v);
//else
Tree[v].push_back(u);
}
set<int> V;
set<int>::iterator it;
set<long long>::iterator itn;
long long count = 0;
for(int j=1;j<=n;j++){ V.insert(j);}
//if(V.find(4)==V.end()) cout<<"Hii";
//V.clear();
queue<int> Q;
set<long long> N;
while(!V.empty())
{
it = V.begin();
int ver = *it;
Q.push(ver);
N.insert(A[ver]);
V.erase(it);
while(!Q.empty())
{
ver = Q.front();
Q.pop();
for(int k=0;k<Tree[ver].size();k++)
{
int x = Tree[ver][k];
//cout<<"Searching"<<x<<"\n";
if(V.find(x) == V.end())
{
continue;
}
else {
Q.push(x);
N.insert(A[x]);
V.erase(V.find(x));
}
// Q.erase(Q.begin(),Q.end());
}
}
itn = N.begin();
count = count + (*itn);
N.clear();
}
cout<<count;
}