답안 #1082348

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

const int N = (int) 1e5 + 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 2 ms 3160 KB Output is correct
2 Correct 2 ms 3164 KB Output is correct
3 Correct 1 ms 3164 KB Output is correct
4 Correct 1 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 6612 KB Output is correct
2 Correct 21 ms 6868 KB Output is correct
3 Correct 18 ms 6096 KB Output is correct
4 Correct 18 ms 6868 KB Output is correct
5 Correct 19 ms 6868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 8540 KB Output is correct
2 Correct 23 ms 7772 KB Output is correct
3 Correct 22 ms 6492 KB Output is correct
4 Correct 28 ms 7516 KB Output is correct
5 Correct 28 ms 6992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 8364 KB Output is correct
2 Correct 23 ms 7568 KB Output is correct
3 Correct 28 ms 6740 KB Output is correct
4 Correct 27 ms 7004 KB Output is correct
5 Correct 26 ms 7132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3160 KB Output is correct
2 Correct 2 ms 3164 KB Output is correct
3 Correct 1 ms 3164 KB Output is correct
4 Correct 1 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
6 Correct 2 ms 3160 KB Output is correct
7 Correct 2 ms 3160 KB Output is correct
8 Correct 3 ms 3164 KB Output is correct
9 Correct 2 ms 3164 KB Output is correct
10 Correct 1 ms 3164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3160 KB Output is correct
2 Correct 2 ms 3164 KB Output is correct
3 Correct 1 ms 3164 KB Output is correct
4 Correct 1 ms 3164 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
6 Correct 17 ms 6612 KB Output is correct
7 Correct 21 ms 6868 KB Output is correct
8 Correct 18 ms 6096 KB Output is correct
9 Correct 18 ms 6868 KB Output is correct
10 Correct 19 ms 6868 KB Output is correct
11 Correct 19 ms 8540 KB Output is correct
12 Correct 23 ms 7772 KB Output is correct
13 Correct 22 ms 6492 KB Output is correct
14 Correct 28 ms 7516 KB Output is correct
15 Correct 28 ms 6992 KB Output is correct
16 Correct 19 ms 8364 KB Output is correct
17 Correct 23 ms 7568 KB Output is correct
18 Correct 28 ms 6740 KB Output is correct
19 Correct 27 ms 7004 KB Output is correct
20 Correct 26 ms 7132 KB Output is correct
21 Correct 2 ms 3160 KB Output is correct
22 Correct 2 ms 3160 KB Output is correct
23 Correct 3 ms 3164 KB Output is correct
24 Correct 2 ms 3164 KB Output is correct
25 Correct 1 ms 3164 KB Output is correct
26 Correct 18 ms 8540 KB Output is correct
27 Correct 22 ms 7444 KB Output is correct
28 Correct 29 ms 7148 KB Output is correct
29 Correct 32 ms 7324 KB Output is correct
30 Correct 27 ms 7260 KB Output is correct