QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#825381#9772. Permutation Routingucup-team4435#RE 0ms3524kbC++233.8kb2024-12-21 18:52:002024-12-21 18:52:01

Judging History

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

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

answer

#include "bits/stdc++.h"


#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define each(x, a) for (auto &x : a)
#define ar array
#define vec vector
#define range(i, n) rep(i, n)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;

int Bit(int mask, int b) { return (mask >> b) & 1; }

template<class T>
bool ckmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool ckmax(T &a, const T &b) {
    if (b > a) {
        a = b;
        return true;
    }
    return false;
}

// [l, r)
template<typename T, typename F>
T FindFirstTrue(T l, T r, const F &predicat) {
    --l;
    while (r - l > 1) {
        T mid = l + (r - l) / 2;
        if (predicat(mid)) {
            r = mid;
        } else {
            l = mid;
        }
    }
    return r;
}


template<typename T, typename F>
T FindLastFalse(T l, T r, const F &predicat) {
    return FindFirstTrue(l, r, predicat) - 1;
}

const int INFi = 2e9;
const ll INF = 2e18;


const int N = 1e3 + 5;

int p[N];
vector<pi> g[N];
int nx[N];
bool was[N];

vector<ar<int, 3>> edges;
void dfs(int v, int par) {
    nx[v] = -1;
    if (was[p[v]]) {
        nx[v] = par;
    }
    was[v] = true;
    for(auto &[u, i] : g[v]) {
        if (u == par) continue;
        bool ww = was[p[v]];
        dfs(u, v);
        if (was[p[v]] != ww) {
            nx[v] = u;
        }
    }
    if (!was[p[v]]) {
        assert(par != -1);
        nx[v] = par;
    }
    if (nx[v] == -1) {
        assert(v == p[v]);
    }
}

int n;
vector<vi> ans;
vi ansp;
bool check() {
    bool ok = true;
    for(int i = 1; i <= n; ++i) ok &= (p[i] == i);
    if (ok) return true;
    dfs(1, -1);
    for(auto &[u, v, i] : edges) {
        if (nx[v] == u && nx[u] == v) {
            ansp.push_back(i);
            swap(p[u], p[v]);
            continue;
        }
        if (nx[v] == u && (nx[u] == -1 && p[u] == u)) {
            ansp.push_back(i);
            swap(p[u], p[v]);
            continue;
        }
        if (nx[u] == v && (nx[v] == -1 && p[v] == v)) {
            ansp.push_back(i);
            swap(p[u], p[v]);
            continue;
        }
    }

    int k = ansp.size();
    ansp.push_back(k);
    std::rotate(ansp.begin(), ansp.end() - 1, ansp.end());
    ans.push_back(ansp);
    ansp.clear();
    for(int i = 1; i <= n; ++i) {
        nx[i] = 0;
        was[i] = false;
    }
    return false;
}

void solve() {
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        cin >> p[i];
    }
    rep(_, n - 1) {
        int u, v; cin >> u >> v;
        g[u].push_back({v, _});
        g[v].push_back({u, _});
        edges.push_back({u, v, _});
    }
    int q = 0;
    while (!check()) {
        q++;
        assert(q <= 3 * n);
    }
//    cerr << q << '\n';
    assert(ans.size() == q);
    cout << ans.size() << '\n';
    for(auto &v : ans) {
        v[0]--;
        for(auto &x : v) cout << x + 1 << ' ';
        cout << '\n';
    }
    ans.clear();
    for(int i = 1; i <= n; ++i) {
        g[i].clear();
        p[i] = 0;
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(12) << fixed;
    int t = 1;
    cin >> t;
    rep(i, t) {
        solve();
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3524kb

input:

1
5
1 4 2 5 3
1 2
2 3
2 4
1 5

output:

3
2 3 4 
1 1 
2 2 4 

result:

ok ok, up to 3 steps were used

Test #2:

score: -100
Runtime Error

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:


result: