結果

問題 No.849 yuki国の分割統治
ユーザー beetbeet
提出日時 2019-07-05 23:28:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,998 bytes
コンパイル時間 2,701 ms
コンパイル使用メモリ 219,220 KB
実行使用メモリ 19,616 KB
最終ジャッジ日時 2024-04-16 08:09:58
合計ジャッジ時間 6,551 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,756 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,948 KB
testcase_06 WA -
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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(a>c) 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;
}
0