QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#186489#5545. Contingency PlanNYCU_YamadaWA 1ms6592kbC++203.6kb2023-09-23 23:09:492023-09-23 23:09:50

Judging History

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

  • [2023-09-23 23:09:50]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:6592kb
  • [2023-09-23 23:09:49]
  • 提交

answer

#ifndef Yamada
#define Yamada
#include Yamada __FILE__ Yamada

const int MAXN = 1e5+5;

vector<pii> e[MAXN];
int n, C;
bitset<MAXN> label;
int head[MAXN], connect[MAXN];
void dfs(int x, int p) {
    for (auto it : e[x]) {
        int to = it.X, eid = it.Y;
        if (to == p) continue;
        head[eid] = to;
        dfs(to, x);
    }
}
pair<bool, vector<pii>> cal(int root) {
    label.reset();
    vector<pii> ret;
    label[root] = 1;
    for (int i = 1; i <= n; ++i) head[i] = 0;
    for (int i = 0; i < SZ(e[root]); ++i) {
        int to = e[root][i].X, eid = e[root][i].Y;
        label[to] = 1;
        if (!i) {
            C = to;
            for (auto it : e[C]) label[it.X] = 1;
            connect[C] = C;
        }
        else {
            connect[to] = e[root][i-1].X;
        }
        debug(eid, to);
        head[eid] = to;
        dfs(to, root);
    }
    for (int i = 1; i <= n; ++i) {
        if (label[i] == 0) {
            connect[C] = i;
            break;
        }
    }
    if (connect[C] == C) return pair<bool, vector<pii>>(false, ret);

    for (int i = 0; i < n-1; ++i) {
        int cur = head[i];
        debug(i, cur);
        if (connect[cur] == 0) ret.eb(cur, root);
        else ret.eb(cur, connect[cur]);
    }
    return pair<bool, vector<pii>>(true, ret);
}

void solve() {
    cin >> n;
    for (int i = 0; i < n-1; ++i) {
        int u, v; cin >> u >> v;
        e[u].eb(v, i), e[v].eb(u, i);
    }

    for (int i = 1; i <= n; ++i) {
        if (SZ(e[i]) == n-1) {
            cout << -1 << endl;
            return;
        }
    }

    int root = 1;
    pair<bool, vector<pii>> ans = cal(root);
    if (ans.X == false) {
        for (auto it : e[C]) {
            if (it.X == 1) continue;
            root = it.X;
            break;
        }
        assert(root != 1);
        ans = cal(root);
    }
    assert(ans.X == true);
    for (auto it : ans.Y) cout << it.X << ' ' << it.Y << endl;
}

signed main() {
    IOS();
    int t = 1; // cin >> t;
    for (int i=1;i<=t;++i) solve();
    return 0;
}

#else

#ifdef cold66
#define _GLIBCXX_DEBUG 1
#endif
#pragma GCC optimize ("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;

#define int int64_t
// #define double __float80
using pii = pair<int, int>;
template <typename T> using MaxHeap = priority_queue<T>;
template <typename T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;

#define SZ(a) ((int)(a).size())
#define ALL(a) begin(a), end(a)
#define RALL(a) rbegin(a), rend(a)
#define ee emplace
#define eb emplace_back
#define ef emplace_front
#define pb pop_back
#define pf pop_front
#define X first
#define Y second

template <size_t D, typename T> struct Vec : vector<Vec<D-1, T>> {
    template <typename... U> Vec(int n = 0, U ... _u) : vector<Vec<D-1, T>>(n, Vec<D-1, T>(_u...)) {}
};
template <typename T> struct Vec<1, T> : vector<T> {
    Vec(int n = 0, const T& val = T()) : vector<T>(n, val) {}
};

#ifdef local
#define IOS() void()
#define debug(...) \
    fprintf(stderr, "\u001b[33m"), \
    fprintf(stderr, "At [%s], line %d: (%s) = ", __FUNCTION__, __LINE__, #__VA_ARGS__), \
    _do(__VA_ARGS__), \
    fprintf(stderr, "\u001b[0m")
template <typename T> void _do(T &&_t) {cerr << _t << endl;}
template <typename T, typename ...U> void _do(T &&_t, U &&..._u) {cerr << _t << ", ", _do(_u...);}
#else
#define IOS() ios_base::sync_with_stdio(0); cin.tie(0)
#define debug(...) void()
#define endl '\n'
#endif

template <typename U, typename V> bool chmin(U &u, V v) {return u > v ? u = v, 1 : 0;}
template <typename U, typename V> bool chmax(U &u, V v) {return u < v ? u = v, 1 : 0;}

#endif

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 6592kb

input:

7
1 2
3 7
2 4
2 5
1 3
3 6

output:

2 6
7 1
4 1
5 1
3 2
6 1

result:

wrong answer cycle detected