QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#828071 | #9772. Permutation Routing | ucup-team6225 | WA | 30ms | 4036kb | C++17 | 4.0kb | 2024-12-23 12:52:46 | 2024-12-23 12:52:59 |
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], book[N], tot, mn, root, vis[N], sum[N], cnt;
vector<pii> edge[N];
vector<int> opr[N * 4], 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) {
for(auto [y, i] : edge[x]) if(y != fa && !vis[y]) {
if(sum[y] && !sum[x] && !book[y] && !book[x]) lin.pb(i), book[x] = book[y] = 1;
Dfs(y, x);
}
}
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) {
for(int i : tmp) book[i] = 0, sum[i] = fr[i] != fr[p[i]];
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;
for(int i : tmp) book[i] = 0;
if(!sum[x]) {
for(auto [y, i] : edge[x]) if(!vis[y] && sum[y]) {
// cerr << num + 1 << " : " << res << " | " << y << " " << p[y] << "\n";
swap(p[x], p[y]), opr[++num].pb(i), book[x] = book[y] = 1;
break;
}
lin.clear(), Dfs(x, 0);
for(int i : lin) opr[num].pb(i), swap(p[u[i]], p[v[i]]);
continue;
}
for(auto [y, i] : edge[x]) if(!vis[y] && fr[y] == fr[p[x]]) {
// cerr << num + 1 << " : " << res << " " << y << " " << p[y] << "\n";
// assert(fr[p[y]] != fr[y]);
opr[++num].pb(i), book[y] = book[x] = 1, swap(p[x], p[y]); break;
}
for(int i : tmp) sum[i] = fr[i] != fr[p[i]];
lin.clear(), Dfs(x, 0);
for(int i : lin) opr[num].pb(i), swap(p[u[i]], p[v[i]]);
}
// cerr << num << "\n";
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3716kb
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: 0
Accepted
time: 6ms
memory: 4012kb
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 4 1 2 1 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 1 2 1 3 2 2 1 2 3 4 1 2 1 3 2 4 1 5 1 1 2 2 3 1 1 1 2 1 3 2 1 2 1 1 0 0 0 5 1 2 2 4 1...
result:
ok ok, up to 7 steps were used
Test #3:
score: 0
Accepted
time: 30ms
memory: 4032kb
input:
10000 10 2 7 5 6 4 8 3 1 10 9 8 10 6 1 5 6 4 7 10 2 6 8 7 6 5 3 9 8 10 4 10 6 1 8 3 9 5 7 2 3 10 9 10 5 10 4 6 10 8 10 4 7 10 4 2 4 1 10 9 7 5 10 3 8 2 6 1 4 10 3 9 3 6 3 7 3 4 3 2 3 5 2 3 8 3 1 10 10 9 5 6 3 4 8 7 2 1 2 6 7 8 5 2 4 9 9 3 3 8 8 5 6 10 4 1 10 2 1 6 7 10 3 4 9 8 5 10 3 3 5 1 3 3 2 3 9...
output:
11 1 5 1 6 2 2 1 1 6 1 7 2 3 4 1 7 3 8 1 4 2 5 9 1 1 1 9 12 1 4 1 6 2 1 8 1 6 1 2 1 7 1 2 1 3 1 5 1 3 1 4 1 9 12 1 6 2 4 7 1 6 1 1 1 5 1 1 1 2 1 9 1 2 1 3 1 8 1 3 18 1 2 1 6 1 7 2 6 3 3 7 5 1 3 6 4 8 3 7 5 9 3 6 4 3 2 7 1 2 6 3 2 7 5 1 6 1 7 2 4 1 4 9...
result:
ok ok, up to 21 steps were used
Test #4:
score: 0
Accepted
time: 27ms
memory: 4036kb
input:
2500 20 19 16 18 14 10 3 6 8 9 20 11 1 13 5 4 17 12 15 7 2 14 9 11 1 7 16 9 5 3 1 16 19 15 4 6 9 7 2 12 15 8 5 20 19 16 9 18 1 1 17 1 15 13 15 9 10 1 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 5 14 6 10 12 14 9 1 13 1 18 15 7 10 7 12 17 4 4 5 16 9 11 10 18 8 13 19 6 20 2 4 3 4 15 11 8 ...
output:
17 2 8 7 1 6 2 19 13 3 6 18 16 1 19 1 6 2 12 13 1 6 1 12 2 3 5 2 1 14 2 4 16 3 18 15 10 2 13 16 2 8 10 1 9 1 3 0 0 42 1 11 1 4 1 6 1 19 1 1 1 13 2 14 10 2 13 11 3 14 10 4 4 13 11 6 9 4 14 4 19 12 4 13 6 9 18 4 14 10 12 5 4 13 11 18 8 5 14 10 4 5 15 5 13 11 9 8 3 4 14...
result:
ok ok, up to 50 steps were used
Test #5:
score: 0
Accepted
time: 20ms
memory: 3920kb
input:
400 50 18 35 3 1 22 2 29 14 27 44 32 4 34 43 33 23 21 48 13 47 46 50 38 12 49 9 11 31 10 8 37 41 42 36 17 15 45 16 24 40 25 30 19 28 7 39 5 20 6 26 45 32 39 28 27 43 29 39 34 40 17 12 8 38 3 23 5 41 40 8 36 43 41 25 19 1 18 43 1 46 28 4 39 11 42 20 44 40 35 43 50 43 15 9 31 20 35 37 22 42 29 30 21 2...
output:
62 3 8 5 9 1 31 1 47 2 35 19 3 43 2 10 3 47 16 21 3 43 3 19 3 39 21 48 4 47 13 3 18 4 30 15 48 28 3 47 33 44 3 43 28 9 3 35 21 44 4 43 2 3 12 3 35 38 21 3 39 2 11 2 43 13 2 47 21 3 43 14 28 3 35 21 44 4 43 4 20 46 4 35 26 21 24 4 47 4 20 34 4 35 27 24 28 3 47 4 44 2 30 28 2...
result:
ok ok, up to 146 steps were used
Test #6:
score: 0
Accepted
time: 20ms
memory: 4000kb
input:
400 50 16 19 35 42 20 8 28 6 25 15 49 31 24 34 10 1 50 33 2 5 26 46 27 13 9 21 23 7 30 29 12 44 18 14 3 40 45 48 43 36 47 4 39 32 37 22 41 38 11 17 13 24 1 43 6 10 12 47 21 47 17 13 2 20 10 8 35 33 5 13 1 20 23 40 13 14 47 9 49 2 42 41 10 42 8 3 27 34 42 29 46 48 46 14 23 42 1 36 26 31 33 14 2 50 7 ...
output:
65 7 6 33 18 34 9 36 2 2 8 11 2 30 7 1 27 1 46 2 22 45 3 13 21 2 4 46 10 33 11 5 13 49 8 45 7 3 46 10 2 3 13 45 11 3 46 29 2 3 13 45 24 4 22 35 2 44 4 13 21 24 19 4 46 42 33 44 3 13 17 45 3 22 16 2 3 26 21 24 3 22 33 48 4 46 21 17 4 4 22 33 23 45 5 46 21 17 12 2 5 22 33 23 45 ...
result:
ok ok, up to 149 steps were used
Test #7:
score: 0
Accepted
time: 13ms
memory: 3932kb
input:
100 100 49 90 5 62 95 42 92 16 74 58 66 85 21 98 15 6 73 13 40 78 44 2 93 11 54 76 80 34 89 96 31 56 67 77 79 39 26 14 23 81 3 69 37 97 83 25 12 22 61 48 17 84 35 47 36 53 7 91 60 100 82 57 4 99 45 28 70 72 59 86 33 64 75 41 94 51 29 46 20 8 24 52 1 43 19 27 38 10 55 68 32 71 65 9 30 88 18 87 63 50 ...
output:
120 12 95 10 65 35 30 52 82 73 7 60 72 33 4 22 2 51 23 2 4 92 1 8 2 37 11 2 8 30 3 68 38 2 3 37 24 6 4 68 80 30 13 5 63 85 49 6 1 4 8 15 12 13 3 68 38 99 3 8 24 6 4 63 38 80 13 3 8 24 45 3 63 38 16 4 68 75 45 52 3 37 16 6 4 63 30 96 32 3 68 49 45 3 8 16 6 3 37 38 59 2 8 30 3 6...
result:
ok ok, up to 298 steps were used
Test #8:
score: -100
Wrong Answer
time: 20ms
memory: 3948kb
input:
100 100 86 35 82 12 83 29 60 50 90 33 87 4 47 51 70 96 48 31 89 84 92 37 55 94 78 80 57 46 6 99 18 74 10 38 2 63 22 34 67 77 72 44 68 42 79 28 13 17 62 8 14 91 97 98 23 66 27 69 100 7 75 49 36 73 88 56 39 43 58 15 85 41 64 32 61 95 40 25 45 26 93 3 5 20 71 1 11 65 19 9 52 21 81 24 76 16 53 54 30 59 ...
output:
142 8 6 10 45 43 19 3 8 7 2 57 82 1 83 2 61 2 2 83 23 2 61 9 3 18 67 44 4 95 39 79 42 4 18 4 60 72 3 22 39 40 3 83 19 1 2 18 9 3 83 73 44 3 22 9 69 3 18 48 44 3 83 74 13 3 61 12 9 3 18 67 64 3 83 74 79 4 18 99 77 9 3 61 74 26 3 18 99 67 4 83 74 68 79 4 22 99 80 35 4 83 87 48 ...
result:
wrong answer Integer parameter [name=number of steps] equals to 317, violates the range [0, 300]