QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#265523#6503. DFS Order 3obssTL 1ms3584kbC++171.8kb2023-11-25 19:04:502023-11-25 19:04:50

Judging History

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

  • [2023-11-25 19:04:50]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3584kb
  • [2023-11-25 19:04:50]
  • 提交

answer

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("abm,avx,mmx,popcnt,sse,sse2,sse3,ssse3,sse4")
#include <bits/stdc++.h>
#define tests
const int N = 1e3 + 10;
using namespace std;
using LL = long long;
int g[N][N];
int st[N], ed[N], p[N];
bool del[N];
int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}
void solve() {
    int n;
    cin >> n;
    if (n == 1) return;
    vector<pair<int, int>> ans;
    auto merge = [&](int a, int b) {
        int fa = find(a);
        int fb = find(b);
        if (fa != fb) {
            p[fa] = fb;
            ans.push_back({a, b});
        }
    };
    for (int i = 1; i <= n; i++) p[i] = i;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) cin >> g[i][j];
        st[i] = 1;
        ed[i] = n;
        del[i] = 0;
        merge(g[i][1], g[i][2]);
    }
    vector<int> ye;
    while (ans.size() < n - 1) {
        for (int i = 1; i <= n; i++) {
            if (ed[i] >= st[i] && !del[g[i][ed[i]]]) {
                del[g[i][ed[i]]] = 1;
                ye.push_back(g[i][ed[i]]);
            }
        }
        for (auto v : ye) {
            for (int i = st[v]; i <= ed[v]; i++) {
                if (!del[g[v][i]]) {
                    st[v] = i + 1;
                    merge(v, g[v][i]);
                    break;
                }
            }
        }
        ye.clear();
        for (int i = 1; i <= n; i++) {
            while (ed[i] >= st[i] && del[g[i][ed[i]]]) ed[i]--;
        }
    }
    for (auto [x, y] : ans) cout << x << ' ' << y << endl;
}

signed main() {
    cin.tie(0)->ios::sync_with_stdio(0);
    cout.tie(0);

    int t = 1;
#ifdef tests
    cin >> t;
#endif
    for (int _ = 1; _ <= t; _++) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3584kb

input:

4
2
1 2
2 1
3
1 2 3
2 1 3
3 2 1
4
1 2 3 4
2 1 3 4
3 2 4 1
4 2 1 3
5
1 2 4 3 5
2 4 1 3 5
3 5 1 2 4
4 2 1 3 5
5 3 1 2 4

output:

1 2
1 2
3 2
1 2
3 2
4 2
1 2
2 4
3 5
3 1

result:

ok correct answer! (4 test cases)

Test #2:

score: -100
Time Limit Exceeded

input:

20000
10
1 2 4 5 6 7 3 8 10 9
2 1 4 5 6 7 3 8 10 9
3 8 1 2 4 5 6 7 10 9
4 5 6 7 1 2 3 8 10 9
5 4 6 7 1 2 3 8 10 9
6 7 4 5 1 2 3 8 10 9
7 6 4 5 1 2 3 8 10 9
8 3 1 2 4 5 6 7 10 9
9 10 1 2 4 5 6 7 3 8
10 1 2 4 5 6 7 3 8 9
10
1 4 3 8 2 9 6 5 7 10
2 8 9 6 3 4 1 5 7 10
3 8 2 9 6 4 1 5 7 10
4 1 3 8 2 9 6 5...

output:

1 2
3 8
4 5
6 7
9 10
10 1
3 1
6 4
1 4

result: