Submission #693962

#TimeUsernameProblemLanguageResultExecution timeMemory
693962safaricolaPhone Plans (CCO22_day2problem2)C++17
0 / 25
631 ms34480 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> ii; #define eb emplace_back #define pb push_back #define mp make_pair #define f first #define s second #define rep(i,n) for(int i = 1; i <= n; i++) ll n,na,nb,k,w,sum,p[200010],ngs[200010]; pair<ll,ii> d[200010], dd[200010]; vector<int> adj[200010],adjl[200010]; set<int> s,ss; map<int,ll> m,mm; int find(int x){ int ind= *upper_bound(s.begin(), s.end(), x); return m[ind]; } int par(int x) { return (p[x]==x)? x:p[x]=par(p[x]); } //p[par(a)] = par(b) bool check(int L){ int bb=nb; ll ma=0; rep(i, na){ if(L<d[i].f){ break; } while(dd[bb].f>L-m[d[i].f]){ // dd is the level bb--; } //cout<<i<<' '<<dd[bb].f<<' '<<mm[dd[bb].f]<<' '<<d[i].f<<' '<<m[d[i].f]<<endl; ma=max(mm[dd[bb].f]+m[d[i].f], ma); } //cout<<L<<' '<<ma<<endl; return ma<k; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin>>n>>na>>nb>>k; rep(i,n) p[i] = i; rep(i,n) ngs[i]=1; rep(i,na){ cin>>d[i].s.f>>d[i].s.s>>d[i].f; } sort(d+1, d+na+1); rep(i,na){ //cout<<d[i].f<<' '; if(par(d[i].s.s)==par(d[i].s.f)){ m[d[i].f]=m[d[i-1].f]; continue; } int init=ngs[par(d[i].s.s)]*(ngs[par(d[i].s.s)]-1)/2+ngs[par(d[i].s.f)]*(ngs[par(d[i].s.f)]-1)/2; int nogrps= ngs[par(d[i].s.s)]+ngs[par(d[i].s.f)]; p[par(d[i].s.s)] = par(d[i].s.f); ngs[par(d[i].s.s)] = nogrps; sum+=nogrps*(nogrps-1)/2-init; m[d[i].f]=sum; //cout<<sum<<endl; } rep(i,nb){ cin>>dd[i].s.f>>dd[i].s.s>>dd[i].f; } rep(i,n) p[i] = i; rep(i,n) ngs[i]=1; sort(dd+1, dd+nb+1); sum=0; rep(i,nb){ //cout<<dd[i].f<<' '; if(par(dd[i].s.s)==par(dd[i].s.f)){ mm[dd[i].f]=mm[dd[i-1].f]; continue; } int init=ngs[par(dd[i].s.s)]*(ngs[par(dd[i].s.s)]-1)/2+ngs[par(dd[i].s.f)]*(ngs[par(dd[i].s.f)]-1)/2; int nogrps= ngs[par(dd[i].s.s)]+ngs[par(dd[i].s.f)]; p[par(dd[i].s.s)] = par(dd[i].s.f); ngs[par(dd[i].s.s)] = nogrps; sum+=nogrps*(nogrps-1)/2-init; mm[dd[i].f]=sum; //cout<<sum<<endl; } int maxn=d[na].f+dd[nb].f; int s=-1, e=maxn, mid; while(mid=(e+s)/2, e-s>1){ if(check(mid)) s=mid; else e=mid; } cout<<e; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...