QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#658429 | #9484. Colored Complete Graph | ucup-team4435# | TL | 0ms | 0kb | C++20 | 2.3kb | 2024-10-19 16:48:33 | 2024-10-19 16:48:33 |
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct DSU {
vector<int> fa, sz;
explicit DSU(int n) : fa(n), sz(n, 1) {
iota(fa.begin(), fa.end(), 0);
}
DSU() = default;
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
bool same(int a, int b) {
return find(a) == find(b);
}
bool unite(int a, int b) {
a = find(a), b = find(b);
if (a == b) {
return false;
}
if (sz[a] < sz[b]) {
swap(a, b);
}
fa[b] = a;
sz[a] += sz[b];
return true;
}
};
void solve() {
int n;
cin >> n;
vector<int> c[2];
vector<pair<int, int>> edges[2];
vector adj(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
cin >> adj[i][j];
}
}
int Q = 0;
auto ask = [&](int i, int j) {
cout << "? " << i + 1 << " " << j + 1 << endl;
Q += 1;
#ifndef LOCAL
char c;
cin >> c;
assert(c != 'F');
edges[c == 'B'].push_back({i, j});
return c == 'B';
#else
int f = adj[i][j];
edges[f].push_back({i, j});
return f;
#endif
};
for (int x = 0; x < n; ++x) {
c[0].push_back(x);
c[1].push_back(x);
int aim = c[0].size() == 2 ? 0 : 1;
while (c[0].size() > 1 && c[1].size() > 1) {
if (ask(x, c[aim ^ 1].end()[-2]) == aim) {
c[aim] = {x};
break;
} else {
c[aim ^ 1].end()[-2] = x;
c[aim ^ 1].pop_back();
}
}
}
int aim = c[1].size() == 1;
assert(c[0].size() == 1 || c[1].size() == 1);
DSU d(n);
vector<pair<int, int>> e;
for (auto [u, v] : edges[aim]) {
if (d.unite(u, v)) {
e.push_back({u, v});
}
}
assert(e.size() == n - 1);
cout << "!" << endl;
for (auto [u, v] : e) {
assert(adj[u][v] == aim);
cout << u + 1 << " " << v + 1 << endl;
}
// cerr << "Q: " << Q << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int test = 1;
// cin >> test;
while (test--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3