-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
105 lines (104 loc) · 2.28 KB
/
main.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
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
#include <iostream>
#include <list>
#include <stdlib.h>
using namespace std;
list <long long int> visitedmums;
long long int c=0;
class Graph
{
long long int V;
list<long long int> *adj;
void DFSUtil(long long int v, bool visited[],long long int museums[],long long int a)
{
visited[v] = true;
c+=museums[v];
list<long long int>::iterator i;
for(i = adj[v].begin(); i != adj[v].end(); ++i)
{
if(!visited[*i])
{
DFSUtil(*i, visited,museums,c);
}
}
}
public:
Graph(long long int V)
{
this->V = V;
adj = new list<long long int>[V];
}
void addEdge(long long int v, long long int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
void connectedComponents(long long int museums[])
{
bool *visited = new bool[V];
for(long long int v = 0; v < V; v++)
{
visited[v] = false;
}
for (int v=0; v<V; v++)
{
if (visited[v] == false)
{
DFSUtil(v, visited,museums,c);
visitedmums.push_back(c);
c=0;
}
}
}
};
int main()
{
long long int n,m,k,i,x,y;
long long int tmsum=0,tvm=0;
cin>>n>>m>>k;
Graph g(n);
long long int museums[n];
for(i=0;i<m;i++)
{
cin>>x >>y;
g.addEdge((x-1),(y-1));
}
for(i=0;i<n;i++)
{
cin>>museums[i];
tmsum+=museums[i];
}
g.connectedComponents(museums);
if (visitedmums.size()==k)
{
cout<<tmsum;
}
else
{
if(visitedmums.size()<k)
{
cout<<-1;
exit(0);
}
visitedmums.sort();
for(i=1;i<=k;i++)
{
if(i%2!=0)
{
tvm+=visitedmums.back();
visitedmums.pop_back();
}
if(i%2==0)
{
tvm+=visitedmums.front();
visitedmums.pop_front();
}
if(tvm==tmsum && i<k)
{
cout<<-1;
exit(0);
}
}
cout<<tvm;
}
return 0;
}