QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#134171#4272. Phone Plansblackyuki0 2ms3560kbC++144.5kb2023-08-03 11:46:202023-08-03 11:46:21

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-03 11:46:21]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:3560kb
  • [2023-08-03 11:46:20]
  • 提交

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[a]=b;
        }
    }
    vi cntA(n),cntB(n);
    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: 3460kb

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: 3444kb

input:

1 0 0 0

output:

0

result:

ok single line: '0'

Test #3:

score: -6
Wrong Answer
time: 1ms
memory: 3528kb

input:

2 0 0 1

output:

1001001001001001001

result:

wrong answer 1st lines differ - expected: '-1', found: '1001001001001001001'

Subtask #2:

score: 0
Dangerous Syscalls

Test #53:

score: 0
Dangerous Syscalls

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:


result:


Subtask #3:

score: 0
Wrong Answer

Test #103:

score: 6
Accepted
time: 0ms
memory: 3560kb

input:

1 0 0 0

output:

0

result:

ok single line: '0'

Test #104:

score: -6
Wrong Answer
time: 2ms
memory: 3560kb

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%