Submission #1240479

#TimeUsernameProblemLanguageResultExecution timeMemory
1240479trimkus열쇠 (IOI21_keys)C++20
0 / 100
1 ms324 KiB
#include "keys.h"
#include <bits/stdc++.h>
using namespace std;


struct DSU {
	vector<int> e;
	vector<unordered_set<int>> st;
	DSU(int n) {
		e = vector<int>(n, -1);
		st = vector<unordered_set<int>>(n);
	}
	int get(int x) {
		return e[x] < 0 ? x : e[x] = get(e[x]);
	}
	bool can_unite(int x, int r) {
		x = get(x);
		return st[x].count(r);
	}
	void unite(int x, int y) {
		x = get(x);
		y = get(y);
		if (x == y) return;
		if (e[x] > e[y]) swap(x, y);
		if (st[x].size() > st[y].size()) {
			swap(st[x], st[y]);
		}
		for (auto& u : st[y]) {
			st[x].insert(u);
		}
		e[x] += e[y];
		st[y].clear();
		e[y] = x;
	}
};


std::vector<int> find_reachable(std::vector<int> r, std::vector<int> u, std::vector<int> v, std::vector<int> c) {
	int N = r.size();
	int M = u.size();
	vector<vector<array<int, 3>>> adj(N);
	for (int i = 0; i < (int)u.size(); ++i) {
		adj[u[i]].push_back({v[i], c[i], i});
		adj[v[i]].push_back({u[i], c[i], i});
	}
	vector<int> cnt(N);
	for (int i = 0; i < N; ++i) {
		vector<int> seen(N), inq(M);
		vector<int> now{i};
		unordered_set<int> have;
		have.insert(r[i]);
		while (true) {
			bool added = false;
			for (auto& v : now) {
				for (auto& [u, c, idx] : adj[v]) {
					if (seen[u]) continue;
					if (have.count(c)) {
						seen[u] = 1;
						now.push_back(u);
						have.insert(r[u]);
						added = true;
					}
				}
			}
			if (!added) break;
		}
		for (int j = 0; j < N; ++j) {
			cnt[i] += seen[j];
		}
	}
	vector<int> res(N);
	int mn = *min_element(begin(cnt), end(cnt));
	for (int i = 0; i < N; ++i) {
		if (mn == cnt[i]) {
			res[i] = 1;
		}
	}
	return res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...