#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
int n, a, b, k;
int par[200005], sz[200005];
int getr(int x){return (par[x] == x ? x : par[x] = getr(par[x]));}
void merge(int a, int b){
a = getr(a), b = getr(b);
if(a == b)return;
sz[a] += sz[b];
par[b] = a;
}
vector <pi> stuf;
vector <pii> adj, adj2;
int ans = 1e18;
void solve(){
cin >> n >> a >> b >> k;
if(k == 0){
cout << 0; return;}
for(int i=1;i<=a;i++){
int x, y, z; cin >> x >> y >> z;
adj.push_back({z, {x, y}});
}
for(int i=1;i<=b;i++){
int x, y, z; cin >> x >> y >> z;
adj2.push_back({z, {x, y}});
}
sort(adj.begin(), adj.end());
sort(adj2.begin(), adj2.end());
for(int i=1;i<=n;i++)par[i] = i, sz[i] = 1;
stuf.push_back({0, 0});
int cur = 0;
for(auto i : adj){
int w = i.fi, x = i.se.fi, y = i.se.se;
if(getr(x) == getr(y))continue;
cur += sz[getr(x)] * sz[getr(y)];
if(cur >= k)ans = min(ans, w);
merge(x, y);
stuf.push_back({cur, w});
}
cur = 0;
for(int i=1;i<=n;i++)par[i] = i, sz[i] = 1;
for(auto i : adj2){
int w = i.fi, x = i.se.fi, y = i.se.se;
if(getr(x) == getr(y))continue;
cur += sz[getr(x)] * sz[getr(y)];
merge(x, y);
pi bruh = {k - cur, -1};
auto it = lower_bound(stuf.begin(), stuf.end(), bruh);
if(it == stuf.end())continue;
ans = min(ans, (*it).se + w);
}
cout << (ans == (int)1e18 ? -1 : ans);
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
Main.cpp:66:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
66 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
11496 KB |
Output is correct |
2 |
Correct |
94 ms |
11648 KB |
Output is correct |
3 |
Correct |
86 ms |
8188 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
121 ms |
18972 KB |
Output is correct |
8 |
Correct |
112 ms |
19464 KB |
Output is correct |
9 |
Correct |
91 ms |
12648 KB |
Output is correct |
10 |
Correct |
95 ms |
16172 KB |
Output is correct |
11 |
Correct |
170 ms |
24556 KB |
Output is correct |
12 |
Correct |
154 ms |
25008 KB |
Output is correct |
13 |
Correct |
160 ms |
25296 KB |
Output is correct |
14 |
Correct |
164 ms |
25300 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
3400 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
185 ms |
25312 KB |
Output is correct |
20 |
Correct |
195 ms |
25304 KB |
Output is correct |
21 |
Correct |
191 ms |
25424 KB |
Output is correct |
22 |
Correct |
190 ms |
25336 KB |
Output is correct |
23 |
Correct |
196 ms |
25340 KB |
Output is correct |
24 |
Correct |
184 ms |
25352 KB |
Output is correct |
25 |
Correct |
192 ms |
25332 KB |
Output is correct |
26 |
Correct |
183 ms |
25336 KB |
Output is correct |
27 |
Correct |
186 ms |
25332 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
196 ms |
25308 KB |
Output is correct |
30 |
Correct |
181 ms |
25280 KB |
Output is correct |
31 |
Correct |
179 ms |
25320 KB |
Output is correct |
32 |
Correct |
189 ms |
25284 KB |
Output is correct |
33 |
Correct |
176 ms |
25200 KB |
Output is correct |
34 |
Correct |
177 ms |
24036 KB |
Output is correct |
35 |
Correct |
206 ms |
23944 KB |
Output is correct |
36 |
Correct |
172 ms |
23888 KB |
Output is correct |
37 |
Correct |
168 ms |
23928 KB |
Output is correct |
38 |
Correct |
152 ms |
20160 KB |
Output is correct |
39 |
Correct |
153 ms |
20264 KB |
Output is correct |
40 |
Correct |
149 ms |
20248 KB |
Output is correct |
41 |
Correct |
147 ms |
20156 KB |
Output is correct |
42 |
Correct |
126 ms |
18920 KB |
Output is correct |
43 |
Correct |
123 ms |
18960 KB |
Output is correct |
44 |
Correct |
131 ms |
19884 KB |
Output is correct |
45 |
Correct |
136 ms |
19976 KB |
Output is correct |
46 |
Correct |
199 ms |
25300 KB |
Output is correct |
47 |
Correct |
184 ms |
25432 KB |
Output is correct |
48 |
Correct |
181 ms |
25340 KB |
Output is correct |
49 |
Correct |
177 ms |
22616 KB |
Output is correct |
50 |
Correct |
180 ms |
22516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
61 ms |
8608 KB |
Output is correct |
4 |
Correct |
58 ms |
8764 KB |
Output is correct |
5 |
Correct |
98 ms |
12684 KB |
Output is correct |
6 |
Correct |
95 ms |
12720 KB |
Output is correct |
7 |
Correct |
96 ms |
12596 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
75 ms |
12060 KB |
Output is correct |
10 |
Correct |
97 ms |
13672 KB |
Output is correct |
11 |
Correct |
86 ms |
12072 KB |
Output is correct |
12 |
Correct |
87 ms |
14268 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
95 ms |
12600 KB |
Output is correct |
18 |
Correct |
89 ms |
12652 KB |
Output is correct |
19 |
Correct |
100 ms |
12672 KB |
Output is correct |
20 |
Correct |
87 ms |
12680 KB |
Output is correct |
21 |
Correct |
93 ms |
12648 KB |
Output is correct |
22 |
Correct |
95 ms |
12644 KB |
Output is correct |
23 |
Correct |
94 ms |
12680 KB |
Output is correct |
24 |
Correct |
101 ms |
12680 KB |
Output is correct |
25 |
Correct |
96 ms |
12752 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
93 ms |
12680 KB |
Output is correct |
28 |
Correct |
99 ms |
12692 KB |
Output is correct |
29 |
Incorrect |
112 ms |
12720 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |