Submission #1055133

# Submission time Handle Problem Language Result Execution time Memory
1055133 2024-08-12T14:45:38 Z j_vdd16 Jail (JOI22_jail) C++17
61 / 100
5000 ms 37976 KB
#include <algorithm>
#include <bitset>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define loop(X, N) for(int X = 0; X < (N); X++)
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;


int n, m;
vvi adj;
vi depth;
vi parent;

constexpr int POW = 20;
vi lift[POW];

vii inOut;
int counter;
void dfs(int node, int p, int d) {
    depth[node] = d;
    lift[0][node] = p;

    inOut[node].first = counter++;

    for (int child : adj[node]) {
        if (child == p) continue;

        parent[child] = node;
        dfs(child, node, d + 1);
    }

    inOut[node].second = counter++;
}

bool isAncestor(int a, int b) {
    return inOut[a].first <= inOut[b].first && inOut[b].first <= inOut[a].second;
}
bool isBetween(int node, int a, int b) {
    return isAncestor(a, node) && isAncestor(node, b);
}
int getLCA(int a, int b) {
    if (depth[a] < depth[b]) swap(a, b);

    int todo = depth[a] - depth[b];
    loop(pow, POW) {
        if (todo & (1 << pow)) {
            a = lift[pow][a];
        }
    }

    if (a == b) return a;

    for (int pow = POW - 1; pow >= 0; pow--) {
        if (lift[pow][a] == lift[pow][b]) continue;

        a = lift[pow][a];
        b = lift[pow][b];
    }

    return lift[0][a];
}
bool isOnSimplePath(int node, int a, int b) {
    int lca = getLCA(a, b);

    bool result = isBetween(node, lca, a) || isBetween(node, lca, b);
    return result;
}

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

    /*
1
2
1 2
2
1 2
2 1
    */

    int q;
    cin >> q;

    while (q--) {
        cin >> n;

        counter = 0;
        adj = vvi(n);
        depth = vi(n);
        parent = vi(n);
        inOut = vii(n);
        loop(i, n - 1) {
            int a, b;
            cin >> a >> b;
            a--; b--;

            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        loop(i, POW) {
            lift[i] = vi(n);
        }

        dfs(0, 0, 0);

        for (int pow = 1; pow < POW; pow++) {
            loop(j, n) {
                int next = lift[pow - 1][j];
                lift[pow][j] = lift[pow - 1][next];
            }
        }

        vii routes;
        cin >> m;
        loop(i, m) {
            int s, t;
            cin >> s >> t;
            s--; t--;

            routes.push_back({s, t});
        }

        vi inDegree(m);
        vector<set<int>> out(m);
        loop(i, m) {
            auto [s1, t1] = routes[i];

            loop(j, m) {
                auto [s2, t2] = routes[j];
                if (i == j) continue;
                
                if (isOnSimplePath(t2, s1, t1)) {
                    if (!out[i].count(j)) {
                        out[i].insert(j);
                        inDegree[j]++;
                    }
                }
                if (isOnSimplePath(s2, s1, t1)) {
                    if (!out[j].count(i)) {
                        out[j].insert(i);
                        inDegree[i]++;
                    }
                }
            }

        }

        stack<int> startingPoints;
        loop(i, m) {
            if (inDegree[i] == 0) startingPoints.push(i);
        }

        int count = 0;
        vb vis(m);
        while (startingPoints.size()) {
            int node = startingPoints.top();
            startingPoints.pop();

            //if (vis[node]) continue;
            //vis[node] = true;

            count++;
            for (int child : out[node]) {
                inDegree[child]--;
                if (inDegree[child] == 0) startingPoints.push(child);
            }
        }

        if (count == m) {
            cout << "Yes" << endl;
        }
        else {
            cout << "No" << endl;
        }
    }
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 10 ms 348 KB Output is correct
5 Correct 16 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 18 ms 736 KB Output is correct
9 Correct 291 ms 8808 KB Output is correct
10 Correct 39 ms 31528 KB Output is correct
11 Correct 7 ms 348 KB Output is correct
12 Correct 162 ms 884 KB Output is correct
13 Execution timed out 5053 ms 37976 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 600 KB Output is correct
29 Correct 9 ms 604 KB Output is correct
30 Correct 6 ms 392 KB Output is correct
31 Correct 6 ms 604 KB Output is correct
32 Correct 3 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 10 ms 348 KB Output is correct
35 Correct 17 ms 604 KB Output is correct
36 Correct 9 ms 348 KB Output is correct
37 Correct 6 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 600 KB Output is correct
29 Correct 9 ms 604 KB Output is correct
30 Correct 6 ms 392 KB Output is correct
31 Correct 6 ms 604 KB Output is correct
32 Correct 3 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 10 ms 348 KB Output is correct
35 Correct 17 ms 604 KB Output is correct
36 Correct 9 ms 348 KB Output is correct
37 Correct 6 ms 348 KB Output is correct
38 Correct 259 ms 9744 KB Output is correct
39 Correct 40 ms 33104 KB Output is correct
40 Correct 302 ms 5896 KB Output is correct
41 Correct 233 ms 2708 KB Output is correct
42 Correct 296 ms 6124 KB Output is correct
43 Correct 23 ms 2620 KB Output is correct
44 Correct 204 ms 1108 KB Output is correct
45 Correct 47 ms 19804 KB Output is correct
46 Correct 42 ms 19796 KB Output is correct
47 Correct 41 ms 23372 KB Output is correct
48 Correct 40 ms 23388 KB Output is correct
49 Correct 41 ms 19956 KB Output is correct
50 Correct 43 ms 20060 KB Output is correct
51 Correct 25 ms 20892 KB Output is correct
52 Correct 25 ms 21084 KB Output is correct
53 Correct 179 ms 2000 KB Output is correct
54 Correct 51 ms 19820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 7 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 8 ms 556 KB Output is correct
13 Correct 117 ms 1068 KB Output is correct
14 Correct 60 ms 1228 KB Output is correct
15 Correct 88 ms 1116 KB Output is correct
16 Execution timed out 5069 ms 21332 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 10 ms 348 KB Output is correct
5 Correct 16 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 18 ms 736 KB Output is correct
9 Correct 291 ms 8808 KB Output is correct
10 Correct 39 ms 31528 KB Output is correct
11 Correct 7 ms 348 KB Output is correct
12 Correct 162 ms 884 KB Output is correct
13 Execution timed out 5053 ms 37976 KB Time limit exceeded
14 Halted 0 ms 0 KB -