結果

問題 No.1266 7 Colors
ユーザー kkktymkkktym
提出日時 2020-10-23 23:10:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 346 ms / 3,000 ms
コード長 2,758 bytes
コンパイル時間 1,142 ms
コンパイル使用メモリ 108,000 KB
実行使用メモリ 20,968 KB
最終ジャッジ日時 2023-09-28 17:50:57
合計ジャッジ時間 8,376 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 246 ms
6,996 KB
testcase_04 AC 322 ms
15,676 KB
testcase_05 AC 252 ms
7,800 KB
testcase_06 AC 329 ms
16,848 KB
testcase_07 AC 343 ms
18,800 KB
testcase_08 AC 323 ms
15,944 KB
testcase_09 AC 304 ms
13,356 KB
testcase_10 AC 292 ms
12,004 KB
testcase_11 AC 264 ms
8,992 KB
testcase_12 AC 272 ms
9,916 KB
testcase_13 AC 286 ms
10,924 KB
testcase_14 AC 252 ms
7,748 KB
testcase_15 AC 346 ms
19,664 KB
testcase_16 AC 278 ms
10,116 KB
testcase_17 AC 346 ms
18,608 KB
testcase_18 AC 276 ms
20,968 KB
testcase_19 AC 165 ms
20,696 KB
testcase_20 AC 166 ms
20,548 KB
testcase_21 AC 226 ms
5,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }
template<class T> inline int  sz(T &a) { return a.size(); }
using ll = long long; using ld = long double;
using pi = pair<int,int>; using pl = pair<ll,ll>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
const int inf = numeric_limits<int>::max();
const ll infll = numeric_limits<ll>::max();
struct UFT{
  vector<int> par;//親
  vector<int> rank;//木の深さ
  vector<int> size;//木の大きさ
  int n;
  
  UFT(int _n)
  {
    n = _n;
    par.resize(n);
    rank.assign(n,0);
    size.assign(n,1);
    rep(i,n){
      par[i] = i;
    }
  }
  
  //xの根を返す
  int find(int x)
  {
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
  }

  //x,yを併合
  void unite(int x,int y)
  {
    x = find(x);
    y = find(y);
    if(x == y) return;

    if(rank[x] < rank[y]){
      par[x] = y;
      size[y] += size[x];
    }
    else{
      par[y] = x;
      size[x] += size[y];
      if(rank[x] == rank[y]) rank[x]++;
    }
  }

  //x,yが同じグループにいるかどうかを返す
  bool same(int x,int y)
  {
    return find(x) == find(y);
  }

  //xの属する木のサイズを探す
  int usize(int x)
  {
    return size[find(x)];
  }
};


int main()
{
  int n,m,q; cin >> n >> m >> q;
  vector<string> s(n);
  rep(i,n) cin >> s[i];

  vi u(m),v(m);
  vvi e(n);
  rep(i,m) {
    cin >> u[i] >> v[i];
    u[i]--; v[i]--;
    e[u[i]].push_back(v[i]);
    e[v[i]].push_back(u[i]);    
  }

  UFT uf(n*7);
  rep(i,n) {
    rep(j,7) {
      int k = (j+1) % 7;
      if(s[i][j] == '1' && s[i][k] == '1') {
	uf.unite(i*7 + j, i*7 + k);
      }
    }
  }

  rep(i,n) {
    for(auto w: e[i]) {
      rep(j,7) {
	if(s[i][j] == '1' && s[w][j] == '1') {
	  uf.unite(i*7 + j, w*7 + j);
	}
      }
    }
  }

  while(q-- > 0) {
    int t,x,y; cin >> t >> x >> y;
    if(t == 1) {
      s[x-1][y-1] = '1';
      int ny = (y - 2 + 7) % 7;
      int nyy = y % 7;
      if(s[x-1][ny] == '1') uf.unite((x-1)*7 + y - 1, (x-1)*7 + ny);
      if(s[x-1][nyy] == '1') uf.unite((x-1)*7 + y - 1, (x-1)*7 + nyy);
      for(auto w: e[x-1]) {
	if(s[w][y-1] == '1') {
	  uf.unite((x-1)*7 + y - 1, w*7 + y - 1);
	}
      }
    }
    if(t == 2) {
      cout << uf.usize((x - 1) * 7) << "\n";
    }
  }
  
  return 0;
}
0