結果

問題 No.1266 7 Colors
ユーザー saksak
提出日時 2020-11-28 20:01:28
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 515 ms / 3,000 ms
コード長 3,326 bytes
コンパイル時間 2,716 ms
コンパイル使用メモリ 217,232 KB
実行使用メモリ 41,084 KB
最終ジャッジ日時 2023-10-10 01:03:11
合計ジャッジ時間 14,319 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 334 ms
13,104 KB
testcase_04 AC 502 ms
30,248 KB
testcase_05 AC 354 ms
14,548 KB
testcase_06 AC 502 ms
33,028 KB
testcase_07 AC 513 ms
36,752 KB
testcase_08 AC 503 ms
31,040 KB
testcase_09 AC 461 ms
25,308 KB
testcase_10 AC 453 ms
23,108 KB
testcase_11 AC 399 ms
16,784 KB
testcase_12 AC 412 ms
18,844 KB
testcase_13 AC 430 ms
20,688 KB
testcase_14 AC 357 ms
14,940 KB
testcase_15 AC 515 ms
37,500 KB
testcase_16 AC 419 ms
19,376 KB
testcase_17 AC 508 ms
35,920 KB
testcase_18 AC 340 ms
39,776 KB
testcase_19 AC 235 ms
40,888 KB
testcase_20 AC 234 ms
41,084 KB
testcase_21 AC 223 ms
9,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (int i=(int)N-1; i>=0; i--)

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(int x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { int d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

struct query { ll t, x, y; };

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

struct UnionFind2d {
  vector<vector<p_ll>> par;
  vector<vector<ll>> s;
  UnionFind2d(ll N, ll M) : par(N,vector<p_ll>(M)), s(N,vector<ll>(M)) { 
    rep(i,N) rep(j,M) { par[i][j] = make_pair(i,j); s[i][j] = 1; } 
  }
  bool isroot(p_ll x) { return par[x.first][x.second]==x; }
  p_ll root(p_ll x) { return isroot(x) ? x : par[x.first][x.second] = root(par[x.first][x.second]); }
  ll size(p_ll x) { return isroot(x) ? s[x.first][x.second] : s[x.first][x.second] = size(root(x)); }
  void unite(p_ll x, p_ll y) { 
    p_ll rx=root(x), ry=root(y);
    if (rx!=ry) { 
      s[rx.first][rx.second] += s[ry.first][ry.second];
      par[ry.first][ry.second] = rx;
    } 
  }
};

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

int main() {
  ll N, M, Q; cin >> N >> M >> Q;
  string s[N]; rep(i,N) cin >> s[i];
  ll u[M], v[M]; rep(i,M) { cin >> u[i] >> v[i]; u[i]--; v[i]--; }
  query q[Q]; rep(i,Q) { cin >> q[i].t >> q[i].x >> q[i].y; q[i].x--; q[i].y--; }

  vector<vector<ll>> adj(N);
  rep(i,M) {
    adj[u[i]].push_back(v[i]);
    adj[v[i]].push_back(u[i]);
  }

  UnionFind2d uf(N,7);
  rep(i,N) rep(j,7) {
    if (s[i][j]=='1'&&s[i][(j+1)%7]=='1') {
      uf.unite(make_pair(i,j), make_pair(i,(j+1)%7));
    }
  }
  rep(i,N) rep(j,7) {
    for (auto x: adj[i]) {
      if (s[i][j]=='1'&&s[x][j]=='1') {
        uf.unite(make_pair(i,j), make_pair(x,j));
      }
    }
  }

  rep(i,Q) {
    if (q[i].t==1) {
      s[q[i].x][q[i].y] = '1';
      if (s[q[i].x][(q[i].y+6)%7]=='1') {
        uf.unite(make_pair(q[i].x,q[i].y), make_pair(q[i].x,(q[i].y+6)%7));
      }
      if (s[q[i].x][(q[i].y+1)%7]=='1') {
        uf.unite(make_pair(q[i].x,q[i].y), make_pair(q[i].x,(q[i].y+1)%7));
      }
      for (auto x: adj[q[i].x]) {
        if (s[x][q[i].y]=='1') {
          uf.unite(make_pair(q[i].x,q[i].y), make_pair(x,q[i].y));
        }
      }
    }
    else cout << uf.size(make_pair(q[i].x,0)) << endl;
  }
  // cout << result << endl;
  return 0;
}
0