QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#837541 | #9772. Permutation Routing | nhuang685 | WA | 0ms | 3476kb | C++23 | 4.0kb | 2024-12-30 06:36:47 | 2024-12-30 06:36:49 |
Judging History
This is the latest submission verdict.
- [2025-01-10 11:38:24]
- hack成功,自动添加数据
- (/hack/1438)
- [2024-12-30 06:36:47]
- Submitted
answer
/**
* @author n685
* @date 2024-12-29 16:25:30
*/
#include "bits/stdc++.h"
#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbg_proj(...) 420
#define dbg_rproj(...) 420420
void nline() {}
void bar() {}
void start_clock() {}
void end_clock() {}
#endif
namespace rs = std::ranges;
namespace rv = std::views;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
void solve() {
int n;
std::cin >> n;
std::vector<int> p(n);
for (int& v : p) {
std::cin >> v;
--v;
}
std::vector<std::vector<std::pair<int, int>>> adj(n);
std::vector<std::pair<int, int>> edges;
for (int i = 0; i < n - 1; ++i) {
int u, v;
std::cin >> u >> v;
--u;
--v;
adj[u].emplace_back(i, v);
adj[v].emplace_back(i, u);
edges.emplace_back(u, v);
}
std::vector<std::vector<int>> ans;
auto apply = [&](const std::vector<int>& v) {
ans.push_back(v);
for (int i : v) {
std::swap(p[edges[i].first], p[edges[i].second]);
}
};
std::vector<int> sub(n), rem(n);
auto comp_sub = [&](auto&& self, int node, int par) -> void {
sub[node] = 1;
for (auto [t, i] : adj[node]) {
if (i == par || rem[i]) {
continue;
}
self(self, i, node);
sub[node] += sub[i];
}
};
auto cent = [&](auto&& self, int node, int par, int sz) -> int {
for (auto [t, i] : adj[node]) {
if (i == par || rem[i]) {
continue;
}
if (2 * sub[i] > sz) {
return self(self, i, node, sz);
}
}
return node;
};
std::vector<int> bel(n);
auto comp_bel_dfs = [&](auto&& self, int node, int par, int val) -> void {
bel[node] = val;
for (auto [t, i] : adj[node]) {
if (i != par && !rem[i]) {
self(self, i, node, val);
}
}
};
auto comp_bel = [&](int rt) -> void {
bel[rt] = -1;
for (auto [t, i] : adj[rt]) {
if (!rem[i]) {
comp_bel_dfs(comp_bel_dfs, i, rt, t);
}
}
};
auto good = [&](int node) -> bool { return bel[node] == bel[p[node]]; };
auto phase1_dfs
= [&](auto&& self, int node, int par, std::vector<int>& fl) -> void {
bool g = good(node);
for (auto [t, i] : adj[node]) {
if (i == par || rem[i]) {
continue;
}
if (g && !good(i)) {
fl.push_back(t);
}
self(self, i, node, fl);
}
};
auto phase1 = [&](int rt, std::vector<int>& fl) -> void {
for (auto [t, i] : adj[rt]) {
if (!rem[i]) {
phase1_dfs(phase1_dfs, i, rt, fl);
}
}
};
auto gen = [&](auto&& self, int rt) -> void {
comp_sub(comp_sub, rt, -1);
rt = cent(cent, rt, -1, sub[rt]);
comp_bel(rt);
while (true) {
std::vector<int> fl;
phase1(rt, fl);
if (fl.empty()) {
break;
}
apply(fl);
}
while (true) {
std::vector<int> fl1;
for (auto [t, i] : adj[rt]) {
if (!rem[i] && !good(i)) {
fl1.push_back(t);
break;
}
}
if (fl1.empty()) {
break;
}
phase1(rt, fl1);
apply(fl1);
std::vector<int> fl2;
for (auto [t, i] : adj[rt]) {
if (!rem[i] && bel[p[rt]] == t) {
fl2.push_back(t);
break;
}
}
phase1(rt, fl2);
if (fl2.empty()) {
break;
}
apply(fl2);
}
rem[rt] = true;
for (auto [t, i] : adj[rt]) {
if (!rem[i]) {
self(self, i);
}
}
};
gen(gen, 0);
std::cout << ans.size() << '\n';
for (const auto& v : ans) {
std::cout << v.size();
for (int i : v) {
std::cout << ' ' << i;
}
std::cout << '\n';
}
}
int main() {
#ifndef LOCAL
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
#endif
int t;
std::cin >> t;
for (int i = 0; i < t; ++i) {
dbg(i + 1);
solve();
bar();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3476kb
input:
1 5 1 4 2 5 3 1 2 2 3 2 4 1 5
output:
7 1 3 1 0 1 1 1 0 1 2 1 0 1 3
result:
wrong answer Integer parameter [name=%d-th edge in step %d] equals to 0, violates the range [1, 4]