結果

問題 No.1266 7 Colors
ユーザー umezo
提出日時 2020-10-23 23:59:44
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 363 ms / 3,000 ms
コード長 1,765 bytes
コンパイル時間 5,570 ms
コンパイル使用メモリ 207,136 KB
最終ジャッジ日時 2025-01-15 14:28:07
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

class Dis{
  public:
  vector<ll> rank,p,siz;
  
  Dis(int s){
    rank.resize(s,0);
    p.resize(s,0);
    siz.resize(s,1);
    rep(i,s) makeSet(i);
  }
  
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
  }
  
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
  
  void unite(int x,int y){
    if(same(x,y)) return;
    link(findSet(x),findSet(y));
  }
  
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      siz[x]+=siz[y];
    }
    else{
      p[x]=y;
      siz[y]+=siz[x];
      if(rank[x]==rank[y]) rank[y]++;
    }
  }
  
  int findSet(int x){
    if(x != p[x]) p[x]=findSet(p[x]);
    return p[x];
  }
  
  int size(int x){
    return siz[findSet(x)];
  }
};

vector<int> G[100010];

int main(){
  ll n,m,q;
  cin>>n>>m>>q;
  
  Dis ds=Dis(7*n);
  
  vector<string> A(n);
  rep(i,n) cin>>A[i];
  
  rep(i,m){
    int x,y;
    cin>>x>>y;
    x--,y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  
  rep(i,n){
    rep(j,6){
      if(A[i][j]=='1' && A[i][j+1]=='1') ds.unite(7*i+j,7*i+j+1);
    }
    if(A[i][0]=='1' && A[i][6]=='1') ds.unite(7*i,7*i+6);
  }
  
  rep(i,n){
    rep(j,7){
      for(auto t:G[i]){
        if(A[i][j]=='1' && A[t][j]=='1') ds.unite(7*i+j,7*t+j);
      }
    }
  }
  
  rep(i,q){
    int c,x,y;
    cin>>c>>x>>y;
    x--,y--;
    if(c==1){
      A[x][y]='1';
      if(A[x][(y+1)%7]=='1') ds.unite(7*x+y,7*x+(y+1)%7);
      if(A[x][(y+6)%7]=='1') ds.unite(7*x+y,7*x+(y+6)%7);
      for(auto t:G[x]){
        if(A[t][y]=='1') ds.unite(7*t+y,7*x+y);
      }
    }
    if(c==2){
      cout<<ds.size(7*x)<<endl;
    }
  }

  return 0;
}
0