QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#134173 | #4272. Phone Plans | blackyuki | 0 | 571ms | 55224kb | C++14 | 4.5kb | 2023-08-03 11:49:15 | 2023-08-03 11:49:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (ll)(lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (ll)(upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
#define popcnt __builtin_popcountll
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
int main(){
ll n,K;vi m(2);cin>>n>>m[0]>>m[1]>>K;
if(K==0)dame(0);
vvi cost(2);
vvp nex(2);
vvvi con(2);
rep(tt,2){
vector<PP> edge(m[tt]);
rep(i,m[tt]){
ll a,b,c;cin>>a>>b>>c;
edge[i]=PP(c,a-1,b-1);
}
sort(all(edge));
vi par(n);rep(i,n)par[i]=i;
vvi gr(n);rep(i,n)gr[i].pb(i);
for(auto x:edge){
ll t,a,b;tie(t,a,b)=x;
if(par[a]==par[b])continue;
cost[tt].pb(t);
a=par[a];b=par[b];
if(gr[a].size()>gr[b].size())swap(a,b);
nex[tt].pb(a,b);
con[tt].pb(gr[a]);
for(ll x:gr[a])gr[b].pb(x);
for(ll x:gr[a])par[x]=b;
}
}
vi cntA(n,1),cntB(n,1);
map<P,ll> cntC;
rep(i,n)cntC[P(i,i)]=1;
ll a=0,b=0,c=0;
vvi par(2,vi(n));rep(i,2)rep(j,n)par[i][j]=j;
auto merge=[&](ll tt,ll i){
for(ll x:con[tt][i]){
ll A=par[0][x],B=par[1][x];
cntA[A]--;cntB[B]--;cntC[P(A,B)]--;
a-=cntA[A];b-=cntB[B];c-=cntC[P(A,B)];
if(tt==0)assert(A==nex[tt][i].fi);
else assert(B==nex[tt][i].fi);
par[tt][x]=nex[tt][i].se;
A=par[0][x];B=par[1][x];
a+=cntA[A];b+=cntB[B];c+=cntC[P(A,B)];
cntA[A]++;cntB[B]++;cntC[P(A,B)]++;
}
};
auto unmerge=[&](ll tt,ll i){
for(ll x:con[tt][i]){
ll A=par[0][x],B=par[1][x];
cntA[A]--;cntB[B]--;cntC[P(A,B)]--;
a-=cntA[A];b-=cntB[B];c-=cntC[P(A,B)];
if(tt==0)assert(A==nex[tt][i].se);
else assert(B==nex[tt][i].se);
par[tt][x]=nex[tt][i].fi;
A=par[0][x];B=par[1][x];
a+=cntA[A];b+=cntB[B];c+=cntC[P(A,B)];
cntA[A]++;cntB[B]++;cntC[P(A,B)]++;
}
};
ll l=0;
while(l<cost[1].size()&&a+b-c<K){
merge(1,l);l++;
}
ll ans=inf;
if(a+b-c>=K)ans=cost[1][l-1];
rep(i,cost[0].size()){
merge(0,i);
while(a+b-c>=K&&l>0){
unmerge(1,l-1);
l--;
}
if(l<cost[1].size()){
chmin(ans,cost[0][i]+cost[1][l]);
if(a+b-c>=K)chmin(ans,cost[0][i]);
}
}
out(ans);
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 6
Accepted
time: 1ms
memory: 3448kb
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: 0ms
memory: 3480kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #3:
score: -6
Wrong Answer
time: 1ms
memory: 3484kb
input:
2 0 0 1
output:
1001001001001001001
result:
wrong answer 1st lines differ - expected: '-1', found: '1001001001001001001'
Subtask #2:
score: 0
Wrong Answer
Test #53:
score: 5
Accepted
time: 571ms
memory: 55224kb
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:
502149991
result:
ok single line: '502149991'
Test #54:
score: 0
Accepted
time: 376ms
memory: 44328kb
input:
200000 200000 87 2694 197233 86229 181875035 85167 196363 328068482 177795 65479 693403443 119609 181977 588691030 115815 139981 486110961 92473 20483 199129328 166989 95385 210368646 98095 54673 357307451 122543 94377 907487846 46611 80735 71787832 158893 117071 521262491 92051 104395 365725125 142...
output:
11965880
result:
ok single line: '11965880'
Test #55:
score: 0
Accepted
time: 164ms
memory: 35008kb
input:
200000 103 199999 1593 75203 150269 64 68675 175215 100 176335 11837 94 33623 63279 56 16617 86741 63 167219 52783 58 6575 134399 1 144065 114171 2 32625 99459 50 35311 152509 36 132975 12211 8 175275 107903 23 17477 21319 95 157759 66683 71 34577 78339 26 154003 26703 18 187863 90945 74 116071 1089...
output:
7037526
result:
ok single line: '7037526'
Test #56:
score: 0
Accepted
time: 1ms
memory: 3488kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #57:
score: 0
Accepted
time: 1ms
memory: 3464kb
input:
1 1 0 0 1 1 1
output:
0
result:
ok single line: '0'
Test #58:
score: -5
Wrong Answer
time: 1ms
memory: 3448kb
input:
2 0 0 1
output:
1001001001001001001
result:
wrong answer 1st lines differ - expected: '-1', found: '1001001001001001001'
Subtask #3:
score: 0
Wrong Answer
Test #103:
score: 6
Accepted
time: 1ms
memory: 3464kb
input:
1 0 0 0
output:
0
result:
ok single line: '0'
Test #104:
score: -6
Wrong Answer
time: 1ms
memory: 3520kb
input:
2 0 0 1
output:
1001001001001001001
result:
wrong answer 1st lines differ - expected: '-1', found: '1001001001001001001'
Subtask #4:
score: 0
Skipped
Dependency #1:
0%