結果
| 問題 |
No.849 yuki国の分割統治
|
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2019-07-05 23:29:11 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,032 bytes |
| コンパイル時間 | 2,689 ms |
| コンパイル使用メモリ | 217,168 KB |
| 最終ジャッジ日時 | 2025-01-07 06:16:18 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 7 WA * 1 TLE * 18 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
struct UnionFind{
Int n,num;
vector<Int> r,p;
UnionFind(){}
UnionFind(Int sz):n(sz),num(sz),r(sz,1),p(sz,0){iota(p.begin(),p.end(),0);}
Int find(Int x){
return (x==p[x]?x:p[x]=find(p[x]));
}
bool same(Int x,Int y){
return find(x)==find(y);
}
void unite(Int x,Int y){
x=find(x);y=find(y);
if(x==y) return;
if(r[x]<r[y]) swap(x,y);
r[x]+=r[y];
p[y]=x;
num--;
}
Int size(Int x){
return r[find(x)];
}
Int count() const{
return num;
}
};
template<typename T>
vector<T> compress(vector<T> v){
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
return v;
}
template<typename T>
map<T, Int> dict(const vector<T> &v){
map<T, Int> res;
for(Int i=0;i<(Int)v.size();i++)
res[v[i]]=i;
return res;
}
//INSERT ABOVE HERE
signed main(){
Int a,b,c,d;
cin>>a>>b>>c>>d;
Int n;
cin>>n;
vector<Int> xs(n),ys(n);
for(Int i=0;i<n;i++) cin>>xs[i]>>ys[i];
UnionFind uf(n);
{
int gx=__gcd(a,c);
int gy=__gcd(b,d);
int dy=(gx==0?min(b,d):abs(b*(c/gx)-d*(a/gx)));
int dx=(gy==0?min(a,c):abs(a*(d/gy)-c*(b/gy)));
if(dy==0) dy=2e9;
if(dx==0) dx=2e9;
using P = pair<int, int>;
map<P, int> mp;
for(int i=0;i<n;i++){
if(mp.count(P(xs[i]%dx,ys[i]%dy)))
uf.unite(i,mp[P(xs[i]%dx,ys[i]%dy)]);
if(mp.count(P((xs[i]+a)%dx,(ys[i]+b)%dy)))
uf.unite(i,mp[P((xs[i]+a)%dx,(ys[i]+b)%dy)]);
if(mp.count(P((xs[i]+c)%dx,(ys[i]+d)%dy)))
uf.unite(i,mp[P((xs[i]+c)%dx,(ys[i]+d)%dy)]);
mp[P(xs[i]%dx,ys[i]%dy)]=i;
}
mp.clear();
for(int i=n-1;i>=0;i--){
if(mp.count(P(xs[i]%dx,ys[i]%dy)))
uf.unite(i,mp[P(xs[i]%dx,ys[i]%dy)]);
if(mp.count(P((xs[i]+a)%dx,(ys[i]+b)%dy)))
uf.unite(i,mp[P((xs[i]+a)%dx,(ys[i]+b)%dy)]);
if(mp.count(P((xs[i]+c)%dx,(ys[i]+d)%dy)))
uf.unite(i,mp[P((xs[i]+c)%dx,(ys[i]+d)%dy)]);
mp[P(xs[i]%dx,ys[i]%dy)]=i;
}
}
vector<Int> vs;
if((b*c-a*d)==0){
if(hypot<double>(a,b)>hypot<double>(c,d)) a=c,b=d;
for(Int i=0;i<n;i++){
for(Int v:vs){
Int x=xs[i]-xs[v];
Int y=ys[i]-ys[v];
Int p=a?x/a:y/b;
if(a*p==x&&b*p==y) uf.unite(i,v);
}
vector<Int> nx;
for(Int v:vs)
nx.emplace_back(uf.find(v));
nx.emplace_back(uf.find(i));
vs=compress(nx);
}
}else{
for(Int i=0;i<n;i++){
for(Int v:vs){
Int x=xs[i]-xs[v];
Int y=ys[i]-ys[v];
Int p=(c*y-d*x)/(b*c-a*d);
Int q=(a*y-b*x)/(a*d-b*c);
if(a*p+c*q==x&&b*p+d*q==y) uf.unite(i,v);
}
vector<Int> nx;
for(Int v:vs)
nx.emplace_back(uf.find(v));
nx.emplace_back(uf.find(i));
vs=compress(nx);
}
}
cout<<uf.count()<<endl;
return 0;
}
beet