이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 4;
const int INF = 1e9;
int n, q, sign[maxn];
struct DisjointSet {
int par[maxn];
vector<int> mySet[maxn];
DisjointSet(int n) {
for (int i = 1; i <= n; i++)
par[i] = i, mySet[i].pb(i);
}
int find(int x) {
if (x != par[x]) par[x] = find(par[x]);
return par[x];
}
bool join(int u, int v, int type) {
int flag = sign[u] == sign[v];
u = find(u); v = find(v);
if (u == v) return 0;
if (mySet[v].size() > mySet[u].size())
swap(mySet[u], mySet[v]);
for (int x : mySet[v]) {
if (type == flag) sign[x] ^= 1;
mySet[u].pb(x);
}
mySet[v].clear();
par[v] = u;
return 1;
}
};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n >> q;
memset(sign, 1, sizeof sign);
DisjointSet Dsu(n);
for (int i = 1; i <= q; i++) {
char type; int u, v; cin >> type >> u >> v;
if (type == 'Q') {
if (Dsu.find(u) != Dsu.find(v)) cout << '?';
else cout << (sign[u] == sign[v] ? 'R' : 'A');
cout << '\n';
}
else Dsu.join(u, v, (type == 'R' ? 0 : 1));
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |