QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#532807#5088. Two ChoreographiesBongoCatEnjoyer#WA 201ms45736kbC++143.3kb2024-08-25 12:14:192024-08-25 12:14:20

Judging History

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

  • [2024-08-25 12:14:20]
  • 评测
  • 测评结果:WA
  • 用时:201ms
  • 内存:45736kb
  • [2024-08-25 12:14:19]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ll long long
#define forn(i, n) for (ll i = 0; i < n; ++i)
#define forr(i, s, n) for (ll i = s; i < n; ++i)
#define fori(i, s, n) for (ll i = s; i > n; --i)
#define fora(i, n) for (auto i: n)
#define vi vector<int>
#define vll vector<ll>
#define prints(n) cout << (n) << " "
#define println(n) cout << (n) << "\n"

const int MOD = 998244353;
// const int MOD = 1e9 + 7;
vector<vi> adj;

vi dep;
vi par;
vector<vi> spt;
vi vis;

vector<pair<int, int>> vp;
vector<vector<pair<int, int>>> cy;


void dfs(int u, int p, int d=0) {
    // cout << u + 1 << ' ' << p + 1 << ' ' << d << '\n';
    if (vis[u]) {
        vp.emplace_back(u, p);
        return;
    }
    dep[u] = d;
    par[u] = p;
    vis[u] = 1;
    for (int v: adj[u]) {
        if (v == p) continue;
        dfs(v, u, d + 1);
    }
}

int lca(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    int jarak = dep[u] - dep[v];
    forn(i, 25) {
        if (jarak & (1 << i)) u = spt[u][i];
    }
    for (int i = 24; i >= 0; i--) {
        if (spt[u][i] != spt[v][i]) {
            u = spt[u][i];
            v = spt[v][i];
        }
    }
    while (u != v) {
        u = par[u];
        v = par[v];
    }
    return u;
}

int jarak (int u, int v) {
    return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}

void procSpt() {
    forn(b, 25) {
        forn(i, adj.size()) {
            if (b) spt[i][b] = spt[spt[i][b - 1]][b - 1];
            else spt[i][b] = par[i];
        }
    }
}

void proc() {
    forn(i, vp.size()) {
        if (vp[i].first > vp[i].second) swap(vp[i].first, vp[i].second);
    }
    sort(vp.begin(), vp.end());
    vp.erase(unique(vp.begin(), vp.end()), vp.end());

    forn(i, vp.size()) {
        int u = vp[i].first, v = vp[i].second;
        int j = jarak(u, v);
        cy[j].push_back({u, v});
    }
}

void printcycle(int u, int v) {
    int _lca = lca(u, v);
    stack<int> rev;
    while (u != _lca) {
        rev.push(u+1);
        u = par[u];
    }
    cout << v + 1<< ' ';
    while (v != _lca) {
        v = par[v];
        cout << v + 1 << ' ';
    }
    while (!rev.empty()) {
        cout << rev.top() << (rev.size() == 1 ? "" : " ");
        rev.pop();
    }
    cout << '\n';
}

void solve() {
    int n;
    cin >> n;
    adj.resize(n);
    dep = vi(n, 0);
    par = vi(n, 0);
    vis = vi(n, 0);
    cy = vector<vector<pair<int, int>>>(n+2, vector<pair<int, int>>());
    spt = vector<vi>(n, vi(25, 0));


    forn(i, 2*n - 3) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    // dfs(0, 0);
    forn(i, n) {
        if (vis[i]) continue;
        dfs(i, i);
    }
    procSpt();
    proc();

    for(int i = cy.size() - 1; i >= 2; i--) {
        if (cy[i].size() > 1) {
            cout << i + 1 << '\n';
            printcycle(cy[i][0].first, cy[i][0].second);
            printcycle(cy[i][1].first, cy[i][1].second);
            return;
        }
    }



    cout << -1 << '\n';
    
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    // cin >> t;
    while (t--) solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3504kb

input:

4
1 2
1 3
1 4
2 3
2 4

output:

3
3 2 1 
4 2 1 

result:

ok 

Test #2:

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

input:

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

output:

3
3 2 1 
5 2 1 

result:

ok 

Test #3:

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

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:

5
3 6 5 2 1 
4 3 6 5 2 

result:

ok 

Test #4:

score: 0
Accepted
time: 1ms
memory: 3580kb

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:

7
36 29 19 4 2 16 1 
24 37 30 14 20 10 8 

result:

ok 

Test #5:

score: 0
Accepted
time: 1ms
memory: 3940kb

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:

27
151 51 167 102 40 154 192 190 23 148 147 141 77 100 21 32 66 43 61 96 16 33 74 58 86 115 19 
51 167 102 40 154 192 190 23 148 147 141 77 100 21 32 66 43 61 96 16 33 74 58 86 115 19 46 

result:

ok 

Test #6:

score: 0
Accepted
time: 8ms
memory: 6516kb

input:

8000
2 1508
2 3068
3 5268
3 5501
6 266
6 2737
6 3197
6 5863
6 6697
7 3492
9 427
9 794
9 3114
9 5509
10 2257
10 4348
11 1479
11 1957
11 2230
11 2500
11 3182
11 4399
11 5051
11 7727
12 7669
13 1403
13 5753
14 2871
14 6956
14 7959
15 6902
17 1630
17 3155
17 5950
18 7232
19 125
19 3280
19 5648
20 6879
2...

output:

974
3173 6455 4930 7315 6176 7911 7331 5904 5192 6760 1218 6605 7956 945 2314 2053 6709 6026 1068 3684 7543 6399 6948 3125 5163 7456 2467 2982 7404 7786 2667 3917 7328 4788 4902 5386 6444 7906 4856 2682 6758 3324 5324 5804 5293 4830 6602 1934 6216 5602 7230 2078 355 6684 1927 7104 2120 1115 3488 613...

result:

ok 

Test #7:

score: 0
Accepted
time: 200ms
memory: 45736kb

input:

99999
1 11261
1 21544
2 9017
2 63063
2 97990
3 11995
3 42473
4 19846
5 38099
6 35872
6 80509
7 73231
8 12356
9 35384
10 45091
12 86727
13 4938
13 48917
14 62383
14 89846
15 28458
15 44277
15 51725
15 84522
16 93258
17 13934
17 42238
18 19000
19 11278
19 23672
19 61502
19 78791
20 85057
20 88080
21 2...

output:

11922
7513 85090 85931 64692 78816 29622 75208 54180 84375 40846 23649 28189 57368 37130 82598 22629 34399 34659 65691 84407 7878 39194 77067 88155 85937 88173 94599 69824 96576 87929 78887 66627 63184 27593 65412 65813 64474 41894 70508 49967 61082 92794 33781 68083 56654 88350 51022 47500 43047 84...

result:

ok 

Test #8:

score: 0
Accepted
time: 201ms
memory: 45660kb

input:

100000
1 68531
2 97359
4 68578
4 83098
4 98443
5 8053
5 30270
5 86617
6 7074
6 12266
6 69396
7 52675
7 78316
7 90757
7 92242
8 32677
8 41353
8 41457
8 74508
9 44234
10 4973
10 38390
10 96049
11 28007
11 68620
13 3016
14 36748
15 8147
15 25110
15 28489
15 72947
15 99347
16 70760
17 12774
17 68407
17 ...

output:

11918
7993 1665 641 1800 3441 1353 48570 25699 13115 64013 15798 16929 65929 36429 57636 9839 170 38583 7611 22743 11825 25797 4197 29062 46815 15542 91561 32141 47040 25388 57102 39214 18886 7331 26488 4974 29843 9113 52226 7306 48359 6532 12429 16835 29771 23961 66084 36137 16738 2110 14868 11364 ...

result:

ok 

Test #9:

score: -100
Wrong Answer
time: 0ms
memory: 3504kb

input:

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

output:

-1

result:

wrong answer Integer -1 violates the range [3, 7]