답안 #1104801

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1104801 2024-10-24T13:04:52 Z _callmelucian Jail (JOI22_jail) C++14
100 / 100
1615 ms 327208 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;

#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())

const int NODE = 5e6 + 6, mn = 1e5 + 2e4 + 4, LOG = 17;
int up[mn][LOG], vis[NODE], depth[mn], n, m;
vector<int> adj[mn], graph[NODE];

// layer 0: nodes
// layer 1: prisoner
// layer 2: starting points
// layer 3: ending points

int node (int layer, int l2, int u) {
    if (layer == 0) return u;
    if (layer == 1) return m + l2 * n + u;
    if (layer == 2) return m + (l2 + LOG) * n + u;
}

void addEdge (int a, int b) {
    graph[a].push_back(b);
}

void dfs (int u, int p, int d) {
    up[u][0] = p, depth[u] = d;
    for (int i = 1; i < LOG; i++) {
        addEdge(node(1, i - 1, u), node(1, i, u)), addEdge(node(1, i - 1, up[u][i - 1]), node(1, i, u));
        addEdge(node(2, i, u), node(2, i - 1, u)), addEdge(node(2, i, u), node(2, i - 1, up[u][i - 1]));
        up[u][i] = up[up[u][i - 1]][i - 1];
    }

    for (int v : adj[u])
        if (v != p) dfs(v, u, d + 1);
}

int goUp (int a, int k) {
    for (int i = 0; i < LOG; i++)
        if (k & (1 << i)) a = up[a][i];
    return a;
}

int lca (int a, int b) {
    if (depth[a] < depth[b]) swap(a, b);
    a = goUp(a, depth[a] - depth[b]);
    if (a == b) return a;
    for (int i = LOG - 1; i >= 0; i--)
        if (up[a][i] != up[b][i]) a = up[a][i], b = up[b][i];
    return up[a][0];
}

void connect (int pivot, int u, int k, bool from) {
    for (int i = 0; i < LOG; i++) {
        if ((k & (1 << i)) == 0) continue;
        if (from) addEdge(pivot, node(2, i, u));
        else addEdge(node(1, i, u), pivot);
        u = up[u][i];
    }
}

void refresh (int n, int sumNode) {
    for (int i = 0; i <= n; i++) adj[i].clear();
    for (int i = 0; i <= sumNode; i++)
        graph[i].clear(), vis[i] = 0;
}

vector<int> line;

bool detectCycle (int u) {
    if (vis[u] == 1) return 1;
    if (vis[u] == 2) return 0;
    vis[u] = 1;
    line.push_back(u);
    for (int v : graph[u])
        if (detectCycle(v)) return vis[u] = 2, 1;
    return vis[u] = 2, 0;
}

bool solve() {
    cin >> n;
    for (int i = 1; i < n; i++) {
        int a, b; cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    cin >> m;
    vector<pii> paths(m + 1);

    for (int i = 1; i <= m; i++)
        cin >> paths[i].first >> paths[i].second;

    // build graph
    dfs(1, 1, 0);
    for (int i = 1; i <= m; i++) {
        int a, b; tie(a, b) = paths[i];
        int lc = lca(a, b);

        // from start point to this path
        connect(node(0, 0, i), up[a][0], depth[a] - depth[lc], 0);
        connect(node(0, 0, i), b, depth[b] - depth[lc], 0);

        // from this path to end point
        connect(node(0, 0, i), up[b][0], depth[b] - depth[lc], 1);
        connect(node(0, 0, i), a, depth[a] - depth[lc], 1);

        addEdge(node(0, 0, i), node(1, 0, a));
        addEdge(node(2, 0, b), node(0, 0, i));
    }

    // detect cycle
    int sumNode = m + 2 * LOG * n;
    for (int i = 1; i <= sumNode; i++)
        if (!vis[i] && detectCycle(i)) return refresh(n, sumNode), 0;

    return refresh(n, sumNode), 1;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int TC; cin >> TC;
    while (TC--) cout << (solve() ? "Yes" : "No") << "\n";

    return 0;
}

Compilation message

jail.cpp: In function 'int node(int, int, int)':
jail.cpp:26:1: warning: control reaches end of non-void function [-Wreturn-type]
   26 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 123728 KB Output is correct
2 Correct 22 ms 123728 KB Output is correct
3 Correct 21 ms 123728 KB Output is correct
4 Correct 43 ms 127436 KB Output is correct
5 Correct 61 ms 130820 KB Output is correct
6 Correct 26 ms 124624 KB Output is correct
7 Correct 25 ms 124624 KB Output is correct
8 Correct 27 ms 124620 KB Output is correct
9 Correct 124 ms 141756 KB Output is correct
10 Correct 913 ms 301036 KB Output is correct
11 Correct 75 ms 123856 KB Output is correct
12 Correct 133 ms 131176 KB Output is correct
13 Correct 1010 ms 305288 KB Output is correct
14 Correct 590 ms 289676 KB Output is correct
15 Correct 681 ms 290244 KB Output is correct
16 Correct 867 ms 299908 KB Output is correct
17 Correct 965 ms 306292 KB Output is correct
18 Correct 851 ms 327208 KB Output is correct
19 Correct 869 ms 306924 KB Output is correct
20 Correct 573 ms 296792 KB Output is correct
21 Correct 574 ms 292796 KB Output is correct
22 Correct 528 ms 288984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 121696 KB Output is correct
2 Correct 52 ms 121672 KB Output is correct
3 Correct 56 ms 122628 KB Output is correct
4 Correct 62 ms 122576 KB Output is correct
5 Correct 59 ms 123084 KB Output is correct
6 Correct 56 ms 123084 KB Output is correct
7 Correct 58 ms 123036 KB Output is correct
8 Correct 57 ms 122984 KB Output is correct
9 Correct 61 ms 123056 KB Output is correct
10 Correct 58 ms 122500 KB Output is correct
11 Correct 55 ms 122628 KB Output is correct
12 Correct 49 ms 122476 KB Output is correct
13 Correct 48 ms 122572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 121696 KB Output is correct
2 Correct 52 ms 121672 KB Output is correct
3 Correct 56 ms 122628 KB Output is correct
4 Correct 62 ms 122576 KB Output is correct
5 Correct 59 ms 123084 KB Output is correct
6 Correct 56 ms 123084 KB Output is correct
7 Correct 58 ms 123036 KB Output is correct
8 Correct 57 ms 122984 KB Output is correct
9 Correct 61 ms 123056 KB Output is correct
10 Correct 58 ms 122500 KB Output is correct
11 Correct 55 ms 122628 KB Output is correct
12 Correct 49 ms 122476 KB Output is correct
13 Correct 48 ms 122572 KB Output is correct
14 Correct 49 ms 123720 KB Output is correct
15 Correct 49 ms 121672 KB Output is correct
16 Correct 47 ms 122596 KB Output is correct
17 Correct 28 ms 125264 KB Output is correct
18 Correct 54 ms 122588 KB Output is correct
19 Correct 46 ms 121672 KB Output is correct
20 Correct 53 ms 122504 KB Output is correct
21 Correct 58 ms 122572 KB Output is correct
22 Correct 27 ms 124624 KB Output is correct
23 Correct 51 ms 121596 KB Output is correct
24 Correct 45 ms 121928 KB Output is correct
25 Correct 50 ms 122524 KB Output is correct
26 Correct 56 ms 121956 KB Output is correct
27 Correct 60 ms 123088 KB Output is correct
28 Correct 22 ms 123728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 121696 KB Output is correct
2 Correct 52 ms 121672 KB Output is correct
3 Correct 56 ms 122628 KB Output is correct
4 Correct 62 ms 122576 KB Output is correct
5 Correct 59 ms 123084 KB Output is correct
6 Correct 56 ms 123084 KB Output is correct
7 Correct 58 ms 123036 KB Output is correct
8 Correct 57 ms 122984 KB Output is correct
9 Correct 61 ms 123056 KB Output is correct
10 Correct 58 ms 122500 KB Output is correct
11 Correct 55 ms 122628 KB Output is correct
12 Correct 49 ms 122476 KB Output is correct
13 Correct 48 ms 122572 KB Output is correct
14 Correct 49 ms 123720 KB Output is correct
15 Correct 49 ms 121672 KB Output is correct
16 Correct 47 ms 122596 KB Output is correct
17 Correct 28 ms 125264 KB Output is correct
18 Correct 54 ms 122588 KB Output is correct
19 Correct 46 ms 121672 KB Output is correct
20 Correct 53 ms 122504 KB Output is correct
21 Correct 58 ms 122572 KB Output is correct
22 Correct 27 ms 124624 KB Output is correct
23 Correct 51 ms 121596 KB Output is correct
24 Correct 45 ms 121928 KB Output is correct
25 Correct 50 ms 122524 KB Output is correct
26 Correct 56 ms 121956 KB Output is correct
27 Correct 60 ms 123088 KB Output is correct
28 Correct 22 ms 123728 KB Output is correct
29 Correct 57 ms 122572 KB Output is correct
30 Correct 56 ms 122448 KB Output is correct
31 Correct 39 ms 122164 KB Output is correct
32 Correct 43 ms 122588 KB Output is correct
33 Correct 46 ms 122592 KB Output is correct
34 Correct 26 ms 124580 KB Output is correct
35 Correct 44 ms 122336 KB Output is correct
36 Correct 27 ms 124620 KB Output is correct
37 Correct 25 ms 124620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 121696 KB Output is correct
2 Correct 52 ms 121672 KB Output is correct
3 Correct 56 ms 122628 KB Output is correct
4 Correct 62 ms 122576 KB Output is correct
5 Correct 59 ms 123084 KB Output is correct
6 Correct 56 ms 123084 KB Output is correct
7 Correct 58 ms 123036 KB Output is correct
8 Correct 57 ms 122984 KB Output is correct
9 Correct 61 ms 123056 KB Output is correct
10 Correct 58 ms 122500 KB Output is correct
11 Correct 55 ms 122628 KB Output is correct
12 Correct 49 ms 122476 KB Output is correct
13 Correct 48 ms 122572 KB Output is correct
14 Correct 49 ms 123720 KB Output is correct
15 Correct 49 ms 121672 KB Output is correct
16 Correct 47 ms 122596 KB Output is correct
17 Correct 28 ms 125264 KB Output is correct
18 Correct 54 ms 122588 KB Output is correct
19 Correct 46 ms 121672 KB Output is correct
20 Correct 53 ms 122504 KB Output is correct
21 Correct 58 ms 122572 KB Output is correct
22 Correct 27 ms 124624 KB Output is correct
23 Correct 51 ms 121596 KB Output is correct
24 Correct 45 ms 121928 KB Output is correct
25 Correct 50 ms 122524 KB Output is correct
26 Correct 56 ms 121956 KB Output is correct
27 Correct 60 ms 123088 KB Output is correct
28 Correct 22 ms 123728 KB Output is correct
29 Correct 57 ms 122572 KB Output is correct
30 Correct 56 ms 122448 KB Output is correct
31 Correct 39 ms 122164 KB Output is correct
32 Correct 43 ms 122588 KB Output is correct
33 Correct 46 ms 122592 KB Output is correct
34 Correct 26 ms 124580 KB Output is correct
35 Correct 44 ms 122336 KB Output is correct
36 Correct 27 ms 124620 KB Output is correct
37 Correct 25 ms 124620 KB Output is correct
38 Correct 132 ms 140464 KB Output is correct
39 Correct 910 ms 301092 KB Output is correct
40 Correct 144 ms 132356 KB Output is correct
41 Correct 200 ms 146844 KB Output is correct
42 Correct 105 ms 143352 KB Output is correct
43 Correct 221 ms 147976 KB Output is correct
44 Correct 86 ms 125392 KB Output is correct
45 Correct 1056 ms 279460 KB Output is correct
46 Correct 1382 ms 293860 KB Output is correct
47 Correct 1097 ms 294528 KB Output is correct
48 Correct 1106 ms 293896 KB Output is correct
49 Correct 762 ms 279968 KB Output is correct
50 Correct 1081 ms 294944 KB Output is correct
51 Correct 1065 ms 295708 KB Output is correct
52 Correct 1018 ms 282660 KB Output is correct
53 Correct 132 ms 136116 KB Output is correct
54 Correct 1389 ms 294480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 121672 KB Output is correct
2 Correct 46 ms 121504 KB Output is correct
3 Correct 46 ms 121500 KB Output is correct
4 Correct 42 ms 121672 KB Output is correct
5 Correct 54 ms 123856 KB Output is correct
6 Correct 46 ms 122572 KB Output is correct
7 Correct 46 ms 122464 KB Output is correct
8 Correct 44 ms 121680 KB Output is correct
9 Correct 48 ms 121672 KB Output is correct
10 Correct 43 ms 121940 KB Output is correct
11 Correct 39 ms 121672 KB Output is correct
12 Correct 37 ms 122564 KB Output is correct
13 Correct 71 ms 126148 KB Output is correct
14 Correct 129 ms 138916 KB Output is correct
15 Correct 67 ms 134856 KB Output is correct
16 Correct 1169 ms 279668 KB Output is correct
17 Correct 1139 ms 285044 KB Output is correct
18 Correct 1293 ms 291112 KB Output is correct
19 Correct 1249 ms 296944 KB Output is correct
20 Correct 997 ms 280952 KB Output is correct
21 Correct 1378 ms 297176 KB Output is correct
22 Correct 1236 ms 284660 KB Output is correct
23 Correct 1261 ms 291564 KB Output is correct
24 Correct 1378 ms 301760 KB Output is correct
25 Correct 1296 ms 291288 KB Output is correct
26 Correct 1416 ms 300848 KB Output is correct
27 Correct 1511 ms 325224 KB Output is correct
28 Correct 1077 ms 302328 KB Output is correct
29 Correct 1141 ms 295420 KB Output is correct
30 Correct 1615 ms 307192 KB Output is correct
31 Correct 1300 ms 293964 KB Output is correct
32 Correct 1599 ms 304888 KB Output is correct
33 Correct 1415 ms 303624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 123728 KB Output is correct
2 Correct 22 ms 123728 KB Output is correct
3 Correct 21 ms 123728 KB Output is correct
4 Correct 43 ms 127436 KB Output is correct
5 Correct 61 ms 130820 KB Output is correct
6 Correct 26 ms 124624 KB Output is correct
7 Correct 25 ms 124624 KB Output is correct
8 Correct 27 ms 124620 KB Output is correct
9 Correct 124 ms 141756 KB Output is correct
10 Correct 913 ms 301036 KB Output is correct
11 Correct 75 ms 123856 KB Output is correct
12 Correct 133 ms 131176 KB Output is correct
13 Correct 1010 ms 305288 KB Output is correct
14 Correct 590 ms 289676 KB Output is correct
15 Correct 681 ms 290244 KB Output is correct
16 Correct 867 ms 299908 KB Output is correct
17 Correct 965 ms 306292 KB Output is correct
18 Correct 851 ms 327208 KB Output is correct
19 Correct 869 ms 306924 KB Output is correct
20 Correct 573 ms 296792 KB Output is correct
21 Correct 574 ms 292796 KB Output is correct
22 Correct 528 ms 288984 KB Output is correct
23 Correct 52 ms 121696 KB Output is correct
24 Correct 52 ms 121672 KB Output is correct
25 Correct 56 ms 122628 KB Output is correct
26 Correct 62 ms 122576 KB Output is correct
27 Correct 59 ms 123084 KB Output is correct
28 Correct 56 ms 123084 KB Output is correct
29 Correct 58 ms 123036 KB Output is correct
30 Correct 57 ms 122984 KB Output is correct
31 Correct 61 ms 123056 KB Output is correct
32 Correct 58 ms 122500 KB Output is correct
33 Correct 55 ms 122628 KB Output is correct
34 Correct 49 ms 122476 KB Output is correct
35 Correct 48 ms 122572 KB Output is correct
36 Correct 49 ms 123720 KB Output is correct
37 Correct 49 ms 121672 KB Output is correct
38 Correct 47 ms 122596 KB Output is correct
39 Correct 28 ms 125264 KB Output is correct
40 Correct 54 ms 122588 KB Output is correct
41 Correct 46 ms 121672 KB Output is correct
42 Correct 53 ms 122504 KB Output is correct
43 Correct 58 ms 122572 KB Output is correct
44 Correct 27 ms 124624 KB Output is correct
45 Correct 51 ms 121596 KB Output is correct
46 Correct 45 ms 121928 KB Output is correct
47 Correct 50 ms 122524 KB Output is correct
48 Correct 56 ms 121956 KB Output is correct
49 Correct 60 ms 123088 KB Output is correct
50 Correct 22 ms 123728 KB Output is correct
51 Correct 57 ms 122572 KB Output is correct
52 Correct 56 ms 122448 KB Output is correct
53 Correct 39 ms 122164 KB Output is correct
54 Correct 43 ms 122588 KB Output is correct
55 Correct 46 ms 122592 KB Output is correct
56 Correct 26 ms 124580 KB Output is correct
57 Correct 44 ms 122336 KB Output is correct
58 Correct 27 ms 124620 KB Output is correct
59 Correct 25 ms 124620 KB Output is correct
60 Correct 132 ms 140464 KB Output is correct
61 Correct 910 ms 301092 KB Output is correct
62 Correct 144 ms 132356 KB Output is correct
63 Correct 200 ms 146844 KB Output is correct
64 Correct 105 ms 143352 KB Output is correct
65 Correct 221 ms 147976 KB Output is correct
66 Correct 86 ms 125392 KB Output is correct
67 Correct 1056 ms 279460 KB Output is correct
68 Correct 1382 ms 293860 KB Output is correct
69 Correct 1097 ms 294528 KB Output is correct
70 Correct 1106 ms 293896 KB Output is correct
71 Correct 762 ms 279968 KB Output is correct
72 Correct 1081 ms 294944 KB Output is correct
73 Correct 1065 ms 295708 KB Output is correct
74 Correct 1018 ms 282660 KB Output is correct
75 Correct 132 ms 136116 KB Output is correct
76 Correct 1389 ms 294480 KB Output is correct
77 Correct 43 ms 121672 KB Output is correct
78 Correct 46 ms 121504 KB Output is correct
79 Correct 46 ms 121500 KB Output is correct
80 Correct 42 ms 121672 KB Output is correct
81 Correct 54 ms 123856 KB Output is correct
82 Correct 46 ms 122572 KB Output is correct
83 Correct 46 ms 122464 KB Output is correct
84 Correct 44 ms 121680 KB Output is correct
85 Correct 48 ms 121672 KB Output is correct
86 Correct 43 ms 121940 KB Output is correct
87 Correct 39 ms 121672 KB Output is correct
88 Correct 37 ms 122564 KB Output is correct
89 Correct 71 ms 126148 KB Output is correct
90 Correct 129 ms 138916 KB Output is correct
91 Correct 67 ms 134856 KB Output is correct
92 Correct 1169 ms 279668 KB Output is correct
93 Correct 1139 ms 285044 KB Output is correct
94 Correct 1293 ms 291112 KB Output is correct
95 Correct 1249 ms 296944 KB Output is correct
96 Correct 997 ms 280952 KB Output is correct
97 Correct 1378 ms 297176 KB Output is correct
98 Correct 1236 ms 284660 KB Output is correct
99 Correct 1261 ms 291564 KB Output is correct
100 Correct 1378 ms 301760 KB Output is correct
101 Correct 1296 ms 291288 KB Output is correct
102 Correct 1416 ms 300848 KB Output is correct
103 Correct 1511 ms 325224 KB Output is correct
104 Correct 1077 ms 302328 KB Output is correct
105 Correct 1141 ms 295420 KB Output is correct
106 Correct 1615 ms 307192 KB Output is correct
107 Correct 1300 ms 293964 KB Output is correct
108 Correct 1599 ms 304888 KB Output is correct
109 Correct 1415 ms 303624 KB Output is correct
110 Correct 85 ms 134772 KB Output is correct
111 Correct 69 ms 134860 KB Output is correct
112 Correct 850 ms 289500 KB Output is correct
113 Correct 1299 ms 297256 KB Output is correct
114 Correct 487 ms 290488 KB Output is correct
115 Correct 519 ms 295204 KB Output is correct
116 Correct 1343 ms 300604 KB Output is correct
117 Correct 1357 ms 292860 KB Output is correct
118 Correct 1380 ms 294572 KB Output is correct
119 Correct 1153 ms 280072 KB Output is correct
120 Correct 112 ms 136636 KB Output is correct
121 Correct 1457 ms 301204 KB Output is correct
122 Correct 1583 ms 301164 KB Output is correct
123 Correct 1268 ms 301092 KB Output is correct
124 Correct 688 ms 282832 KB Output is correct
125 Correct 891 ms 290728 KB Output is correct
126 Correct 854 ms 297564 KB Output is correct
127 Correct 852 ms 291888 KB Output is correct
128 Correct 949 ms 290484 KB Output is correct
129 Correct 1182 ms 307452 KB Output is correct
130 Correct 936 ms 291240 KB Output is correct