#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=2e5+5;
const ll inf=2e18;
int n,A,B,k,pa[maxN+1],par[maxN+1];
ll ca[maxN+1],cb[maxN+1];
map<pii,int> mp;
struct canh
{
int u,v;
ll w;
};
struct node
{
int u,pre,cur;
};
vector<node> vec_a[maxN+1],vec_b[maxN+1];
canh a[maxN+1],b[maxN+1];
struct dsu
{
int parent[maxN+1];
ll cnt;
vector<int> vec[maxN+1];
dsu()
{
memset(parent,-1,sizeof(parent));
for(int i=1;i<=n;i++)
{
vec[i].clear();
}
cnt=0;
}
void clr()
{
memset(parent,-1,sizeof(parent));
for(int i=1;i<=n;i++)
{
vec[i].clear();
}
cnt=0;
}
int Find(int v)
{
if(parent[v]<0)
{
return v;
}
return parent[v]=Find(parent[v]);
}
vector<node> Union(int a,int b)
{
a=Find(a);
b=Find(b);
vector<node> vt;
if(a==b)
{
return vt;
}
if(-parent[a]<-parent[b])
{
swap(a,b);
}
cnt+=(parent[a]*parent[b]);
parent[a]+=parent[b];
parent[b]=a;
//cout<<vec[a].size()<<" "<<vec[b].size()<<" ";
for(auto i:vec[b])
{
vec[a].push_back(i);
vt.push_back({i,b,a});
}
return vt;
}
}ds;
int main()
{
//freopen("","r",stdin);
//freopen("","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>A>>B>>k;
for(int i=1;i<=A;i++)
{
cin>>a[i].u>>a[i].v>>a[i].w;
}
sort(a+1,a+A+1,[&](canh x,canh y)
{
return x.w<y.w;
});
for(int i=1;i<=B;i++)
{
cin>>b[i].u>>b[i].v>>b[i].w;
}
sort(b+1,b+B+1,[&](canh x,canh y)
{
return x.w<y.w;
});
if(k==0)
{
cout<<0;
return 0;
}
ll ans=inf;
for(int i=1;i<=n;i++)
{
ds.vec[i].push_back(i);
}
for(int i=1;i<=A;i++)
{
//cout<<a[i].u<<" "<<a[i].v<<" "<<a[i].w<<'\n';
vec_a[i]=ds.Union(a[i].u,a[i].v);
ca[i]=ds.cnt;
if(ca[i]>=k)
{
ans=min(ans,a[i].w);
}
}
ds.clr();
for(int i=1;i<=n;i++)
{
ds.vec[i].push_back(i);
}
for(int i=1;i<=B;i++)
{
vec_b[i]=ds.Union(b[i].u,b[i].v);
cb[i]=ds.cnt;
if(cb[i]>=k)
{
ans=min(ans,b[i].w);
}
}
for(int i=1;i<=n;i++)
{
pa[i]=i;
par[i]=ds.Find(i);
mp[{pa[i],par[i]}]++;
}
ll ch=0;
for(int i=1,j=B;i<=A;i++)
{
for(auto [u,pre,cur]:vec_a[i])
{
mp[{pa[u],par[u]}]--;
ch-=mp[{pa[u],par[u]}];
pa[u]=cur;
ch+=mp[{pa[u],par[u]}];
mp[{pa[u],par[u]}]++;
}
while(j>=0&&ca[i]+cb[j]-ch>=k)
{
for(auto [u,pre,cur]:vec_b[j])
{
mp[{pa[u],par[u]}]--;
ch-=mp[{pa[u],par[u]}];
par[u]=pre;
ch+=mp[{pa[u],par[u]}];
mp[{pa[u],par[u]}]++;
}
j--;
}
if(j!=B)
{
//cout<<i<<" "<<j<<" "<<ch<<'\n';
ans=min(ans,a[i].w+b[j+1].w);
}
}
if(ans==inf)
{
cout<<-1;
}
else cout<<ans;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:147:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
147 | for(auto [u,pre,cur]:vec_a[i])
| ^
Main.cpp:157:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
157 | for(auto [u,pre,cur]:vec_b[j])
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
16728 KB |
Output is correct |
2 |
Correct |
5 ms |
16732 KB |
Output is correct |
3 |
Correct |
6 ms |
16924 KB |
Output is correct |
4 |
Correct |
6 ms |
16732 KB |
Output is correct |
5 |
Correct |
6 ms |
16732 KB |
Output is correct |
6 |
Correct |
58 ms |
22376 KB |
Output is correct |
7 |
Correct |
65 ms |
23944 KB |
Output is correct |
8 |
Correct |
102 ms |
28856 KB |
Output is correct |
9 |
Correct |
106 ms |
29008 KB |
Output is correct |
10 |
Correct |
113 ms |
29008 KB |
Output is correct |
11 |
Correct |
106 ms |
28992 KB |
Output is correct |
12 |
Correct |
6 ms |
16728 KB |
Output is correct |
13 |
Correct |
7 ms |
16988 KB |
Output is correct |
14 |
Correct |
6 ms |
16948 KB |
Output is correct |
15 |
Correct |
100 ms |
30284 KB |
Output is correct |
16 |
Correct |
139 ms |
29324 KB |
Output is correct |
17 |
Correct |
112 ms |
29268 KB |
Output is correct |
18 |
Correct |
111 ms |
29264 KB |
Output is correct |
19 |
Correct |
106 ms |
29364 KB |
Output is correct |
20 |
Correct |
107 ms |
29264 KB |
Output is correct |
21 |
Correct |
114 ms |
29248 KB |
Output is correct |
22 |
Correct |
106 ms |
29268 KB |
Output is correct |
23 |
Correct |
103 ms |
28868 KB |
Output is correct |
24 |
Correct |
103 ms |
30288 KB |
Output is correct |
25 |
Correct |
115 ms |
29268 KB |
Output is correct |
26 |
Correct |
141 ms |
29344 KB |
Output is correct |
27 |
Correct |
107 ms |
29312 KB |
Output is correct |
28 |
Correct |
111 ms |
29264 KB |
Output is correct |
29 |
Correct |
117 ms |
29268 KB |
Output is correct |
30 |
Correct |
108 ms |
29252 KB |
Output is correct |
31 |
Correct |
106 ms |
29268 KB |
Output is correct |
32 |
Correct |
107 ms |
28968 KB |
Output is correct |
33 |
Correct |
102 ms |
27732 KB |
Output is correct |
34 |
Correct |
85 ms |
27988 KB |
Output is correct |
35 |
Correct |
97 ms |
27984 KB |
Output is correct |
36 |
Correct |
86 ms |
27984 KB |
Output is correct |
37 |
Correct |
7 ms |
17240 KB |
Output is correct |
38 |
Correct |
7 ms |
17148 KB |
Output is correct |
39 |
Correct |
7 ms |
17084 KB |
Output is correct |
40 |
Correct |
7 ms |
17244 KB |
Output is correct |
41 |
Correct |
95 ms |
27792 KB |
Output is correct |
42 |
Correct |
100 ms |
27728 KB |
Output is correct |
43 |
Correct |
119 ms |
28368 KB |
Output is correct |
44 |
Correct |
108 ms |
28292 KB |
Output is correct |
45 |
Correct |
112 ms |
29264 KB |
Output is correct |
46 |
Correct |
113 ms |
29112 KB |
Output is correct |
47 |
Correct |
111 ms |
29088 KB |
Output is correct |
48 |
Correct |
88 ms |
27960 KB |
Output is correct |
49 |
Correct |
86 ms |
27984 KB |
Output is correct |
50 |
Correct |
7 ms |
17500 KB |
Output is correct |
51 |
Correct |
65 ms |
24084 KB |
Output is correct |
52 |
Correct |
58 ms |
22360 KB |
Output is correct |
53 |
Correct |
6 ms |
16984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
538 ms |
73164 KB |
Output is correct |
2 |
Correct |
332 ms |
59848 KB |
Output is correct |
3 |
Correct |
325 ms |
58504 KB |
Output is correct |
4 |
Correct |
6 ms |
16732 KB |
Output is correct |
5 |
Correct |
6 ms |
16732 KB |
Output is correct |
6 |
Correct |
7 ms |
16732 KB |
Output is correct |
7 |
Correct |
92 ms |
27792 KB |
Output is correct |
8 |
Correct |
73 ms |
27732 KB |
Output is correct |
9 |
Correct |
140 ms |
48072 KB |
Output is correct |
10 |
Correct |
386 ms |
62412 KB |
Output is correct |
11 |
Correct |
72 ms |
45068 KB |
Output is correct |
12 |
Correct |
68 ms |
44884 KB |
Output is correct |
13 |
Correct |
66 ms |
44880 KB |
Output is correct |
14 |
Correct |
67 ms |
45068 KB |
Output is correct |
15 |
Correct |
5 ms |
16732 KB |
Output is correct |
16 |
Correct |
55 ms |
37160 KB |
Output is correct |
17 |
Correct |
54 ms |
37204 KB |
Output is correct |
18 |
Correct |
108 ms |
31916 KB |
Output is correct |
19 |
Correct |
617 ms |
85660 KB |
Output is correct |
20 |
Correct |
642 ms |
85232 KB |
Output is correct |
21 |
Correct |
665 ms |
85132 KB |
Output is correct |
22 |
Correct |
605 ms |
85800 KB |
Output is correct |
23 |
Correct |
614 ms |
85448 KB |
Output is correct |
24 |
Correct |
626 ms |
85992 KB |
Output is correct |
25 |
Correct |
616 ms |
85396 KB |
Output is correct |
26 |
Correct |
717 ms |
85192 KB |
Output is correct |
27 |
Correct |
70 ms |
44900 KB |
Output is correct |
28 |
Correct |
108 ms |
32076 KB |
Output is correct |
29 |
Correct |
637 ms |
85740 KB |
Output is correct |
30 |
Correct |
617 ms |
85896 KB |
Output is correct |
31 |
Correct |
619 ms |
85284 KB |
Output is correct |
32 |
Correct |
618 ms |
85496 KB |
Output is correct |
33 |
Correct |
610 ms |
85248 KB |
Output is correct |
34 |
Correct |
672 ms |
85196 KB |
Output is correct |
35 |
Correct |
617 ms |
85192 KB |
Output is correct |
36 |
Correct |
625 ms |
84940 KB |
Output is correct |
37 |
Correct |
73 ms |
44876 KB |
Output is correct |
38 |
Correct |
226 ms |
49552 KB |
Output is correct |
39 |
Correct |
225 ms |
49744 KB |
Output is correct |
40 |
Correct |
233 ms |
49916 KB |
Output is correct |
41 |
Correct |
228 ms |
49744 KB |
Output is correct |
42 |
Correct |
106 ms |
27720 KB |
Output is correct |
43 |
Correct |
102 ms |
27656 KB |
Output is correct |
44 |
Correct |
98 ms |
28244 KB |
Output is correct |
45 |
Correct |
96 ms |
28316 KB |
Output is correct |
46 |
Correct |
632 ms |
85620 KB |
Output is correct |
47 |
Correct |
647 ms |
85552 KB |
Output is correct |
48 |
Correct |
65 ms |
44884 KB |
Output is correct |
49 |
Correct |
618 ms |
84136 KB |
Output is correct |
50 |
Incorrect |
66 ms |
44880 KB |
Output isn't correct |
51 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
16732 KB |
Output is correct |
2 |
Correct |
6 ms |
16732 KB |
Output is correct |
3 |
Correct |
63 ms |
19928 KB |
Output is correct |
4 |
Correct |
43 ms |
21844 KB |
Output is correct |
5 |
Correct |
525 ms |
74436 KB |
Output is correct |
6 |
Incorrect |
67 ms |
40276 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
16728 KB |
Output is correct |
2 |
Correct |
5 ms |
16732 KB |
Output is correct |
3 |
Correct |
6 ms |
16924 KB |
Output is correct |
4 |
Correct |
6 ms |
16732 KB |
Output is correct |
5 |
Correct |
6 ms |
16732 KB |
Output is correct |
6 |
Correct |
58 ms |
22376 KB |
Output is correct |
7 |
Correct |
65 ms |
23944 KB |
Output is correct |
8 |
Correct |
102 ms |
28856 KB |
Output is correct |
9 |
Correct |
106 ms |
29008 KB |
Output is correct |
10 |
Correct |
113 ms |
29008 KB |
Output is correct |
11 |
Correct |
106 ms |
28992 KB |
Output is correct |
12 |
Correct |
6 ms |
16728 KB |
Output is correct |
13 |
Correct |
7 ms |
16988 KB |
Output is correct |
14 |
Correct |
6 ms |
16948 KB |
Output is correct |
15 |
Correct |
100 ms |
30284 KB |
Output is correct |
16 |
Correct |
139 ms |
29324 KB |
Output is correct |
17 |
Correct |
112 ms |
29268 KB |
Output is correct |
18 |
Correct |
111 ms |
29264 KB |
Output is correct |
19 |
Correct |
106 ms |
29364 KB |
Output is correct |
20 |
Correct |
107 ms |
29264 KB |
Output is correct |
21 |
Correct |
114 ms |
29248 KB |
Output is correct |
22 |
Correct |
106 ms |
29268 KB |
Output is correct |
23 |
Correct |
103 ms |
28868 KB |
Output is correct |
24 |
Correct |
103 ms |
30288 KB |
Output is correct |
25 |
Correct |
115 ms |
29268 KB |
Output is correct |
26 |
Correct |
141 ms |
29344 KB |
Output is correct |
27 |
Correct |
107 ms |
29312 KB |
Output is correct |
28 |
Correct |
111 ms |
29264 KB |
Output is correct |
29 |
Correct |
117 ms |
29268 KB |
Output is correct |
30 |
Correct |
108 ms |
29252 KB |
Output is correct |
31 |
Correct |
106 ms |
29268 KB |
Output is correct |
32 |
Correct |
107 ms |
28968 KB |
Output is correct |
33 |
Correct |
102 ms |
27732 KB |
Output is correct |
34 |
Correct |
85 ms |
27988 KB |
Output is correct |
35 |
Correct |
97 ms |
27984 KB |
Output is correct |
36 |
Correct |
86 ms |
27984 KB |
Output is correct |
37 |
Correct |
7 ms |
17240 KB |
Output is correct |
38 |
Correct |
7 ms |
17148 KB |
Output is correct |
39 |
Correct |
7 ms |
17084 KB |
Output is correct |
40 |
Correct |
7 ms |
17244 KB |
Output is correct |
41 |
Correct |
95 ms |
27792 KB |
Output is correct |
42 |
Correct |
100 ms |
27728 KB |
Output is correct |
43 |
Correct |
119 ms |
28368 KB |
Output is correct |
44 |
Correct |
108 ms |
28292 KB |
Output is correct |
45 |
Correct |
112 ms |
29264 KB |
Output is correct |
46 |
Correct |
113 ms |
29112 KB |
Output is correct |
47 |
Correct |
111 ms |
29088 KB |
Output is correct |
48 |
Correct |
88 ms |
27960 KB |
Output is correct |
49 |
Correct |
86 ms |
27984 KB |
Output is correct |
50 |
Correct |
7 ms |
17500 KB |
Output is correct |
51 |
Correct |
65 ms |
24084 KB |
Output is correct |
52 |
Correct |
58 ms |
22360 KB |
Output is correct |
53 |
Correct |
6 ms |
16984 KB |
Output is correct |
54 |
Correct |
538 ms |
73164 KB |
Output is correct |
55 |
Correct |
332 ms |
59848 KB |
Output is correct |
56 |
Correct |
325 ms |
58504 KB |
Output is correct |
57 |
Correct |
6 ms |
16732 KB |
Output is correct |
58 |
Correct |
6 ms |
16732 KB |
Output is correct |
59 |
Correct |
7 ms |
16732 KB |
Output is correct |
60 |
Correct |
92 ms |
27792 KB |
Output is correct |
61 |
Correct |
73 ms |
27732 KB |
Output is correct |
62 |
Correct |
140 ms |
48072 KB |
Output is correct |
63 |
Correct |
386 ms |
62412 KB |
Output is correct |
64 |
Correct |
72 ms |
45068 KB |
Output is correct |
65 |
Correct |
68 ms |
44884 KB |
Output is correct |
66 |
Correct |
66 ms |
44880 KB |
Output is correct |
67 |
Correct |
67 ms |
45068 KB |
Output is correct |
68 |
Correct |
5 ms |
16732 KB |
Output is correct |
69 |
Correct |
55 ms |
37160 KB |
Output is correct |
70 |
Correct |
54 ms |
37204 KB |
Output is correct |
71 |
Correct |
108 ms |
31916 KB |
Output is correct |
72 |
Correct |
617 ms |
85660 KB |
Output is correct |
73 |
Correct |
642 ms |
85232 KB |
Output is correct |
74 |
Correct |
665 ms |
85132 KB |
Output is correct |
75 |
Correct |
605 ms |
85800 KB |
Output is correct |
76 |
Correct |
614 ms |
85448 KB |
Output is correct |
77 |
Correct |
626 ms |
85992 KB |
Output is correct |
78 |
Correct |
616 ms |
85396 KB |
Output is correct |
79 |
Correct |
717 ms |
85192 KB |
Output is correct |
80 |
Correct |
70 ms |
44900 KB |
Output is correct |
81 |
Correct |
108 ms |
32076 KB |
Output is correct |
82 |
Correct |
637 ms |
85740 KB |
Output is correct |
83 |
Correct |
617 ms |
85896 KB |
Output is correct |
84 |
Correct |
619 ms |
85284 KB |
Output is correct |
85 |
Correct |
618 ms |
85496 KB |
Output is correct |
86 |
Correct |
610 ms |
85248 KB |
Output is correct |
87 |
Correct |
672 ms |
85196 KB |
Output is correct |
88 |
Correct |
617 ms |
85192 KB |
Output is correct |
89 |
Correct |
625 ms |
84940 KB |
Output is correct |
90 |
Correct |
73 ms |
44876 KB |
Output is correct |
91 |
Correct |
226 ms |
49552 KB |
Output is correct |
92 |
Correct |
225 ms |
49744 KB |
Output is correct |
93 |
Correct |
233 ms |
49916 KB |
Output is correct |
94 |
Correct |
228 ms |
49744 KB |
Output is correct |
95 |
Correct |
106 ms |
27720 KB |
Output is correct |
96 |
Correct |
102 ms |
27656 KB |
Output is correct |
97 |
Correct |
98 ms |
28244 KB |
Output is correct |
98 |
Correct |
96 ms |
28316 KB |
Output is correct |
99 |
Correct |
632 ms |
85620 KB |
Output is correct |
100 |
Correct |
647 ms |
85552 KB |
Output is correct |
101 |
Correct |
65 ms |
44884 KB |
Output is correct |
102 |
Correct |
618 ms |
84136 KB |
Output is correct |
103 |
Incorrect |
66 ms |
44880 KB |
Output isn't correct |
104 |
Halted |
0 ms |
0 KB |
- |