QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#828205#9772. Permutation Routingucup-team6225RE 6ms3936kbC++174.2kb2024-12-23 14:37:212024-12-23 14:37:21

Judging History

你现在查看的是最新测评结果

  • [2025-01-10 11:38:24]
  • hack成功,自动添加数据
  • (/hack/1438)
  • [2024-12-23 14:37:21]
  • 评测
  • 测评结果:RE
  • 用时:6ms
  • 内存:3936kb
  • [2024-12-23 14:37:21]
  • 提交

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]];
        int flag = 0;
        for(auto [y, i] : edge[x])  if(!vis[y] && !sum[y])   {flag = 1; break;}
        if(!flag)   break;
        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]) {
            int pre = num;
            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;
            }
            // assert(num > pre);
            if(num == pre)  ++num;
            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;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3756kb

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: 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 
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: -100
Runtime Error

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:


result: