#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define int long long
#define all(x) (x).begin(), (x).end()
typedef long double ld;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef map<int, int> mii;
typedef unordered_map<int, int> umii;
const int mod = 1e9 + 7;
const int inf = INTMAX_MAX;
const bool tc = false;
const int mxn = 1e5 + 1;
int c[mxn]; set<pii> adj[mxn];
class DisjointSets {
public:
vector<int> parents;
vector<int> sizes;
DisjointSets(int size) : parents(size), sizes(size, 1) {
for (int i = 0; i < size; i++) { parents[i] = i; }
}
/** @return the "representative" node in x's component */
int find(int x) { return parents[x] == x ? x : (parents[x] = find(parents[x])); }
/** @return whether the merge changed connectivity */
bool unite(int x, int y) {
int x_root = find(x);
int y_root = find(y);
if (x_root == y_root) { return false; }
if (sizes[x_root] < sizes[y_root]) { swap(x_root, y_root); }
sizes[x_root] += sizes[y_root];
parents[y_root] = x_root;
return true;
}
/** @return whether x and y are in the same connected component */
bool connected(int x, int y) { return find(x) == find(y); }
};
void dfs(int node, int col) {
if (c[node] == col) return;
c[node] = col;
for (auto &neighbour : adj[node]) {
if (c[neighbour.fi] == col * neighbour.se) continue;
dfs(neighbour.fi, col * neighbour.se);
}
}
inline void solve() {
int n, q;
cin >> n >> q;
DisjointSets dsu(n);
while (q--) {
char t; cin >> t;
if (t == 'A') {
// attract, different
int x, y;
cin >> x >> y;
x--; y--;
if (c[x] == c[y] && c[x] != 0) {
if (dsu.sizes[dsu.find(x)] < dsu.sizes[dsu.find(y)]) dfs(x, -c[y]);
else dfs(y, -c[x]);
dsu.unite(x, y);
continue;
}
dsu.unite(x, y);
adj[x].insert({y, -1});
adj[y].insert({x, -1});
if (!c[x] && !c[y]) {
assert(dsu.sizes[dsu.find(x)] == 2 && dsu.sizes[dsu.find(y)] == 2);
c[x] = 1; c[y] = -1;
continue;
}
if (!c[x]) {
// dfs from x
dfs(x, -1 * c[y]);
}
if (!c[y]) {
// dfs from y
dfs(y, -1 * c[x]);
}
} else if (t == 'R') {
int x, y;
cin >> x >> y;
x--; y--;
if (c[x] != c[y] && c[x] != 0) {
if (dsu.sizes[dsu.find(x)] < dsu.sizes[dsu.find(y)]) dfs(x, c[y]);
else dfs(y, c[x]);
dsu.unite(x, y);
continue;
}
dsu.unite(x, y);
adj[x].insert({y, 1});
adj[y].insert({x, 1});
if (!c[x] && !c[y]) {
//cout << dsu.sizes[dsu.find(x)] << " " << dsu.sizes[dsu.find(x)] << '\n';
assert(dsu.sizes[dsu.find(x)] == 2 && dsu.sizes[dsu.find(y)] == 2);
c[x] = 1; c[y] = 1;
continue;
}
if (!c[x]) {
// dfs from x
dfs(x, c[y]);
}
if (!c[y]) {
// dfs from y
dfs(y, c[x]);
}
} else {
// query
int x, y;
cin >> x >> y;
x--; y--;
if (!dsu.connected(x, y)) {
cout << "?\n";
continue;
}
cout << (c[x] == c[y] ? "R" : "A") << '\n';
}
}
}
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
signed main() {
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
//setIO();
int t = 1;
if (tc) {
cin >> t;
}
while (t--) {
solve();
}
}
Compilation message (stderr)
charges.cpp: In function 'void setIO(std::string)':
charges.cpp:150:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
150 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
charges.cpp:151:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
151 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |