結果

問題 No.1266 7 Colors
ユーザー furafura
提出日時 2020-10-23 22:46:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 176 ms / 3,000 ms
コード長 1,636 bytes
コンパイル時間 2,268 ms
コンパイル使用メモリ 206,520 KB
実行使用メモリ 14,880 KB
最終ジャッジ日時 2023-09-28 17:06:34
合計ジャッジ時間 6,651 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 130 ms
5,700 KB
testcase_04 AC 165 ms
11,008 KB
testcase_05 AC 130 ms
6,460 KB
testcase_06 AC 165 ms
12,096 KB
testcase_07 AC 175 ms
13,372 KB
testcase_08 AC 167 ms
11,244 KB
testcase_09 AC 153 ms
9,660 KB
testcase_10 AC 144 ms
8,888 KB
testcase_11 AC 134 ms
6,700 KB
testcase_12 AC 137 ms
7,648 KB
testcase_13 AC 139 ms
8,336 KB
testcase_14 AC 132 ms
5,912 KB
testcase_15 AC 176 ms
13,652 KB
testcase_16 AC 138 ms
7,656 KB
testcase_17 AC 171 ms
12,788 KB
testcase_18 AC 101 ms
14,880 KB
testcase_19 AC 119 ms
14,448 KB
testcase_20 AC 120 ms
14,352 KB
testcase_21 AC 49 ms
4,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

using graph=vector<vector<int>>;

void add_undirected_edge(graph& G,int u,int v){
	G[u].emplace_back(v);
	G[v].emplace_back(u);
}

class union_find{
	int n;
	vector<int> p;
public:
	union_find(int n):n(n),p(n,-1){}
	int find(int u){ return p[u]<0?u:p[u]=find(p[u]); }
	void unite(int u,int v){
		u=find(u); v=find(v);
		if(u!=v){
			if(p[v]<p[u]) swap(u,v);
			p[u]+=p[v]; p[v]=u; n--;
		}
	}
	bool is_same(int u,int v){ return find(u)==find(v); }
	int size()const{ return n; }
	int size(int u){ return -p[find(u)]; }
};

int main(){
	int n,m,q; scanf("%d%d%d",&n,&m,&q);
	vector<string> color(n);
	rep(u,n) cin>>color[u];
	graph G(n);
	rep(i,m){
		int u,v; scanf("%d%d",&u,&v); u--; v--;
		add_undirected_edge(G,u,v);
	}

	union_find U(7*n);
	rep(u,n) for(int v:G[u]) {
		rep(c,7) if(color[u][c]=='1' && color[v][c]=='1') {
			U.unite(u*7+c,v*7+c);
		}
	}
	rep(u,n){
		rep(c,7){
			if(color[u][c]=='1' && color[u][(c+1)%7]=='1') {
				U.unite(7*u+c,7*u+(c+1)%7);
			}
			if(color[u][c]=='1' && color[u][(c+6)%7]=='1') {
				U.unite(7*u+c,7*u+(c+6)%7);
			}
		}
	}

	rep(_,q){
		int type,u,y; scanf("%d%d%d",&type,&u,&y); u--; y--;
		if(type==1){
			color[u][y]='1';
			for(int v:G[u]){
				rep(c,7) if(color[u][c]=='1' && color[v][c]=='1') {
					U.unite(u*7+c,v*7+c);
				}
			}
			rep(c,7){
				if(color[u][c]=='1' && color[u][(c+1)%7]=='1') {
					U.unite(7*u+c,7*u+(c+1)%7);
				}
				if(color[u][c]=='1' && color[u][(c+6)%7]=='1') {
					U.unite(7*u+c,7*u+(c+6)%7);
				}
			}
		}
		else{
			printf("%d\n",U.size(7*u));
		}
	}

	return 0;
}
0