QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#702795#5088. Two Choreographiestamthegod#WA 3ms9852kbC++233.3kb2024-11-02 16:35:192024-11-02 16:35:20

Judging History

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

  • [2024-11-02 16:35:20]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:9852kb
  • [2024-11-02 16:35:19]
  • 提交

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};
    }
}
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()
{
    while(true)
    {
        shuffle(edge + 1, edge + 2 * n - 2, rng);
        fill(lab, lab + n + 1, -1);
        for(int i=1; i<=n; i++)
            adj[i].clear();
        fill(mark, mark + n + 1, 0);
        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(1, 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();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 9780kb

input:

4
1 2
1 3
1 4
2 3
2 4

output:

3
2 1 4 
2 1 3 

result:

ok 

Test #2:

score: 0
Accepted
time: 0ms
memory: 9760kb

input:

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

output:

3
1 2 3 
2 1 5 

result:

ok 

Test #3:

score: 0
Accepted
time: 2ms
memory: 9836kb

input:

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

output:

3
1 5 2 
6 3 1 

result:

ok 

Test #4:

score: 0
Accepted
time: 0ms
memory: 9840kb

input:

40
1 16
1 40
2 4
2 16
2 36
3 25
3 38
4 1
4 13
5 11
5 27
6 4
7 5
7 11
8 10
8 14
8 24
9 34
10 20
12 35
13 2
14 10
14 20
15 18
15 28
15 31
16 6
16 13
17 5
17 11
17 27
18 9
19 1
19 4
19 16
20 24
21 12
21 33
21 35
22 38
23 12
23 21
25 28
25 31
25 34
25 38
26 14
26 20
27 7
27 11
28 3
28 31
29 16
29 19
30 ...

output:

3
2 36 16 
29 16 19 

result:

ok 

Test #5:

score: -100
Wrong Answer
time: 2ms
memory: 9852kb

input:

201
1 7
1 114
1 119
2 49
2 93
4 197
5 139
6 1
6 27
7 39
7 121
8 127
9 130
9 145
11 106
11 136
11 193
12 2
12 116
13 55
13 69
13 105
13 187
13 196
14 144
14 177
15 127
15 134
15 145
15 155
15 184
15 199
16 96
16 177
17 20
21 100
22 68
22 71
22 81
22 142
23 148
23 190
24 12
24 81
24 89
25 158
25 159
2...

output:

1
173 0 134 
154 0 96 

result:

wrong answer Integer 1 violates the range [3, 201]