답안 #1082345

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1082345 2024-08-31T07:54:56 Z vuavisao Experimental Charges (NOI19_charges) C++14
100 / 100
30 ms 9820 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 100'000 + 10;
const char STATE[] = {'A', 'R', '?'};


int n, q;
int type[N];
vector<pair<int, int>> g[N];
pair<int, int> qq[N];

int parent[N];
int state[N];
int res[N];

int ancestor(int u) {
	if (parent[u] == u) return u;
	return parent[u] = ancestor(parent[u]);
}

void join(int u, int v) {
	u = ancestor(u); v = ancestor(v);
	if (u == v) return;
	parent[v] = u;
}

void dfs(int u) {
	for (const auto& x : g[u]) {
		int v = x.first, change = x.second;
		if (state[v] != -1) continue;
		state[v] = state[u] ^ change;
		dfs(v);
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cin >> n >> q;
	for (int i = 1; i <= q; ++ i) {
		char c; cin >> c;
		cin >> qq[i].first >> qq[i].second;
		int u = qq[i].first, v = qq[i].second;
		if (c == 'A') {
			g[u].push_back(make_pair(v, 1));
			g[v].push_back(make_pair(u, 1));
		}
		else if (c == 'R') {
			g[u].push_back(make_pair(v, 0));
			g[v].push_back(make_pair(u, 0));
		}
		else {
			type[i] = 1;
		}
	}

	memset(state, -1, sizeof(state));
	for (int i = 1; i <= n; ++ i) {
		if (state[i] == -1) {
			state[i] = 0;
			dfs(i);
		}
	}
	
	for (int i = 1; i <= n; ++ i) parent[i] = i;

	for (int i = 1; i <= q; ++ i) {
		int u = qq[i].first, v = qq[i].second;
		if (type[i] == 0) {
			join(u, v);
		}
		else {
			int res = 2;
			if (ancestor(u) == ancestor(v)) {
				res = (state[u] == state[v]);
			}
			cout << STATE[res] << '\n';
		}
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3164 KB Output is correct
2 Correct 1 ms 3164 KB Output is correct
3 Correct 2 ms 3168 KB Output is correct
4 Correct 2 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 7628 KB Output is correct
2 Correct 19 ms 7780 KB Output is correct
3 Correct 20 ms 7116 KB Output is correct
4 Correct 22 ms 7892 KB Output is correct
5 Correct 26 ms 7892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 9812 KB Output is correct
2 Correct 24 ms 9052 KB Output is correct
3 Correct 23 ms 7772 KB Output is correct
4 Correct 30 ms 8788 KB Output is correct
5 Correct 27 ms 8280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 9560 KB Output is correct
2 Correct 26 ms 8792 KB Output is correct
3 Correct 29 ms 8032 KB Output is correct
4 Correct 30 ms 8296 KB Output is correct
5 Correct 27 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3164 KB Output is correct
2 Correct 1 ms 3164 KB Output is correct
3 Correct 2 ms 3168 KB Output is correct
4 Correct 2 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
6 Correct 2 ms 3416 KB Output is correct
7 Correct 2 ms 3164 KB Output is correct
8 Correct 2 ms 3164 KB Output is correct
9 Correct 2 ms 3164 KB Output is correct
10 Correct 2 ms 3164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3164 KB Output is correct
2 Correct 1 ms 3164 KB Output is correct
3 Correct 2 ms 3168 KB Output is correct
4 Correct 2 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
6 Correct 18 ms 7628 KB Output is correct
7 Correct 19 ms 7780 KB Output is correct
8 Correct 20 ms 7116 KB Output is correct
9 Correct 22 ms 7892 KB Output is correct
10 Correct 26 ms 7892 KB Output is correct
11 Correct 22 ms 9812 KB Output is correct
12 Correct 24 ms 9052 KB Output is correct
13 Correct 23 ms 7772 KB Output is correct
14 Correct 30 ms 8788 KB Output is correct
15 Correct 27 ms 8280 KB Output is correct
16 Correct 21 ms 9560 KB Output is correct
17 Correct 26 ms 8792 KB Output is correct
18 Correct 29 ms 8032 KB Output is correct
19 Correct 30 ms 8296 KB Output is correct
20 Correct 27 ms 8540 KB Output is correct
21 Correct 2 ms 3416 KB Output is correct
22 Correct 2 ms 3164 KB Output is correct
23 Correct 2 ms 3164 KB Output is correct
24 Correct 2 ms 3164 KB Output is correct
25 Correct 2 ms 3164 KB Output is correct
26 Correct 20 ms 9820 KB Output is correct
27 Correct 27 ms 8836 KB Output is correct
28 Correct 29 ms 8540 KB Output is correct
29 Correct 25 ms 8532 KB Output is correct
30 Correct 28 ms 8540 KB Output is correct