Submission #994212

# Submission time Handle Problem Language Result Execution time Memory
994212 2024-06-07T09:03:11 Z vjudge1 Jail (JOI22_jail) C++17
49 / 100
5000 ms 494016 KB
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

vector<vector<int>> graph;
vector<int> cur_path;

bool dfs(int const cur, int const par, int const goal) {
    if (cur == goal) {
        cur_path.push_back(cur);
        return true;
    }

    for (int const& neigh : graph[cur]) {
        if (neigh == par)
            continue;

        if (dfs(neigh, cur, goal)) {
            cur_path.push_back(cur);
            return true;
        }
    }

    return false;
}

bool solve() {
    int n;
    cin >> n;

    graph.assign(n, {});

    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;

        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    int m;
    cin >> m;

    vector<int> start(m);
    vector<int> end(m);
    vector<vector<int>> path(m);

    for (int i = 0; i < m; i++) {
        cin >> start[i] >> end[i];
        start[i]--; end[i]--;

        cur_path.clear();
        dfs(start[i], -1, end[i]);

        path[i] = move(cur_path);
    }

    vector<char> ended(m);
    vector<char> good(m);
    vector<vector<int>> in_path(n);

    for (int iter = 0; iter < m; iter++) {
        good.assign(m, true);
        in_path.assign(n, {});

        for (int i = 0; i < m; i++) {
            if (ended[i]) {
                good[i] = false;
                continue;
            }

            for (int const& v : path[i])
                in_path[v].push_back(i);
        }

        for (int i = 0; i < m; i++) {
            if (ended[i])
                continue;

            for (int const& j : in_path[start[i]])
                if (i != j)
                    good[j] = false;

            if (in_path[end[i]].size() > 1)
                good[i] = false;
        }

        int const good_i = find(good.begin(), good.end(), true) - good.begin();

        if (good_i == m)
            return false;

        ended[good_i] = true;
    }

    return true;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--)
        cout << (solve() ? "Yes\n" : "No\n");
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 8 ms 472 KB Output is correct
5 Correct 16 ms 476 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 9 ms 628 KB Output is correct
9 Correct 3439 ms 28544 KB Output is correct
10 Execution timed out 5072 ms 493968 KB Time limit exceeded
11 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 488 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 1 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 488 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 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 344 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 2 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 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 488 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 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 344 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 2 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 348 KB Output is correct
29 Correct 9 ms 604 KB Output is correct
30 Correct 2 ms 508 KB Output is correct
31 Correct 3 ms 536 KB Output is correct
32 Correct 5 ms 348 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 4 ms 348 KB Output is correct
35 Correct 7 ms 344 KB Output is correct
36 Correct 3 ms 348 KB Output is correct
37 Correct 3 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 488 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 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 344 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 2 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 348 KB Output is correct
29 Correct 9 ms 604 KB Output is correct
30 Correct 2 ms 508 KB Output is correct
31 Correct 3 ms 536 KB Output is correct
32 Correct 5 ms 348 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 4 ms 348 KB Output is correct
35 Correct 7 ms 344 KB Output is correct
36 Correct 3 ms 348 KB Output is correct
37 Correct 3 ms 348 KB Output is correct
38 Correct 3497 ms 28596 KB Output is correct
39 Execution timed out 5062 ms 494016 KB Time limit exceeded
40 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 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 5 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 3 ms 348 KB Output is correct
13 Correct 24 ms 504 KB Output is correct
14 Correct 52 ms 348 KB Output is correct
15 Correct 46 ms 348 KB Output is correct
16 Execution timed out 5054 ms 8060 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 8 ms 472 KB Output is correct
5 Correct 16 ms 476 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 9 ms 628 KB Output is correct
9 Correct 3439 ms 28544 KB Output is correct
10 Execution timed out 5072 ms 493968 KB Time limit exceeded
11 Halted 0 ms 0 KB -