結果

問題 No.1266 7 Colors
ユーザー snow39snow39
提出日時 2020-10-23 22:43:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 3,000 ms
コード長 2,259 bytes
コンパイル時間 1,071 ms
コンパイル使用メモリ 101,532 KB
実行使用メモリ 20,140 KB
最終ジャッジ日時 2023-09-28 16:54:09
合計ジャッジ時間 5,212 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 86 ms
6,552 KB
testcase_04 AC 165 ms
14,996 KB
testcase_05 AC 92 ms
7,012 KB
testcase_06 AC 172 ms
16,256 KB
testcase_07 AC 179 ms
18,388 KB
testcase_08 AC 169 ms
15,212 KB
testcase_09 AC 147 ms
12,292 KB
testcase_10 AC 131 ms
11,568 KB
testcase_11 AC 101 ms
8,068 KB
testcase_12 AC 109 ms
9,124 KB
testcase_13 AC 119 ms
10,224 KB
testcase_14 AC 91 ms
7,008 KB
testcase_15 AC 173 ms
18,484 KB
testcase_16 AC 113 ms
9,368 KB
testcase_17 AC 176 ms
17,776 KB
testcase_18 AC 79 ms
20,140 KB
testcase_19 AC 79 ms
19,880 KB
testcase_20 AC 79 ms
19,804 KB
testcase_21 AC 42 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

class DisjointSet{
public:
  int N;
  vector<int> rank,p;
  vector<int> sz;
 
  DisjointSet(){}
  DisjointSet(int n):N(n),rank(n),p(n),sz(n){
    for(int i=0;i<N;i++) makeSet(i);
  }
 
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
    sz[x]=1;
  }
 
  bool same(int x,int y){
    return leader(x)==leader(y);
  }
 
  void unite(int x,int y){
    if(same(x,y)) return;
    link(leader(x),leader(y));
  }
 
  void link(int x,int y){
    if(rank[x]>rank[y]) swap(x,y);

    p[x]=y;
    sz[y]+=sz[x];
    if(rank[x]==rank[y]){
      ++rank[y];
    }
  }
 
  int leader(int x){
    if(x!=p[x]) p[x]=leader(p[x]);
    return p[x];
  }
 
  int size(int x){
    return sz[leader(x)];
  }
};

const int L=7;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N,M,Q;
  cin>>N>>M>>Q;
  vector<string> S(N);
  rep(i,N) cin>>S[i];
  vector<vector<int>> g(N);
  rep(i,M){
    int a,b;
    cin>>a>>b;
    a--;b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  auto trans = [&](int a,int b){
    return a*L+b;
  };

  DisjointSet us(L*N);
  rep(i,N){
    for(int j=0;j<L;j++){
      if(S[i][j]=='1'&&S[i][(j+1)%L]=='1') us.unite(trans(i,j),trans(i,(j+1)%L));
    }
  }
  rep(i,N){
    for(auto nex:g[i]){
      rep(j,L){
        if(S[i][j]=='1'&&S[nex][j]=='1') us.unite(trans(i,j),trans(nex,j));
      }
    }
  }

  rep(q,Q){
    int t,x,y;
    cin>>t>>x>>y;

    if(t==1){
      x--;y--;
      S[x][y]='1';
      if(S[x][(y+1)%L]=='1') us.unite(trans(x,y),trans(x,(y+1)%L));
      if(S[x][(y-1+L)%L]=='1') us.unite(trans(x,y),trans(x,(y-1+L)%L));
      for(auto nex:g[x]){
        if(S[x][y]=='1'&&S[nex][y]=='1') us.unite(trans(x,y),trans(nex,y));
      }
    }else{
      x--;
      int ans=us.size(trans(x,0));
      cout<<ans<<"\n";
    }
  }

  return 0;
}
0