QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#702519#5088. Two Choreographiestamthegod#WA 4ms17528kbC++233.1kb2024-11-02 16:08:402024-11-02 16:08:41

Judging History

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

  • [2024-11-02 16:08:41]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:17528kb
  • [2024-11-02 16:08:40]
  • 提交

answer

#include<bits/stdc++.h>

#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n;
vector<int> adj[maxN];
int jump[maxN][20];
int depth[maxN];
pair<int, int> edge[maxN];
int mark[maxN];
void ReadInput()
{
    cin >> n;
    for(int i=1; i<=2*n-3; i++)
    {
        int u, v;
        cin >> u >> v;
        edge[i] = {u, v};
    }
    shuffle(edge + 1, edge + 2 * n - 2, rng);
}
void dfs(int u, int par)
{
    for(int v : adj[u])
    {
        if(v == par) continue;
        jump[v][0] = u;
        depth[v] = depth[u] + 1;
        for(int i=1; i<=18; i++)
            jump[v][i] = jump[jump[v][i - 1]][i - 1];
        dfs(v, u);
    }
}
int lca(int u, int v)
{
    if(depth[u] < depth[v]) swap(u, v);
    int k = depth[u] - depth[v];
    for(int i=18; i>=0; i--)
        if(k >> i & 1) u = jump[u][i];
    if(u == v) return u;
    for(int i=18; i>=0; i--)
        if(jump[u][i] != jump[v][i])
        {
            u = jump[u][i];
            v = jump[v][i];
        }
    return jump[u][0];
}
int dist(int u, int v)
{
    return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
int lab[maxN];
int findset(int u)
{
    return lab[u] < 0 ? u : lab[u] = findset(lab[u]);
}
int unite(int u, int v)
{
    int r = findset(u), s = findset(v);
    if(r == s) return false;
    if(lab[r] < lab[s]) swap(r, s);
    lab[r] += lab[s];
    lab[s] = r;
    return true;
}
void go(int x, int y)
{
    vector<int> vc1, vc2;
    int t = lca(x, y);
    while(x != t)
    {
        vc1.pb(x);
        x = jump[x][0];
    }
    while(y != t)
    {
        vc2.pb(y);
        y = jump[y][0];
    }
    vc2.pb(t);
    reverse(vc2.begin(), vc2.end());
    for(int v : vc1)
        cout << v << " ";
    for(int v : vc2)
        cout << v << " ";
    cout << '\n';
}
void print(int x, int y, int res)
{
    cout << res << '\n';
    go(edge[x].fi, edge[x].se);
    go(edge[y].fi, edge[y].se);
}
void Solve()
{
    memset(lab, -1, sizeof lab);
    for(int i=1; i<=2*n-3; i++)
    {
        if(unite(edge[i].fi, edge[i].se))
        {
            adj[edge[i].fi].pb(edge[i].se);
            adj[edge[i].se].pb(edge[i].fi);
            mark[i] = 1;
        }
    }
    dfs(3, 0);
    map<int, int> mp;
    for(int i=1; i<=2*n-3; i++)
    {
        if(mark[i]) continue;
        int len = dist(edge[i].fi, edge[i].se) + 1;
        if(mp[len])
        {
            print(mp[len], i, len);
            return;
        }
        mp[len] = i;
    }
}
#define taskname "sol"
int32_t main()
{
    if (fopen(taskname ".inp", "r"))
    {
        freopen(taskname ".inp", "r", stdin);
        //freopen(taskname ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    //cin >> T;
    for(int itest=1; itest<=T; itest++)
    {
        ReadInput();
        Solve();
    }
}


详细

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 17528kb

input:

4
1 2
1 3
1 4
2 3
2 4

output:


result:

wrong output format Unexpected end of file - int32 expected