-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathccc03s5.cpp
59 lines (54 loc) · 1.13 KB
/
ccc03s5.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
#include <bits/stdc++.h>
using namespace std;
vector<int> par (10001), rnk(10001), dest(10001);
int find(int n)
{
if (n != par[n])
par[n] = find(par[n]);
return par[n];
}
bool merge(int a, int b)
{
a = find(a), b = find(b);
if (a == b) return false;
if (rnk[a] > rnk[b])
par[b] = a;
else
par[a] = b;
if (rnk[a] == rnk[b])
rnk[b]++;
return true;
}
int main()
{
iota(par.begin(), par.end(), 0);
vector<pair<int,pair<int,int>>> edge;
int n, m, d, a, b, c;
scanf("%i%i%i", &n, &m, &d);
while (m--)
{
scanf("%i%i%i", &a, &b, &c);
edge.push_back(make_pair(c, make_pair(a, b)));
}
sort(edge.rbegin(), edge.rend());
for (int x = 0; x < d; x++)
{
scanf("%i", &a);
dest[a] = true;
}
for (auto &x : edge)
{
a = x.second.first, b = x.second.second, c = x.first;
if (merge(a, b) && d)
{
if (dest[a]) dest[a] = false, d--;
if (dest[b]) dest[b] = false, d--;
}
if (!d)
{
printf("%i", c);
break;
}
}
return 0;
}