QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#149180 | #4272. Phone Plans | Myrrh | 0 | 200ms | 65912kb | C++14 | 3.2kb | 2023-08-24 08:00:39 | 2023-08-24 08:00:41 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAXN=2e5+50;
int N,M1,M2;
long long K;
struct Edge
{
int x,y,Len;
}e1[MAXN],e2[MAXN];
bool cmp(Edge a,Edge b)
{
return a.Len<b.Len;
}
/*
Mp[i][j]:同时在1的i子树和2的j子树的点数量
当一个点在另外一个图中跟Mp[i][j]个点联通,那么在这个图中就应该去掉这些贡献
合并:按秩合并
分裂:记录合并时候的Size
每次弹出最后一个
最后要reverse
*/
map<int,int>Mp[MAXN];
vector<int>Vec1[MAXN],Vec2[MAXN];
int father1[MAXN],father2[MAXN];
int Last[MAXN],Root[MAXN],Size[MAXN];
bool Use[MAXN];
long long Merge(int i)
{
int fx=father1[e1[i].x],fy=father1[e1[i].y];
if(fx==fy)
{
return 0;
}
if(Vec1[fx].size()<Vec1[fy].size())
{
swap(fx,fy);
}
long long Now=1ll*Vec1[fx].size()*Vec1[fy].size();
for(int j=0;j<Vec1[fy].size();j++)
{
int v=Vec1[fy][j];
Now-=Mp[fx][father2[v]];
}
for(int j=0;j<Vec1[fy].size();j++)
{
int v=Vec1[fy][j];
Vec1[fx].push_back(v);
father1[v]=fx;
Mp[fy][father2[v]]--;
Mp[fx][father2[v]]++;
}
return Now;
}
long long Split(int i)
{
if(Root[i]==0)
return 0;
int i1=Root[i],i2=Last[i];
long long Now=1ll*(Vec2[i1].size()-Vec2[i2].size())*Vec2[i2].size();
for(int j=0;j<Vec2[i2].size();j++)
{
int v=Vec2[i2][j];
Mp[father1[v]][i1]--;
Mp[father1[v]][i2]++;
father2[v]=i2;
}
for(int j=0;j<Vec2[i2].size();j++)
{
int v=Vec2[i2][j];
Now-=Mp[father1[v]][i1];
}
return Now;
}
int main()
{
scanf("%d%d%d%lld",&N,&M1,&M2,&K);
for(int i=1;i<=M1;i++)
{
scanf("%d%d%d",&e1[i].x,&e1[i].y,&e1[i].Len);
}
for(int i=1;i<=M2;i++)
{
scanf("%d%d%d",&e2[i].x,&e2[i].y,&e2[i].Len);
}
sort(e1+1,e1+M1+1,cmp);
sort(e2+1,e2+M2+1,cmp);
for(int i=1;i<=N;i++)
{
father2[i]=i;
Vec2[i].push_back(i);
}
for(int i=1;i<=M2;i++)
{
int fx=father2[e2[i].x],fy=father2[e2[i].y];
if(fx==fy)
{
Root[i]=0;
continue;
}
if(Vec2[fx].size()<Vec2[fy].size())
{
swap(fx,fy);
}
Root[i]=fx;
Last[i]=fy;
Size[i]=Vec2[fy].size();
for(int j=0;j<Vec2[fy].size();j++)
{
Vec2[fx].push_back(Vec2[fy][j]);
father2[Vec2[fy][j]]=fx;
}
}
long long Cnt=0;
for(int i=1;i<=N;i++)
{
Mp[i][father2[i]]++;
father1[i]=i;
Vec1[i].push_back(i);
if(Use[father2[i]]==false)
{
Use[father2[i]]=true;
Cnt+=1ll*Vec2[father2[i]].size()*(Vec2[father2[i]].size()-1)/2;
}
}
int r=M2;
int ans=2e9+1;
if(Cnt>=K)
{
ans=e2[M2].Len;
}
int Now=Split(r);
for(int i=1;i<=M1;i++)
{
Cnt+=Merge(i);
while(Cnt-Now>=K)
{
Cnt-=Now;
r--;
Now=Split(r);
}
if(Cnt>=K)
{
ans=min(ans,e1[i].Len+e2[r].Len);
}
}
if(ans==2e9+1)
{
puts("-1");
}
else
{
printf("%d",ans);
}
}
/*
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* i and j are misplaced
* variable misuse
* do smth instead of nothing and stay organized
* vector cause MLE or TLE
* wrong file name
* DON'T SLEEP IN THE EXAM
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
* DON'T GET A SWELLED HEAD
* YOU MUSTN'T HAVE EMOTION
->be based on Benq's code
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 6
Accepted
time: 1ms
memory: 22440kb
input:
6 4 4 9 1 2 1 2 3 2 1 4 3 3 4 4 5 6 40 1 5 30 2 6 20 3 6 10
output:
33
result:
ok single line: '33'
Test #2:
score: 0
Accepted
time: 1ms
memory: 22612kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 1ms
memory: 22224kb
input:
2 0 0 1
output:
-1
result:
ok single line: '-1'
Test #4:
score: -6
Runtime Error
input:
2 10 10 1 1 1 915886339 2 2 430624192 1 1 879808770 1 2 577221915 1 1 439429731 1 2 304911826 1 1 148009333 1 2 51122687 1 1 558282772 1 2 421196385 2 1 436692145 1 2 654020563 2 2 162573477 2 2 989531779 1 1 646687051 2 2 549037477 2 2 699532275 1 1 679375858 2 1 83352965 2 1 415698228
output:
result:
Subtask #2:
score: 0
Wrong Answer
Test #53:
score: 0
Wrong Answer
time: 200ms
memory: 65912kb
input:
200000 100000 100000 7628995 23677 113459 839167193 165893 15365 669621527 26287 109671 214795757 156871 136723 522277985 9957 100463 806718116 104783 161385 156110975 184577 92225 545172629 57373 130083 980035338 167231 49597 919886451 115601 24325 717233004 99413 141311 737488449 83437 62759 76873...
output:
634185692
result:
wrong answer 1st lines differ - expected: '502149991', found: '634185692'
Subtask #3:
score: 0
Runtime Error
Test #103:
score: 6
Accepted
time: 3ms
memory: 22436kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #104:
score: 0
Accepted
time: 1ms
memory: 22380kb
input:
2 0 0 1
output:
-1
result:
ok single line: '-1'
Test #105:
score: -6
Runtime Error
input:
2 10 200000 1 2 1 319832429 1 1 412617159 2 1 337734185 1 2 674652880 1 2 454610992 2 2 688717944 1 1 189208056 2 2 951221780 1 2 594191431 2 2 87592160 1 2 833491749 2 2 732961971 2 1 575969648 2 2 268359887 2 1 436382098 1 2 514959278 1 2 305446083 1 2 365989813 1 2 296840111 1 1 541558213 1 1 497...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%