QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#472323#4329. MPorNPtree0 220ms136196kbC++173.6kb2024-07-11 15:40:122024-07-11 15:40:13

Judging History

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

  • [2024-07-11 15:40:13]
  • 评测
  • 测评结果:0
  • 用时:220ms
  • 内存:136196kb
  • [2024-07-11 15:40:12]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 3e5 + 5;

struct Edge {
    int a, b, x, y, v;
} E[N];

vector<int> G[N];
int n, dfn[N], low[N], inS[N], wh[N];
stack<int> S;

void Tarjan(int x, int fa = -1) {
    dfn[x] = low[x] = ++dfn[0], inS[x] = 1;
    S.push(x);
    for (auto v : G[x]) {
        if (v == fa) fa = -1;
        else {
            if (!dfn[v]) {
                Tarjan(v, x);
                low[x] = min(low[x], low[v]);
            } else low[x] = min(low[x], dfn[v]);
        }
    }
    if (low[x] == dfn[x]) {
        int v;
        do {
            v = S.top(), S.pop(), inS[v] = 0;
            wh[v] = x;
        } while (v != x);
    }
}

void dfs(int l, int r, int L, int R) {
    if (l > r || L == R) return;
    int mid = (L + R) >> 1, p = r;
    for (int i = r; i >= l; --i) {
        if (E[i].v > mid) swap(E[i], E[p--]);
    }
    for (int i = l; i <= p; ++i) {
        G[E[i].x].push_back(E[i].y), G[E[i].y].push_back(E[i].x);
    }
    for (int i = l; i <= p; ++i) {
        if (!dfn[E[i].x]) Tarjan(E[i].x);
        if (!dfn[E[i].y]) Tarjan(E[i].y);
    }
    for (int i = p; i >= l; --i) {
        if (wh[E[i].x] != wh[E[i].y]) swap(E[i], E[p--]);
    }
    for (int i = p + 1; i <= r; ++i) {
        if (wh[E[i].x]) E[i].x = wh[E[i].x];
        if (wh[E[i].y]) E[i].y = wh[E[i].y];
        E[i].v = mid + 1;
    }
    for (int i = l; i <= r; ++i) {
        G[E[i].x].clear(), G[E[i].y].clear();
        dfn[E[i].x] = dfn[E[i].y] = wh[E[i].x] = wh[E[i].y] = 0;
    }
    dfn[0] = 0;
    dfs(l, p, L, mid), dfs(p + 1, r, mid + 1, R);
}

vector< pair<int, int> > ad[N];
int fa[N << 1], fat[20][N << 1], val[20][N << 1], dep[N << 1];

int find(int x) {
    return (fa[x] == x ? x : fa[x] = find(fa[x]));
}

signed main() {
    int m; scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; ++i) {
        scanf("%d%d", &E[i].x, &E[i].y);
        E[i].a = E[i].x, E[i].b = E[i].y, E[i].v = i;
    }
    dfs(1, m, 1, m + 1);
    for (int i = 1; i <= m; ++i) {
        ad[E[i].v].push_back(make_pair(E[i].a, E[i].b));
    }
    for (int i = 1; i <= n; ++i) fa[i] = i;
    int tot = n;
    for (int i = 1; i <= m; ++i) {
        for (auto [x, y] : ad[i]) {
            if (find(x) != find(y)) {
                x = find(x), y = find(y), ++tot;
                val[0][x] = val[0][y] = i;
                fat[0][x] = fat[0][y] = fa[x] = fa[y] = fa[tot] = tot;
            }
        }
    }
    for (int i = 1; i < 20; ++i) {
        for (int j = tot; j; --j) {
            fat[i][j] = fat[i - 1][fat[i - 1][j]];
            val[i][j] = max(val[i - 1][j], val[i - 1][fat[i - 1][j]]);
        }
    }
    for (int i = tot; ~i; --i) {
        dep[i] = dep[fat[0][i]] + 1;
    }
    int q; scanf("%d", &q);
    for (int i = 1, x, y; i <= q; ++i) {
        scanf("%d%d", &x, &y);
        if (find(x) != find(y)) {
            puts("-1");
        } else {
            if (dep[x] < dep[y]) swap(x, y);
            int res = 0;
            for (int j = 19; ~j; --j) {
                if (dep[fat[j][x]] >= dep[y]) {
                    res = max(res, val[j][x]);
                    x = fat[j][x];
                }
            }
            if (x != y) {
                for (int j = 19; ~j; --j) {
                    if (fat[j][x] != fat[j][y]) {
                        res = max(res, max(val[j][x], val[j][y]));
                        x = fat[j][x], y = fat[j][y];
                    }
                }
                res = max(res, max(val[0][x], val[0][y]));
            }
            printf("%d\n", res);
        }
    }
    return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 10
Accepted
time: 0ms
memory: 99996kb

input:

2 0
1
1 2

output:

-1

result:

ok single line: '-1'

Test #2:

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

input:

2 1
1 2
1
1 2

output:

-1

result:

ok single line: '-1'

Test #3:

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

input:

2 2
1 2
1 2
1
1 2

output:

2

result:

ok single line: '2'

Test #4:

score: 0
Accepted
time: 20ms
memory: 115352kb

input:

286524 0
1
202914 240681

output:

-1

result:

ok single line: '-1'

Test #5:

score: -10
Wrong Answer
time: 76ms
memory: 117200kb

input:

700 244650
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60
1 ...

output:

957

result:

wrong answer 1st lines differ - expected: '1374', found: '957'

Subtask #2:

score: 0
Wrong Answer

Test #23:

score: 20
Accepted
time: 10ms
memory: 108552kb

input:

2 2
1 2
1 2
1
1 2

output:

2

result:

ok single line: '2'

Test #24:

score: 0
Accepted
time: 41ms
memory: 113264kb

input:

282511 0
299916
203511 263473
33 36199
85417 282256
41463 66702
26089 112045
52624 109596
97631 189221
112098 264315
152230 239106
118434 88509
193593 148199
57764 125288
248092 64862
7738 150987
189425 258219
117900 129173
157845 121684
39664 265329
55969 219916
226232 202281
273560 226801
88551 26...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 299916 lines

Test #25:

score: -20
Wrong Answer
time: 220ms
memory: 136196kb

input:

131072 262142
1 2
1 2
3 4
3 4
2 3
2 3
5 6
5 6
7 8
7 8
6 7
6 7
4 5
4 5
9 10
9 10
11 12
11 12
10 11
10 11
13 14
13 14
15 16
15 16
14 15
14 15
12 13
12 13
8 9
8 9
17 18
17 18
19 20
19 20
18 19
18 19
21 22
21 22
23 24
23 24
22 23
22 23
20 21
20 21
25 26
25 26
27 28
27 28
26 27
26 27
29 30
29 30
31 32
31...

output:

32769
131073
131073
65537
65537
32769
131073
65537
65537
131073
131073
131073
131073
131073
131073
131073
131073
131073
32769
131073
65537
65537
16385
32769
131073
131073
32769
131073
131073
65537
65537
131073
131073
131073
131073
131073
131073
32769
131073
131073
131073
131073
131073
131073
65537
1...

result:

wrong answer 1st lines differ - expected: '65534', found: '32769'

Subtask #3:

score: 0
Wrong Answer

Test #34:

score: 30
Accepted
time: 7ms
memory: 102272kb

input:

2 0
1
1 2

output:

-1

result:

ok single line: '-1'

Test #35:

score: 0
Accepted
time: 4ms
memory: 104040kb

input:

2 1
1 2
1
1 2

output:

-1

result:

ok single line: '-1'

Test #36:

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

input:

2 2
1 2
1 2
1
1 2

output:

2

result:

ok single line: '2'

Test #37:

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

input:

4775 0
272121
3382 4011
1390 580
2440 2719
4264 2087
4280 90
600 195
990 1184
246 447
2105 3318
143 695
2182 2164
3100 1030
1330 1690
3230 2353
2822 4362
115 3657
2669 4650
1238 1922
1983 2640
1354 1463
3310 2802
2104 1313
750 2171
2601 3618
3727 3904
1463 1230
1060 4265
1853 3431
4472 99
4661 682
3...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 272121 lines

Test #38:

score: -30
Wrong Answer
time: 7ms
memory: 106616kb

input:

100 4950
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60
1 61...

output:

79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
79
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
156
1...

result:

wrong answer 1st lines differ - expected: '100', found: '79'

Subtask #4:

score: 0
Skipped

Dependency #1:

0%