QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#606102 | #8757. 图 | UESTC_OldEastWest | RE | 88ms | 3576kb | C++17 | 2.5kb | 2024-10-02 22:09:08 | 2024-10-02 22:09:14 |
Judging History
answer
#include <bits/stdc++.h>
void charming() {
int n, m; std::cin >> n >> m;
int k = (m + n - 2) / (n - 1);
std::vector G(k, std::vector (n, std::vector<int> ()));
std::vector<std::vector<int> > pre(k, std::vector<int> (n));
for (int i = 0; i < k; ++i) {
std::iota(pre[i].begin(), pre[i].end(), 0);
}
std::function<int(int, int)> find = [&](int d, int x) {
if (x == pre[d][x]) return x;
else return pre[d][x] = find(d, pre[d][x]);
};
std::function<bool(int, int, int)> Check = [&](int d, int u, int v) {
int fu = find(d, u), fv = find(d, v);
if (fu == fv) return false;
else return true;
};
std::function<std::vector<std::vector<int> >(int, int)> Solve
= [&](int st, int en) {
std::vector<std::vector<int> > path;
for (int i = 0; i < k; ++i) {
std::vector<int> new_path;
std::vector<int> que, lst(n, -1);
que.emplace_back(st);
int head = 0, tail = 1;
while (head < tail) {
int u = que[head++];
for (int v : G[i][u]) {
if (lst[v] == -1 && v != st) {
lst[v] = u;
que.emplace_back(v);
++tail;
}
}
}
assert(lst[en] != -1);
int now = en;
while (now > -1) {
new_path.emplace_back(now);
now = lst[now];
}
reverse(new_path.begin(), new_path.end());
path.emplace_back(new_path);
}
return path;
};
bool ok = false;
std::vector<std::vector<int> > path;
int st = -1, en = -1;
for (int i = 0, u, v; i < m; ++i) {
std::cin >> u >> v, --u, --v;
int l = 0, r = k - 1, idx = r;
while (l <= r) {
int mid = l + r >> 1;
if (Check(mid, u, v)) r = mid - 1, idx = mid;
else l = mid + 1;
}
G[idx][u].emplace_back(v);
G[idx][v].emplace_back(u);
int fu = find(idx, u), fv = find(idx, v);
pre[idx][fu] = fv;
if (idx == k - 1) {
ok = true;
st = u, en = v;
path = Solve(st, en);
break;
}
}
if (ok) {
std::cout << st + 1 << ' ' << en + 1 << '\n';
for (auto vec : path) {
int cnt = vec.size();
std::cout << cnt << ' ';
for (int i = 0 ; i < cnt; ++i) {
std::cout << vec[i] + 1 << " \n"[i == cnt - 1];
}
}
}
else std::cout << -1 << '\n';
}
signed main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int t; std::cin >> t;
while (t--) charming();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 88ms
memory: 3576kb
input:
10000 2 20 1 2 1 2 2 1 1 2 1 2 2 1 1 2 2 1 1 2 1 2 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 1 2 2 1 2 20 2 1 2 1 2 1 2 1 2 1 1 2 1 2 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 1 2 2 1 1 2 2 20 1 2 2 1 1 2 1 2 2 1 2 1 1 2 1 2 2 1 2 1 1 2 1 2 1 2 1 2 2 1 1 2 1 2 1 2 2 1 2 1 2 20 1 2 2 1 2 1 1 2 1 2 1 2 2 1 1 2 2 ...
output:
2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 ...
result:
ok Answer correct. (10000 test cases)
Test #2:
score: -100
Runtime Error
input:
10000 5 20 2 1 2 5 5 3 3 1 4 5 1 4 4 3 4 5 3 5 5 4 2 3 5 2 3 4 3 5 1 4 4 3 4 2 2 1 1 3 5 1 5 20 4 2 1 3 1 2 4 5 2 4 3 1 5 3 5 1 4 5 4 3 2 4 1 4 4 3 5 2 1 2 3 5 1 5 4 1 3 4 4 3 5 20 1 4 1 3 1 5 5 1 4 5 3 4 4 5 2 3 1 2 2 4 4 5 4 5 2 4 2 5 4 2 4 3 4 2 2 5 2 1 3 1 5 20 2 5 2 3 4 5 4 2 3 4 2 1 5 4 2 5 2 ...