QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#827938 | #9772. Permutation Routing | ucup-team6225 | WA | 5ms | 3936kb | C++17 | 3.4kb | 2024-12-23 11:37:21 | 2024-12-23 11:37:21 |
Judging History
answer
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
using pii = pair<int, int>;
const int N = 1010, SZ = (1 << 18) + 5;
static char buf[SZ], *bgn = buf, *til = buf;
char getc() {
if(bgn == til) bgn = buf, til = buf + fread(buf, 1, SZ, stdin);
return bgn == til ? EOF : *bgn++;
}
template<typename T>
void read(T &x) {
char ch = getc(); T fh = 0; x = 0;
while(ch < '0' || ch > '9') fh |= (ch == '-'), ch = getc();
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getc();
x = fh ? -x : x;
}
template<typename Type, typename... argc>
void read(Type &x, argc &...args) {read(x), read(args...);}
template<typename T>
void print(T x) {
if(x < 0) putchar('-'), x = -x;
if(x / 10) print(x / 10);
putchar(x % 10 + '0');
}
int n, p[N], u[N], v[N], siz[N], fr[N], tot, mn, root, vis[N], sum[N], cnt;
vector<pii> edge[N];
vector<int> opr[N], tmp, lin;
void dfszx(int x, int fa) {
siz[x] = 1;
int res = 0;
for(auto [y, i] : edge[x])
if(y != fa && !vis[y]) dfszx(y, x), res = max(res, siz[y]), siz[x] += siz[y];
res = max(res, tot - siz[x]);
if(res < mn) mn = res, root = x;
}
void getroot(int x) {dfszx(x, 0), tot = siz[x], mn = N, dfszx(x, 0);}
void dfs(int x, int fa, int id) {
fr[x] = id, tmp.pb(x);
for(auto [y, i] : edge[x]) if(y != fa && !vis[y]) dfs(y, x, id);
}
void Dfs(int x, int fa) {
sum[x] = fr[x] != fr[p[x]];
for(auto [y, i] : edge[x]) if(y != fa && !vis[y]) {
Dfs(y, x);
if(sum[y] && !sum[x]) lin.pb(i);
}
}
void solve(int x, int num) {
vis[x] = 1, fr[x] = x, tmp.clear(), tmp.pb(x);
for(auto [y, i] : edge[x]) if(!vis[y]) dfs(y, x, y);
// cerr << x << " " << num << "\n";
while(1) {
lin.clear(), Dfs(x, 0);
if(lin.empty()) break;
++num;
for(int i : lin) opr[num].pb(i), swap(p[u[i]], p[v[i]]);
}
// cerr << num << "\n";
while(1) {
int res = 0;
for(int i : tmp) sum[i] = fr[i] != fr[p[i]], res += sum[i];
if(!res) break;
if(!sum[x]) {
for(auto [y, i] : edge[x]) if(!vis[y] && sum[y]) {
swap(p[x], p[y]), opr[++num].pb(i);
break;
}
continue;
}
for(auto [y, i] : edge[x]) if(!vis[y] && y == fr[p[x]]) {
opr[++num].pb(i); break;
}
lin.clear(), Dfs(x, 0);
for(int i : lin) opr[num].pb(i), swap(p[u[i]], p[v[i]]);
swap(p[x], p[fr[p[x]]]);
}
cnt = max(cnt, num);
for(auto [y, i] : edge[x]) if(!vis[y]) getroot(y), solve(root, num);
}
void mian() {
for(int i = 1; i <= cnt; ++i) opr[i].clear();
read(n), cnt = 0;
for(int i = 1; i <= n; ++i) read(p[i]), edge[i].clear(), vis[i] = 0;
for(int i = 1; i < n; ++i) {
read(u[i], v[i]);
edge[u[i]].pb({v[i], i}), edge[v[i]].pb({u[i], i});
}
getroot(1), solve(root, 0);
print(cnt), putchar('\n');
for(int i = 1; i <= cnt; ++i) {
print(opr[i].size()), putchar(' ');
for(int x : opr[i]) print(x), putchar(' ');
putchar('\n');
}
}
int main() {
#ifdef Kelly
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("err.txt", "w", stderr);
#endif
int T = 1; read(T);
while(T--) mian();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3636kb
input:
1 5 1 4 2 5 3 1 2 2 3 2 4 1 5
output:
5 1 4 1 3 1 1 1 2 1 4
result:
ok ok, up to 5 steps were used
Test #2:
score: -100
Wrong Answer
time: 5ms
memory: 3936kb
input:
10000 5 2 3 1 5 4 1 5 3 2 1 2 1 4 5 1 2 3 4 5 2 3 3 4 2 1 4 5 5 4 2 5 1 3 3 5 2 3 4 1 3 1 5 1 3 4 2 5 5 3 2 1 1 3 2 4 5 1 2 3 4 5 2 1 3 5 2 3 5 4 5 1 2 3 4 5 4 5 3 4 4 2 4 1 5 5 2 1 4 3 2 1 5 1 3 1 1 4 5 4 1 2 5 3 3 1 5 1 1 2 1 4 5 5 3 4 2 1 3 1 3 5 4 3 3 2 5 3 4 1 2 5 3 2 3 5 1 5 3 4 5 3 4 1 2 5 2 ...
output:
5 1 2 1 3 1 1 1 4 1 1 0 2 1 1 1 3 3 2 2 3 1 2 1 4 0 0 2 1 2 1 3 4 1 4 1 2 1 1 1 3 5 1 3 1 4 1 1 1 2 1 1 6 1 3 1 2 1 1 1 4 1 1 1 3 3 1 2 2 4 1 1 2 3 1 1 1 4 1 2 0 7 2 2 3 1 1 1 2 1 3 2 2 4 1 3 2 4 1 5 1 1 1 2 2 1 3 1 2 1 3 2 1 2 1 1 0 0 0 5 1 2 1 4 2 2...
result:
wrong answer vertex 3 appears twice in step 1