QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#149189 | #4272. Phone Plans | Myrrh | 0 | 196ms | 84648kb | C++14 | 3.4kb | 2023-08-24 08:14:45 | 2023-08-24 08:14:45 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAXN=4e5+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+2;
if(Cnt>=K)
{
ans=e2[M2].Len;
}
int Now=Split(r);
while(r&&Cnt-Now>=K)
{
Cnt-=Now;
r--;
if(r)
Now=Split(r);
}
if(Cnt>=K)
ans=min(ans,e2[r].Len);
for(int i=1;i<=M1;i++)
{
Cnt+=Merge(i);
while(r&&Cnt-Now>=K)
{
Cnt-=Now;
r--;
//cout<<r<<endl;
if(r)
Now=Split(r);
}
if(Cnt>=K)
{
ans=min(ans,e1[i].Len+e2[r].Len);
}
}
if(ans==2e9+2)
{
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
Wrong Answer
Test #1:
score: 6
Accepted
time: 4ms
memory: 41196kb
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: 8ms
memory: 41232kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Accepted
time: 4ms
memory: 40992kb
input:
2 0 0 1
output:
-1
result:
ok single line: '-1'
Test #4:
score: 0
Accepted
time: 4ms
memory: 41192kb
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:
51122687
result:
ok single line: '51122687'
Test #5:
score: 0
Accepted
time: 7ms
memory: 41176kb
input:
2 10 10 1 1 1 1000000000 1 2 1000000000 2 2 1000000000 2 1 1000000000 1 2 1000000000 1 1 1000000000 1 2 1000000000 2 2 1000000000 1 2 1000000000 1 2 1000000000 2 1 1000000000 1 2 1000000000 2 1 1000000000 2 2 1000000000 1 2 1000000000 2 2 1000000000 1 1 1000000000 1 1 1000000000 2 2 1000000000 1 2 1...
output:
1000000000
result:
ok single line: '1000000000'
Test #6:
score: -6
Wrong Answer
time: 38ms
memory: 44696kb
input:
2000 0 200000 1199833 636 1231 120395621 689 1640 497332138 1861 1980 798839749 560 1283 560726905 1332 328 431171189 1203 1764 466367210 1102 347 317109768 1462 789 761470540 350 1093 551905741 1718 1047 548650524 51 546 56733461 58 1519 744207013 826 956 943929583 1969 207 238061756 99 47 99683567...
output:
10312605
result:
wrong answer 1st lines differ - expected: '9768257', found: '10312605'
Subtask #2:
score: 0
Wrong Answer
Test #53:
score: 0
Wrong Answer
time: 196ms
memory: 84648kb
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:
634185691
result:
wrong answer 1st lines differ - expected: '502149991', found: '634185691'
Subtask #3:
score: 0
Wrong Answer
Test #103:
score: 6
Accepted
time: 12ms
memory: 41272kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #104:
score: 0
Accepted
time: 4ms
memory: 40956kb
input:
2 0 0 1
output:
-1
result:
ok single line: '-1'
Test #105:
score: 0
Accepted
time: 48ms
memory: 44332kb
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:
10104
result:
ok single line: '10104'
Test #106:
score: 0
Accepted
time: 45ms
memory: 44552kb
input:
2 10 200000 1 1 1 1000000000 1 1 1000000000 1 2 1000000000 1 2 1000000000 1 2 1000000000 1 1 1000000000 2 1 1000000000 2 2 1000000000 2 2 1000000000 2 2 1000000000 1 1 1000000000 1 2 1000000000 1 1 1000000000 2 1 1000000000 1 1 1000000000 1 1 1000000000 2 1 1000000000 1 1 1000000000 2 2 1000000000 2...
output:
1000000000
result:
ok single line: '1000000000'
Test #107:
score: -6
Wrong Answer
time: 121ms
memory: 74812kb
input:
200000 10 200000 17900 199675 76237 290240030 50211 6922 761990536 92097 120746 607490 192856 130409 616541101 50427 80049 330957286 129588 180294 466199684 8674 110653 155297749 91380 156344 798960399 102127 40858 801752583 94673 105892 152356242 185676 6183 263664373 169026 112948 812501808 93517 ...
output:
643142599
result:
wrong answer 1st lines differ - expected: '75425485', found: '643142599'
Subtask #4:
score: 0
Skipped
Dependency #1:
0%