QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#304587#5088. Two Choreographieswarner1129WA 1ms3576kbC++204.6kb2024-01-13 21:30:002024-01-13 21:30:01

Judging History

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

  • [2024-01-13 21:30:01]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3576kb
  • [2024-01-13 21:30:00]
  • 提交

answer

#include <bits/stdc++.h>
 
using namespace std;

template<ranges::range T, class = enable_if_t<!is_convertible_v<T, string_view>>>
istream& operator>>(istream &s, T &&v) { for (auto &&x : v) s >> x; return s; }
template<ranges::range T, class = enable_if_t<!is_convertible_v<T, string_view>>>
ostream& operator<<(ostream &s, T &&v) { for (auto &&x : v) s << x << ' '; return s; }
 
#ifdef LOCAL
template<class... T> void dbg(T... x) { char e{}; ((cerr << e << x, e = ' '), ...); }
#define debug(x...) dbg(#x, '=', x, '\n')
#else
#define debug(...) ((void)0)
#endif
 
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define ff first
#define ss second
 
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using Pt = pair<i64, i64>;
 
template<class T> inline constexpr T inf = numeric_limits<T>::max() / 2;
constexpr int mod = 1e9 + 7, inv2 = (mod + 1) / 2;
constexpr double eps = 1e-9;
 
template<class T> bool chmin(T &a, T b) { return (b < a and (a = b, true)); }
template<class T> bool chmax(T &a, T b) { return (a < b and (a = b, true)); }

Pt operator+(Pt a, Pt b) { return {a.ff + b.ff, a.ss + b.ss}; }
Pt operator-(Pt a, Pt b) { return {a.ff - b.ff, a.ss - b.ss}; }
i64 operator^(Pt a, Pt b) { return a.ff * b.ss - a.ss * b.ff; }
i64 operator*(Pt a, Pt b) { return a.ff * b.ff + a.ss * b.ss; }
i64 cro(Pt a, Pt b, Pt c) { return (b - a) ^ (c - a); }
i64 norm(Pt a) { return a * a; }

struct DSU {
    vector<int> f;
    DSU(int n) : f(n, -1) {}
    int Find(int x) { return f[x] < 0 ? x : f[x] = Find(f[x]); }
    bool Union(int x, int y) {
        x = Find(x);
        y = Find(y);
        if (x == y) return false;
        f[x] = y;
        return true;
    }
};

void solve() {
    int n;
    cin >> n;

    vector<pair<int, int>> edg(n * 2 - 3);
    for (auto &[u, v] : edg) {
        cin >> u >> v;
        u--, v--;
    }

    vector G(n, vector<int>{});
    DSU dsu(n);
    vector<int> res;
    for (int i = 0; i < n * 2 - 3; i++) {
        auto [u, v] = edg[i];
        if (!dsu.Union(u, v)) {
            res.push_back(i);
        } else {
            G[u].push_back(v);
            G[v].push_back(u);
        }
    }

    vector<int> seq, dep(n), pa(n, -1), in(n);
    auto dfs = [&](auto self, int u, int f) -> void {
        if (f != -1) dep[u] = dep[f] + 1;
        in[u] = seq.size();
        seq.push_back(u);
        
        pa[u] = f;
        for (int v : G[u]) if (v != f) {
            self(self, v, u);
        }
    };
    dfs(dfs, 0, -1);
    for (int i = 0; i < n; i++)
        if (pa[i] == -1) {
            dfs(dfs, i, -1);
        }

    const int lgN = __lg(n);
    vector st(lgN + 1, vector<int>(n));
    st[0] = seq;
    auto cmp = [&](int x, int y) {
        return dep[x] < dep[y] ? x : y;
    };
    for (int i = 0; i < lgN; i++)
        for (int j = 0; j + (1 << i) < n; j++)
            st[i + 1][j] = cmp(st[i][j], st[i][j + (1 << i)]);

    auto lca = [&](int x, int y) {
        if (x == y) return x;
        if ((x = in[x] + 1) > (y = in[y] + 1))
            swap(x, y);
        int h = __lg(y - x);
        return pa[cmp(st[h][x], st[h][y - (1 << h)])];
    };
    
    auto dist = [&](int x, int y) {
        return dep[x] + dep[y] - 2 * dep[lca(x, y)];
    };

    pair<int, int> cyc{-1, -1};

    vector<int> deg(n);
    map<int, int> len;
    for (int i : res) {
        auto [u, v] = edg[i];
        int w = dist(u, v);
        if (len[w]) {
            cyc = {i, len[w]};
            break;
        } else {
            len[w] = i;
        }
        deg[u]++, deg[v]++;
    }

    auto getcyc = [&](int a, int b) -> vector<int> {
        int l = lca(a, b);
        vector<int> A, B;
        while (a != l) {
            A.push_back(a + 1);
            a = pa[a];
        }
        while (b != l) {
            B.push_back(b + 1);
            b = pa[b];
        }
        A.push_back(l + 1);
        A.insert(A.end(), rall(B));
        return A;
    };

    if (cyc.ff != -1) {
        auto [a, b] = cyc;
        auto A = getcyc(edg[a].ff, edg[a].ss);
        auto B = getcyc(edg[b].ff, edg[b].ss);
        cout << A.size() << '\n';
        cout << A << '\n' << B << '\n';
    } else {
        if (deg[seq[0]] < deg[seq[n - 1]]) {
            reverse(all(seq));
        }
        cout << 3 << '\n';
        cout << seq[0] + 1 << ' ' << seq[1] + 1 << ' ' << seq[2] + 1 << '\n';
        cout << seq[0] + 1 << ' ' << seq[n - 2] + 1 << ' ' << seq[n - 1] + 1 << '\n';
    }
}
 
signed main() {
    cin.tie(0)->sync_with_stdio(false);
    cin.exceptions(cin.failbit);
    int T = 1;
    // cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}


详细

Test #1:

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

input:

4
1 2
1 3
1 4
2 3
2 4

output:

3
4 3 2
4 2 1

result:

wrong answer Wrong output - Nonexisting edge.