QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#677223 | #9484. Colored Complete Graph | m107239# | ML | 0ms | 0kb | C++20 | 2.2kb | 2024-10-26 10:35:59 | 2024-10-26 10:35:59 |
answer
#include <iostream>
#include <vector>
#include <string>
using namespace std;
using ll = long long;
struct Dsu {
vector<int> root;
vector<pair<int, int>> edges;
int component;
Dsu(int n): root(n + 1), component(n) {
for (int i = 1; i <= n; i++) {
root[i] = i;
}
}
int find(int x) {
return root[x] == x ? x : root[x] = find(x);
}
int _merge(int x, int y) {
int x_ = find(x), y_ = find(y);
if (x_ == y_) {
return false;
}
root[x_] = y_;
edges.emplace_back(x, y);
return true;
}
void merge(int x, int y) {
component -= _merge(x, y);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> ess;
Dsu r(n), b(n);
char prev = -1, first = -1;
for (int i = 1; i <= n; i++) {
cout << "? " << i << " " << i % n + 1 << endl;
string z;
cin >> z;
if (z[0] == 'R') {
r.merge(i, i % n + 1);
} else {
b.merge(i, i % n + 1);
}
if (i == 1) {
first = z[0];
} else if (prev != z[0]) {
ess.push_back(i);
}
prev = z[0];
}
if (prev != first) {
ess.push_back(1);
}
for (int i = 1; i <= n && (r.component != 1 && b.component != 1); i++) {
for (int j = 1; j <= n && (r.component != 1 && b.component != 1); j++) {
if (r.find(i) != r.find(j) && b.find(i) != b.find(j)) {
cout << "? " << i << " " << j << endl;
string s;
cin >> s;
if (s[0] == 'R') {
r.merge(i, j);
} else {
b.merge(i, j);
}
}
}
}
if (r.component == 1) {
cout << "!" << "\n";
for (auto [u, v] : r.edges) {
cout << u << " " << v << "\n";
}
} else if (b.component == 1) {
cout << "!" << "\n";
for (auto [u, v] : b.edges) {
cout << u << " " << v << "\n";
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Memory Limit Exceeded
input:
3 B B B
output:
? 1 2 ? 2 3 ? 3 1