QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#369427 | #6503. DFS Order 3 | bigJ | WA | 89ms | 3836kb | C++20 | 1.8kb | 2024-03-28 08:55:39 | 2024-03-28 08:55:39 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = int64_t;
namespace stdr = std::ranges;
namespace stdv = std::views;
struct DSU {
std::vector<int> f, siz;
DSU() {}
DSU(int n) {
init(n);
}
void init(int n) {
f.resize(n);
std::iota(f.begin(), f.end(), 0);
siz.assign(n, 1);
}
constexpr int find(int x) {
while (x != f[x]) {
x = f[x] = f[f[x]];
}
return x;
}
constexpr int operator[](int i) {
return find(i);
}
bool same(int x, int y) {
return find(x) == find(y);
}
bool merge(int x, int y, bool t = true) {
x = find(x);
y = find(y);
if (x == y) {
return false;
}
if (t && siz[x] < siz[y]) {
std::swap(x, y);
}
siz[x] += siz[y];
f[y] = x;
return true;
}
int size(int x) {
return siz[find(x)];
}
};
void solve() {
int n;
std::cin >> n;
std::vector a(n, std::vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::cin >> a[i][j];
a[i][j]--;
}
}
std::set<std::pair<int, int>> ans;
DSU dsu(n);
for (int j = 0; j < n && ans.size() < n - 1; j++) {
for (int i = 0; i < n && ans.size() < n - 1; i++) {
if (dsu.merge(a[i][0], a[i][j])) {
ans.emplace(a[i][0], a[i][j]);
}
}
}
for (auto [x, y] : ans) {
std::cout << x + 1 << " " << y + 1 << "\n";
}
}
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3836kb
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 1 3 5
result:
ok correct answer! (4 test cases)
Test #2:
score: -100
Wrong Answer
time: 89ms
memory: 3828kb
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 1 4 3 1 3 8 4 5 4 6 6 7 9 10 10 1 1 3 1 4 2 8 2 9 3 8 5 4 6 9 7 5 10 7 1 5 1 9 1 10 2 4 3 8 3 10 5 6 7 5 8 2 1 6 1 7 2 4 2 6 3 7 3 8 5 7 9 6 10 2 1 9 2 3 2 10 3 6 3 7 4 5 4 7 5 1 8 9 1 8 1 10 2 6 2 10 3 8 4 8 5 10 7 8 7 9 1 2 1 10 2 3 3 7 4 8 5 8 6 5 6 7 9 2 1 4 2 3 4 2 5 9 5 10 6 4 6 7 8 4 9 1 ...
result:
wrong answer ord[1] didn't pass the test (test case 2)