QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#663055 | #4272. Phone Plans | SimonLJK | 0 | 216ms | 82236kb | C++14 | 1.9kb | 2024-10-21 12:25:03 | 2024-10-21 12:25:03 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+999;
int n,m[2];
ll k,cnt[N],now;
struct edge{
int u,v,w;
bool operator <(const edge&A) const{
return w<A.w;
}
}e[2][N];
vector<int> bel[N],mer[N];
unordered_map<int,int> mp[N];
void ins(int u,int col){
mp[u][0]++; mp[u][col]++;
now+=mp[u][0]-mp[u][col];
return;
}
void era(int u,int col){
mp[u][0]--; mp[u][col]--;
now-=mp[u][0]-mp[u][col];
}
int id[N],f[N],sz[N];
int find(int x){
if(id[x]==x) return x;
return id[x]=find(id[x]);
}
int main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
int u,v,w;
cin>>n>>m[0]>>m[1]>>k;
for(int i=1;i<=m[0];i++)
cin>>e[0][i].u>>e[0][i].v>>e[0][i].w;
sort(e[0]+1,e[0]+m[0]+1);
for(int i=1;i<=m[1];i++)
cin>>e[1][i].u>>e[1][i].v>>e[1][i].w;
sort(e[1]+1,e[1]+m[1]+1);
for(int i=1;i<=n;i++){
id[i]=i,sz[i]=1;
bel[i].push_back(i);
}
for(int i=1;i<=m[0];i++){
cnt[i]=cnt[i-1];
u=e[0][i].u; v=e[0][i].v;
u=find(u); v=find(v);
if(u==v) continue;
if(sz[u]<sz[v]) swap(u,v);
for(int j=0;j<sz[v];j++)
bel[u].push_back(bel[v][j]);
swap(mer[i],bel[v]);
cnt[i]+=1ll*sz[u]*sz[v];
id[v]=u; sz[u]+=sz[v];
}
for(int i=1;i<=n;i++){
f[i]=id[i];
id[i]=i; sz[i]=1;
bel[i].resize(1); bel[i][0]=i;
mp[i][0]=mp[i][f[i]]=1;
}
int ans=2e9+1;
for(int i=m[0],j=0;i>=0;i--){
while(cnt[i]+now<k){
if(j==m[1]) break;
j++; u=e[1][j].u; v=e[1][j].v;
u=find(u); v=find(v);
if(u==v) continue;
if(sz[u]<sz[v]) swap(u,v);
bel[u].resize(sz[u]+sz[v]);
for(int q=0;q<sz[v];q++){
bel[u].push_back(bel[v][q]);
era(v,f[bel[v][q]]); ins(u,f[bel[v][q]]);
}
id[v]=u; sz[u]+=sz[v];
}
ans=min(ans,e[0][i].w+e[1][j].w);
for(int j=0;j<mer[i].size();j++){
era(find(mer[i][j]),f[mer[i][j]]);
f[mer[i][j]]=mer[i][0];
ins(find(mer[i][j]),f[mer[i][j]]);
}
}
if(ans==2e9+1) cout<<-1;
else cout<<ans;
return 0;
}
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: 30168kb
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: 6
Accepted
time: 3ms
memory: 26384kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #3:
score: 0
Wrong Answer
time: 0ms
memory: 26420kb
input:
2 0 0 1
output:
0
result:
wrong answer 1st lines differ - expected: '-1', found: '0'
Subtask #2:
score: 0
Wrong Answer
Test #53:
score: 0
Wrong Answer
time: 216ms
memory: 82236kb
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:
508471502
result:
wrong answer 1st lines differ - expected: '502149991', found: '508471502'
Subtask #3:
score: 0
Wrong Answer
Test #103:
score: 6
Accepted
time: 4ms
memory: 26416kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #104:
score: 0
Wrong Answer
time: 0ms
memory: 28148kb
input:
2 0 0 1
output:
0
result:
wrong answer 1st lines differ - expected: '-1', found: '0'
Subtask #4:
score: 0
Skipped
Dependency #1:
0%