QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#702959#5088. Two Choreographiestamthegod#TL 2ms10012kbC++234.0kb2024-11-02 16:51:552024-11-02 16:51:55

Judging History

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

  • [2024-11-02 16:51:55]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:10012kb
  • [2024-11-02 16:51:55]
  • 提交

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;
}
vector<int> 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());
    vector<int> res;
    for(int v : vc1)
        res.pb(v);
    for(int v : vc2)
        res.pb(v);
    return res;
    for(int v : vc1)
        cout << v << " ";
    for(int v : vc2)
        cout << v << " ";
    cout << '\n';
}
void print(int x, int y, int res)
{
    vector<int> path1 = go(edge[x].fi, edge[x].se);
    vector<int> path2 = go(edge[y].fi, edge[y].se);
    vector<int> vc1 = path1, vc2 = path2;
    sort(vc1.begin(), vc1.end());
    sort(vc2.begin(), vc2.end());
    vc1.erase(unique(vc1.begin(), vc1.end()), vc1.end());
    vc2.erase(unique(vc2.begin(), vc2.end()), vc2.end());
    if(res < 3) while(true);
  //  if(vc1 == vc2 || vc1.size() != vc2.size() || vc1.size() != res) while(true);
    cout << res << '\n';
    for(int v : path1) cout << v << " ";
    cout << '\n';
    for(int v : path2)
        cout << v << " ";
    cout << '\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 + 2 * n + 4, 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();
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 10012kb

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: 2ms
memory: 9788kb

input:

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

output:

3
1 3 2 
3 1 4 

result:

ok 

Test #3:

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

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
2 5 6 
1 5 2 

result:

ok 

Test #4:

score: -100
Time Limit Exceeded

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:


result: