QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#304594#5088. Two Choreographieswarner1129WA 38ms19816kbC++204.7kb2024-01-13 21:33:092024-01-13 21:33:09

Judging History

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

  • [2024-01-13 21:33:09]
  • 评测
  • 测评结果:WA
  • 用时:38ms
  • 内存:19816kb
  • [2024-01-13 21:33:09]
  • 提交

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);
    vector<bool> vis(n);
    auto dfs = [&](auto self, int u, int f) -> void {
        vis[u] = 1;
        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 (!vis[i]) {
            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;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3572kb

input:

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

output:

3
2 1 5 
2 1 3 

result:

ok 

Test #3:

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

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:

4
4 3 1 2 
6 5 2 1 

result:

ok 

Test #4:

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

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
13 4 2 
7 5 11 

result:

ok 

Test #5:

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

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:

5
48 39 7 1 119 
48 39 7 1 114 

result:

ok 

Test #6:

score: 0
Accepted
time: 3ms
memory: 4416kb

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:

7
711 320 5791 479 7707 42 7178 
602 242 284 2325 314 4274 399 

result:

ok 

Test #7:

score: 0
Accepted
time: 35ms
memory: 19816kb

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:

23
15095 30660 4903 76986 11743 46207 3594 22313 10591 10246 8898 2764 43628 10655 5428 5754 8745 82174 5288 6606 1256 6036 51472 
14003 7702 64252 11902 10033 22483 446 34344 8669 5309 6827 96498 7078 74590 10232 4591 57501 6120 67209 5392 7119 4069 58311 

result:

ok 

Test #8:

score: 0
Accepted
time: 38ms
memory: 19780kb

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:

6
10589 7666 6859 35358 9196 10402 
8544 6160 1461 29348 4654 37226 

result:

ok 

Test #9:

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

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:

3
1 2 3
1 6 7

result:

wrong answer Wrong output - Nonexisting edge.