結果

問題 No.1266 7 Colors
ユーザー okok
提出日時 2020-10-23 22:56:31
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 186 ms / 3,000 ms
コード長 3,222 bytes
コンパイル時間 960 ms
コンパイル使用メモリ 89,304 KB
実行使用メモリ 21,312 KB
最終ジャッジ日時 2023-09-28 17:26:28
合計ジャッジ時間 5,386 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 124 ms
7,800 KB
testcase_04 AC 165 ms
16,144 KB
testcase_05 AC 128 ms
8,340 KB
testcase_06 AC 175 ms
17,464 KB
testcase_07 AC 180 ms
19,536 KB
testcase_08 AC 172 ms
16,396 KB
testcase_09 AC 161 ms
13,548 KB
testcase_10 AC 152 ms
12,620 KB
testcase_11 AC 136 ms
9,404 KB
testcase_12 AC 143 ms
10,496 KB
testcase_13 AC 147 ms
11,500 KB
testcase_14 AC 130 ms
8,360 KB
testcase_15 AC 186 ms
19,752 KB
testcase_16 AC 140 ms
10,712 KB
testcase_17 AC 173 ms
19,004 KB
testcase_18 AC 76 ms
21,312 KB
testcase_19 AC 90 ms
20,208 KB
testcase_20 AC 90 ms
20,256 KB
testcase_21 AC 46 ms
4,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>
#include<bitset>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;


class union_find
{
	int  _setnum;
	vector<int> par, nume;
public:
	union_find(){
	}
	
	union_find(int x){
		par.resize(x);
		nume.resize(x);
		init();
	}
	
	~union_find(){
		//
		
	}
	
	void clear(){
		_setnum = 0;
		par.clear();
		nume.clear();
	}
	
	void init(){
		_setnum = par.size();
		for(int i = 0; i < par.size(); i++){
			par[i] = i;
			nume[i] = 1;
		}
	}
	
	void resize(int x){
		
		par.resize(x);
		nume.resize(x);
		init();
	}

	int find(int x){
		return par[x] == x ? x : par[x] = find(par[x]);
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
	
		if(x == y)return;
		
		_setnum--;
		
		if(nume[x] > nume[y]) std::swap(x,y);
		
		par[x] = y;
		nume[y] += nume[x];
	}
	
	bool same(int x, int y){
		return find(x) == find(y);
	}
	
	int numel(int x){
		return nume[find(x)];
	}
	
	int size(){
		return par.size();
	}
	
	int setnum(){
		return _setnum;
	}
};


signed main(){
	cout<<fixed<<setprecision(10);
    
	const int MAX = 7;
	int N, M, Q;
    vector<int> col;
    vector<vector<int>> G;
    union_find uf;
    
    cin>>N>>M>>Q;
	
    col.resize(N);
    G.resize(N);
    uf.resize(N * MAX);
    
	for(int i = 0; i < N; i++){
        string s;
        
        cin>>s;
        
        for(int j = 0; j < MAX; j++){
            col[i] = col[i] * 2 + s[j] - '0';
        }
        
        for(int j = 0; j < MAX; j++){
            if(((col[i]>>j)&1) && ((col[i]>>((j+1)%MAX))&1)) {
                uf.unite(i * MAX + (j+1)%MAX, i * MAX + j);
            }
        }
    }
    
    for(int i = 0; i < M; i++){
        int u, v;
        
        cin>>u>>v;
        
        u--, v--;
        
        G[u].push_back(v);
        G[v].push_back(u);
    }
    
    for(int i = 0; i < N; i++){
        for(int j = 0; j < G[i].size(); j++){
            for(int k = 0; k < MAX; k++){
                if(((col[i]>>k)&1) && ((col[G[i][j]]>>(k))&1)) {
                    uf.unite(i * MAX + k, G[i][j] * MAX + k);
                }
            }
        }
    }
    
    for(int i = 0; i < Q; i++){
        int a, b, c;
        
        cin>>a>>b>>c;
        
        if(a == 1) {
            c = MAX - c;
            b--;
            
            col[b] |= 1<<c;
            
            for(int j = 0; j < MAX; j++){
                if(((col[b]>>j)&1) && ((col[b]>>((j+1)%MAX))&1)) {
                    uf.unite(b * MAX + (j+1)%MAX, b * MAX + j);
                }
            }
        
            for(int j = 0; j < G[b].size(); j++){
                for(int k = 0; k < MAX; k++){
                    if(((col[b]>>k)&1) && ((col[G[b][j]]>>(k))&1)) {
                        uf.unite(b * MAX + k, G[b][j] * MAX + k);
                    }
                }
            }
            
        } else if(a == 2) {
            b--;
            cout<<uf.numel(b * MAX + 6)<<endl;
        }
    }
	return 0;
}
0