QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#677269#9484. Colored Complete Graphm107239#TL 0ms3808kbC++201.9kb2024-10-26 10:52:502024-10-26 10:52:51

Judging History

你现在查看的是最新测评结果

  • [2024-10-26 10:52:51]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3808kb
  • [2024-10-26 10:52:50]
  • 提交

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(root[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);
        }
    }
    for (int i = 1, j = 1; i <= n && (r.component != 1 && b.component != 1); i++) {
        while (b.find(i) == b.find(j) || r.find(i) == r.find(j)) {
            j = j % n + 1;
        }
        cout << "? " << i << " " << j << endl;
        string z;
        cin >> z;
        if (z[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: 100
Accepted
time: 0ms
memory: 3808kb

input:

3
B
B
B

output:

? 1 2
? 2 3
? 3 1
!
1 2
2 3

result:

ok AC

Test #2:

score: -100
Time Limit Exceeded

input:

983
B
R
R
B
B
B
B
B
R
B
R
R
R
R
R
R
R
B
B
R
R
B
R
B
R
R
B
B
R
B
R
R
R
R
B
R
B
B
B
R
R
R
B
B
R
R
B
R
B
R
B
B
B
R
B
R
R
B
R
B
B
R
R
R
B
B
B
B
R
B
R
R
B
R
B
B
R
B
R
B
R
B
R
R
R
B
B
B
R
R
B
B
B
R
R
B
R
B
B
B
R
B
B
R
R
B
B
R
R
R
R
B
R
R
B
B
B
R
B
B
B
B
R
B
R
R
B
R
R
R
B
R
R
B
R
R
B
R
R
B
R
B
R
B
B
R
B
R
...

output:

? 1 2
? 2 3
? 3 4
? 4 5
? 5 6
? 6 7
? 7 8
? 8 9
? 9 10
? 10 11
? 11 12
? 12 13
? 13 14
? 14 15
? 15 16
? 16 17
? 17 18
? 18 19
? 19 20
? 20 21
? 21 22
? 22 23
? 23 24
? 24 25
? 25 26
? 26 27
? 27 28
? 28 29
? 29 30
? 30 31
? 31 32
? 32 33
? 33 34
? 34 35
? 35 36
? 36 37
? 37 38
? 38 39
? 39 40
? 40 ...

result: