Submission #564433

# Submission time Handle Problem Language Result Execution time Memory
564433 2022-05-19T08:01:47 Z hoanghq2004 Tropical Garden (IOI11_garden) C++14
100 / 100
2832 ms 49812 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include "garden.h"
#include "gardenlib.h"

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 3e5 + 10;

vector <pair <int, int>> e[N];
vector <int> adj[N];
int to[N], root[N], deg[N], pos[N];
int in[N], out[N], comp[N];
int d[N], sz[N], flag[N];

int IsAnc(int u, int v) {
    return in[u] <= in[v] && out[v] <= out[u];
}
vector <int> s[N];

//void answer(int x) {
//    cout << x << '\n';
//}

void count_routes(int n, int m, int p, int r[][2], int q, int g[]) {
    vector <pair <int, int>> edges;
    for (int i = 0; i < m; ++i) {
        e[r[i][0]].push_back({r[i][1], i << 1});
        e[r[i][1]].push_back({r[i][0], i << 1 ^ 1});
        edges.push_back({r[i][0], r[i][1]});
        edges.push_back({r[i][1], r[i][0]});
    }
    memset(root, -1, sizeof(root));
    for (int i = 0; i < m * 2; ++i) {
        auto [u, v] = edges[i];
        to[i] = -1;
        for (auto [w, j]: e[v]) {
            if (i / 2 == j / 2) continue;
            to[i] = j;
            break;
        }
        if (to[i] == -1) to[i] = e[v][0].second;
    }
    for (int i = 0; i < m * 2; ++i) ++deg[to[i]];
    queue <int> Q;
    for (int i = 0; i < m * 2; ++i)
        if (!deg[i]) Q.push(i);
    while (Q.size()) {
        int v = Q.front();
        Q.pop();
        flag[v] = 1;
        int u = to[v];
        adj[u].push_back(v);
        if (!--deg[u]) Q.push(u);
    }
    int ti = 0, cnt = 0;
    function <void(int)> dfs = [&](int u) {
        in[u] = ++ti;
        comp[u] = cnt;
        if (d[u] == 0) root[u] = u;
        for (auto v: adj[u]) {
            root[v] = root[u];
            d[v] = d[u] + 1;
            dfs(v);
        }
        out[u] = ti;
    };
//    cout << edges[9].first << ' ' << edges[9].second << "sdfdsf\n";
//    for (int i = 0; i < m * 2; ++i) cout << adj[i].size() << ' ';
//    for (int i = 0; i < m * 2; ++i) cout << to[i] << ' ';
//    cout << '\n';
    for (int u = 0; u < m * 2; ++u) {
        if (flag[u] || comp[u]) continue;
        int v = u;
        ++cnt;
        int num = 0;
//        cout << u << "aa\n";
        do {
            pos[v] = num++;
            dfs(v);
            v = to[v];
        } while (u != v);
        do {
            sz[v] = num;
            v = to[v];
        } while (u != v);
    }
    vector <int> fin;
    for (int i = 0; i < e[p].size(); ++i) {
        fin.push_back(e[p][i].second);
        if (i == 1) break;
    }
    for (int r = 0; r < n; ++r) {
        int i = e[r][0].second;
        s[comp[i]].push_back(i);
    }
    for (int id = 0; id < q; ++id) {
        int x = g[id];
        int ans = 0;
        for (auto v: fin) {
            for (auto u: s[comp[v]]) {
                if (flag[u] && flag[v]) {
                    if (IsAnc(v, u) && (d[u] - d[v] == x)) ++ans;
                } else {
                    if (flag[v]) continue;
                    int y = x - d[u];
                    if (y < 0) continue;
                    int w = root[u];
                    int T = pos[v] - pos[w];
                    if (T < 0) T += sz[v];
                    if (y % sz[v] == T) ++ans;
                }
            }
        }
        answer(ans);
    }
}
//
//int main() {
//    int n, m, p;
//    cin >> n >> m >> p;
//    int r[m][2];
//    for (int i = 0; i < m; ++i) {
//        int u, v;
//        cin >> u >> v;
//        r[i][0] = u, r[i][1] = v;
//    }
//    int q;
//    cin >> q;
//    int g[q];
//    for (int i = 0; i < q; ++i) cin >> g[i];
//    count_routes(n, m, p, r, q, g);
//}

Compilation message

garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:42:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   42 |         auto [u, v] = edges[i];
      |              ^
garden.cpp:44:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   44 |         for (auto [w, j]: e[v]) {
      |                   ^
garden.cpp:96:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |     for (int i = 0; i < e[p].size(); ++i) {
      |                     ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 22960 KB Output is correct
2 Correct 12 ms 22792 KB Output is correct
3 Correct 11 ms 22868 KB Output is correct
4 Correct 11 ms 22612 KB Output is correct
5 Correct 11 ms 22588 KB Output is correct
6 Correct 13 ms 23044 KB Output is correct
7 Correct 11 ms 22628 KB Output is correct
8 Correct 13 ms 22740 KB Output is correct
9 Correct 15 ms 23516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 22960 KB Output is correct
2 Correct 12 ms 22792 KB Output is correct
3 Correct 11 ms 22868 KB Output is correct
4 Correct 11 ms 22612 KB Output is correct
5 Correct 11 ms 22588 KB Output is correct
6 Correct 13 ms 23044 KB Output is correct
7 Correct 11 ms 22628 KB Output is correct
8 Correct 13 ms 22740 KB Output is correct
9 Correct 15 ms 23516 KB Output is correct
10 Correct 12 ms 22612 KB Output is correct
11 Correct 26 ms 25968 KB Output is correct
12 Correct 56 ms 30576 KB Output is correct
13 Correct 44 ms 32820 KB Output is correct
14 Correct 186 ms 46468 KB Output is correct
15 Correct 201 ms 47904 KB Output is correct
16 Correct 170 ms 43696 KB Output is correct
17 Correct 156 ms 42988 KB Output is correct
18 Correct 60 ms 31272 KB Output is correct
19 Correct 173 ms 46440 KB Output is correct
20 Correct 235 ms 47412 KB Output is correct
21 Correct 160 ms 43952 KB Output is correct
22 Correct 159 ms 42696 KB Output is correct
23 Correct 188 ms 47548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 22960 KB Output is correct
2 Correct 12 ms 22792 KB Output is correct
3 Correct 11 ms 22868 KB Output is correct
4 Correct 11 ms 22612 KB Output is correct
5 Correct 11 ms 22588 KB Output is correct
6 Correct 13 ms 23044 KB Output is correct
7 Correct 11 ms 22628 KB Output is correct
8 Correct 13 ms 22740 KB Output is correct
9 Correct 15 ms 23516 KB Output is correct
10 Correct 12 ms 22612 KB Output is correct
11 Correct 26 ms 25968 KB Output is correct
12 Correct 56 ms 30576 KB Output is correct
13 Correct 44 ms 32820 KB Output is correct
14 Correct 186 ms 46468 KB Output is correct
15 Correct 201 ms 47904 KB Output is correct
16 Correct 170 ms 43696 KB Output is correct
17 Correct 156 ms 42988 KB Output is correct
18 Correct 60 ms 31272 KB Output is correct
19 Correct 173 ms 46440 KB Output is correct
20 Correct 235 ms 47412 KB Output is correct
21 Correct 160 ms 43952 KB Output is correct
22 Correct 159 ms 42696 KB Output is correct
23 Correct 188 ms 47548 KB Output is correct
24 Correct 13 ms 22612 KB Output is correct
25 Correct 52 ms 25916 KB Output is correct
26 Correct 72 ms 30704 KB Output is correct
27 Correct 2366 ms 32860 KB Output is correct
28 Correct 630 ms 46384 KB Output is correct
29 Correct 2626 ms 47524 KB Output is correct
30 Correct 1547 ms 45272 KB Output is correct
31 Correct 1988 ms 44520 KB Output is correct
32 Correct 539 ms 31552 KB Output is correct
33 Correct 585 ms 48240 KB Output is correct
34 Correct 2614 ms 49812 KB Output is correct
35 Correct 2832 ms 45644 KB Output is correct
36 Correct 2404 ms 44512 KB Output is correct
37 Correct 381 ms 49328 KB Output is correct
38 Correct 2317 ms 42056 KB Output is correct