This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define vt vector
#define size(x) (int((x).size()))
#define all(x) begin(x), end(x)
#define REP(a,b,c,d) for(int a=(b);(d)>0?a<=(c):a>=(c);a+=(d))
#define FOR(a,b,c) REP(a,b,c,1)
#define ROF(a,b,c) REP(a,b,c,-1)
struct DSU {
vt<int> uf;
DSU(int n):uf(vt<int>(n,-1)){}
int find(int i){
return uf[i]<0?i:uf[i]=find(uf[i]);
}
bool unite(int a,int b){
if((a=find(a))==(b=find(b)))
return false;
if(uf[a]>uf[b])
swap(a,b);
uf[a]+=uf[b];
uf[b]=a;
return false;
}
int sz(int i){
return -uf[find(i)];
}
};
signed main() {
#ifndef DEBUG
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int N,M,D;
cin>>N>>M>>D;
vt<bool> x_need(D),y_need(D);
FOR(i,0,N-1){
int x,y;
cin>>x>>y;
x_need[x]=y_need[y]=true;
}
vt<int> R(D,-1);
vt<vt<int>> change(D);
FOR(i,0,M-1){
int x,y;
cin>>x>>y;
if(!y_need[y]){
R[y]=max(R[y],x);
change[x].push_back(y);
}
}
int ans=D*D;
FOR(l,0,D-1){
vt<vt<int>> can_kill(D);
vt<bool> deleted(D);
DSU uf(D);
int gap=0;
auto insert=[&](int i){
deleted[i]=true;
for(int k:{-1,1}){
int j=(i+k+D)%D;
if(deleted[j])
uf.unite(i,j);
}
gap=max(gap,uf.sz(i));
};
FOR(i,0,D-1){
if (R[i]>=0)
can_kill[R[i]].push_back(i);
else if(!y_need[i])
insert(i);
}
int need=accumulate(all(x_need),0);
FOR(d,1,D){
int r=(l+d-1)%D;
need-=x_need[r];
for(int i:can_kill[r])
insert(i);
if(!need)
ans=min(ans,d*(D-gap));
}
for(int i:change[l])
R[i]=l;
}
cout<<ans<<'\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |