結果

問題 No.1266 7 Colors
ユーザー どららどらら
提出日時 2020-10-23 23:37:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 365 ms / 3,000 ms
コード長 2,217 bytes
コンパイル時間 1,881 ms
コンパイル使用メモリ 172,892 KB
実行使用メモリ 20,016 KB
最終ジャッジ日時 2023-09-28 18:53:01
合計ジャッジ時間 9,570 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
12,952 KB
testcase_01 AC 8 ms
13,144 KB
testcase_02 AC 9 ms
13,196 KB
testcase_03 AC 258 ms
15,404 KB
testcase_04 AC 341 ms
19,356 KB
testcase_05 AC 259 ms
15,396 KB
testcase_06 AC 345 ms
18,868 KB
testcase_07 AC 358 ms
18,752 KB
testcase_08 AC 339 ms
19,572 KB
testcase_09 AC 312 ms
16,984 KB
testcase_10 AC 306 ms
16,704 KB
testcase_11 AC 267 ms
15,884 KB
testcase_12 AC 286 ms
16,132 KB
testcase_13 AC 299 ms
16,416 KB
testcase_14 AC 262 ms
15,196 KB
testcase_15 AC 362 ms
18,756 KB
testcase_16 AC 298 ms
15,972 KB
testcase_17 AC 365 ms
18,476 KB
testcase_18 AC 279 ms
19,744 KB
testcase_19 AC 174 ms
19,528 KB
testcase_20 AC 173 ms
20,016 KB
testcase_21 AC 224 ms
14,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

class UnionFind{
  int n;
  vector<int> data, sz;
public:
  UnionFind(int _n):n(_n),data(_n,-1),sz(_n,1){}
  bool link(int a, int b){ //if new link, then true.
    int ra=find_root(a);
    int rb=find_root(b);
    if(ra!=rb){
      if(data[rb]<data[ra]) swap(ra,rb);
      data[ra]+=data[rb];
      data[rb]=ra;

      int sum = sz[ra] + sz[rb];
      sz[ra] = sum;
      sz[rb] = sum;
    }
    return (ra!=rb);
  }
  bool check(int a, int b){ //if same set, then true.
    return (find_root(a)==find_root(b));
  }
  int find_root(int a){
    return ((data[a]<0)?a:(data[a]=find_root(data[a])));
  }
  int size(int a){
    return sz[find_root(a)];
  }
};

int get_index(int c, int i){
  return c * 100005 + i;
}

UnionFind uf(1000005);
vector<int> G[100005];

int main(){
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<string> v;
  rep(i,N){
    string s;
    cin >> s;
    v.push_back(s);
    rep(j,s.size()){
      if(s[j] == '1' && s[(j+1)%(s.size())] == '1'){
        uf.link(get_index(j,i), get_index((j+1)%(s.size()),i));
      }
    }
  }
  rep(i,M){
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);

    rep(j,v[a].size()){
      if(v[a][j] == '1' && v[b][j] == '1'){
        uf.link(get_index(j,a), get_index(j,b));
      }
    }
  }

  rep(q,Q){
    int t, x, y;
    cin >> t >> x >> y;
    x--;
    y--;
    if(t == 1){
      v[x][y] = '1';
      if(v[x][(y-1+v[x].size())%(v[x].size())] == '1'){
        uf.link(get_index(y,x), get_index((y-1+v[x].size())%(v[x].size()),x));
      }
      if(v[x][(y+1)%(v[x].size())] == '1'){
        uf.link(get_index(y,x), get_index((y+1)%(v[x].size()),x));
      }
      rep(i,G[x].size()){
        int j = G[x][i];
        if(v[x][y] == '1' && v[j][y] == '1'){
          uf.link(get_index(y,x), get_index(y,j));
        }
      }
    }else{
      cout << uf.size(get_index(0,x)) << endl;
    }
  }
  
  
  return 0;
}
0