/*
* Created at 1:32 PM on 30 Mar 2024
*/
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <numeric>
#include <cassert>
#include <functional>
#ifdef LOCAL
#include "debug.h"
#else
#define trace(...) 42
#endif
struct DSU {
private:
std::size_t n;
std::vector<size_t> link;
std::vector<int> size;
std::vector<int> flipped;
public:
explicit DSU(const size_t n_) : n(n_),
size(n_, 1), link(n_),
flipped(n_, 0) {
std::iota(link.begin(), link.end(), 0);
}
std::size_t find(std::size_t x) const {
while (x != link[x]) {
x = link[x];
}
return x;
}
int find_charge(std::size_t x) const {
auto res = flipped[x];
while (x != link[x]) {
x = link[x];
res ^= flipped[x];
}
return res;
}
enum class InteractionType {
attract,
repel,
uncertain
};
bool unite(std::size_t x, std::size_t y, const InteractionType it) {
assert(it != InteractionType::uncertain);
bool flip = it == InteractionType::attract and find_charge(x) == find_charge(y);
flip |= it == InteractionType::repel and find_charge(x) != find_charge(y);
x = find(x);
y = find(y);
if (x == y) {
assert(!flip);
return false;
}
if (size[x] < size[y]) {
std::swap(x, y);
}
trace(x, y, flip);
flipped[y] ^= flip; // TODO: checks?
link[y] = x;
size[x] += size[y];
return true;
}
InteractionType interact(const std::size_t x, const std::size_t y) const {
if (find(x) != find(y)) {
return InteractionType::uncertain;
}
return find_charge(x) != find_charge(y) ? InteractionType::attract : InteractionType::repel;
}
};
int32_t main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::size_t n, q;
std::cin >> n >> q;
// if (n > 1000) {
// std::cout << "Hello\n";
// return 0;
// }
DSU dsu(n);
while (q--) {
char op;
std::cin >> op;
std::size_t a, b;
std::cin >> a >> b;
--a, --b;
if (op == 'A') {
dsu.unite(a, b, DSU::InteractionType::attract);
} else if (op == 'R') {
dsu.unite(a, b, DSU::InteractionType::repel);
} else if (op == 'Q') {
char ans;
switch (dsu.interact(a, b)) {
case DSU::InteractionType::attract:
ans = 'A';
break;
case DSU::InteractionType::repel:
ans = 'R';
break;
case DSU::InteractionType::uncertain:
ans = '?';
break;
}
std::cout << ans << '\n';
}
}
return 0;
}
Compilation message
charges.cpp: In constructor 'DSU::DSU(size_t)':
charges.cpp:26:22: warning: 'DSU::size' will be initialized after [-Wreorder]
26 | std::vector<int> size;
| ^~~~
charges.cpp:25:25: warning: 'std::vector<long unsigned int> DSU::link' [-Wreorder]
25 | std::vector<size_t> link;
| ^~~~
charges.cpp:31:14: warning: when initialized here [-Wreorder]
31 | explicit DSU(const size_t n_) : n(n_),
| ^~~
charges.cpp: In member function 'bool DSU::unite(std::size_t, std::size_t, DSU::InteractionType)':
charges.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define trace(...) 42
| ^~
charges.cpp:77:9: note: in expansion of macro 'trace'
77 | trace(x, y, flip);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
1884 KB |
Output is correct |
2 |
Correct |
13 ms |
1884 KB |
Output is correct |
3 |
Correct |
20 ms |
2184 KB |
Output is correct |
4 |
Correct |
14 ms |
1884 KB |
Output is correct |
5 |
Correct |
13 ms |
1984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
1884 KB |
Output is correct |
2 |
Correct |
15 ms |
1884 KB |
Output is correct |
3 |
Correct |
17 ms |
2044 KB |
Output is correct |
4 |
Correct |
18 ms |
1884 KB |
Output is correct |
5 |
Correct |
20 ms |
1880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
1884 KB |
Output is correct |
2 |
Correct |
21 ms |
2292 KB |
Output is correct |
3 |
Correct |
19 ms |
1884 KB |
Output is correct |
4 |
Correct |
17 ms |
2052 KB |
Output is correct |
5 |
Correct |
17 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
12 ms |
1884 KB |
Output is correct |
7 |
Correct |
13 ms |
1884 KB |
Output is correct |
8 |
Correct |
20 ms |
2184 KB |
Output is correct |
9 |
Correct |
14 ms |
1884 KB |
Output is correct |
10 |
Correct |
13 ms |
1984 KB |
Output is correct |
11 |
Correct |
15 ms |
1884 KB |
Output is correct |
12 |
Correct |
15 ms |
1884 KB |
Output is correct |
13 |
Correct |
17 ms |
2044 KB |
Output is correct |
14 |
Correct |
18 ms |
1884 KB |
Output is correct |
15 |
Correct |
20 ms |
1880 KB |
Output is correct |
16 |
Correct |
17 ms |
1884 KB |
Output is correct |
17 |
Correct |
21 ms |
2292 KB |
Output is correct |
18 |
Correct |
19 ms |
1884 KB |
Output is correct |
19 |
Correct |
17 ms |
2052 KB |
Output is correct |
20 |
Correct |
17 ms |
1884 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
14 ms |
1884 KB |
Output is correct |
27 |
Correct |
15 ms |
1884 KB |
Output is correct |
28 |
Correct |
17 ms |
1920 KB |
Output is correct |
29 |
Correct |
21 ms |
1884 KB |
Output is correct |
30 |
Correct |
16 ms |
1884 KB |
Output is correct |