結果

問題 No.1266 7 Colors
ユーザー momoharamomohara
提出日時 2020-06-04 22:43:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 144 ms / 3,000 ms
コード長 1,780 bytes
コンパイル時間 1,700 ms
コンパイル使用メモリ 172,440 KB
実行使用メモリ 14,848 KB
最終ジャッジ日時 2023-09-27 23:25:53
合計ジャッジ時間 7,933 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 73 ms
5,696 KB
testcase_04 AC 115 ms
10,932 KB
testcase_05 AC 79 ms
6,272 KB
testcase_06 AC 125 ms
11,960 KB
testcase_07 AC 133 ms
13,140 KB
testcase_08 AC 113 ms
11,196 KB
testcase_09 AC 102 ms
9,400 KB
testcase_10 AC 97 ms
8,856 KB
testcase_11 AC 84 ms
6,752 KB
testcase_12 AC 89 ms
7,504 KB
testcase_13 AC 92 ms
8,064 KB
testcase_14 AC 79 ms
5,944 KB
testcase_15 AC 144 ms
13,408 KB
testcase_16 AC 90 ms
7,584 KB
testcase_17 AC 136 ms
12,740 KB
testcase_18 AC 69 ms
14,848 KB
testcase_19 AC 70 ms
14,284 KB
testcase_20 AC 71 ms
14,368 KB
testcase_21 AC 41 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;

#define rep(i,n,m) for(ll (i)=(n);(i)<(m);i++)
#define REP(i,n) rep(i,0,n)
#define en "\n"

template <class T> using vec=vector<T>;
template <class T> using vvec=vec<vec<T>>;

class UnionFind {
 vector<int> data;
 int num;
public:
 UnionFind(int size) : data(size, -1), num(size) { }
 bool unionSet(int x, int y) {
  x = root(x); y = root(y);
  if (x != y) {
   if (data[y] < data[x]) swap(x, y);
   data[x] += data[y]; data[y] = x;
   num--;
  }
  return x != y;
 }
 bool findSet(int x, int y) {
  return root(x) == root(y);
 }
 int root(int x) {
  return data[x] < 0 ? x : data[x] = root(data[x]);
 }
 int size(int x) {
  return -data[root(x)];
 }
 int numSet() {
  return num;
 }
};

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

    ll n,m,q;
    cin>>n>>m>>q;
    vec<string> col(n);
    vvec<int> g(n);

    UnionFind uni(n*7);
    REP(i,n){
        cin>>col[i];
        REP(j,7){
            if(col[i][j]=='1'&&col[i][(j+1)%7]=='1'){
                uni.unionSet(i*7+j,i*7+(j+1)%7);
            }
        }
    }

    REP(i,m){
        ll a,b;
        cin>>a>>b;
        a--;b--;
        g[a].push_back(b);
        g[b].push_back(a);
        REP(j,7) if(col[a][j]=='1' && col[b][j]=='1')uni.unionSet(a*7+j,b*7+j);
    }

    REP(i,q){
        ll t,x,y;
        cin>>t>>x>>y;
        x--;y--;
        if(t==1){
            col[x][y]='1';
            if(col[x][(y+6)%7]=='1') uni.unionSet(x*7+y,x*7+(y+6)%7);
            if(col[x][(y+1)%7]=='1') uni.unionSet(x*7+y,x*7+(y+1)%7);
            for(auto to:g[x]){
                if(col[to][y]=='1') uni.unionSet(x*7+y,to*7+y);
            }
        }else{
            cout<<uni.size(x*7)<<en;
        }
    }

    return 0;
}
0