Submission #99504

# Submission time Handle Problem Language Result Execution time Memory
99504 2019-03-04T11:32:28 Z naoai Tropical Garden (IOI11_garden) C++14
69 / 100
5000 ms 82592 KB
#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"

using namespace std;

#define fin cin
#define fout cout
//ifstream fin("x.in"); ofstream fout("x.out");

typedef long long i64;
const int nmax = 15e4 + 5;
const int mmax = 2 * 15e4 + nmax + 5;
const int qmax = 2000;
const pair<int, int> nul = {-1, -1};

static vector<int> c[mmax + 1];
static pair<int, int> pe_ciclu[mmax + 1];
static int reprezentant[mmax + 1], h[mmax + 1];

static bool finalok[mmax + 1];

static vector<int> g[mmax + 1], gt[mmax + 1];
static vector<pair<int, int>> gorig[nmax + 1];

static int top, stk[mmax + 1];

static vector<pair<int, int>> qry[mmax + 1];
static int rasp[qmax + 1];

bool cmp (pair<int, int> a, pair<int, int> b) {
    return a.second < b.second;
}

int best (int nod, int indm) {
    if (gorig[nod].size() == 1)
        return gorig[nod][0].second;
    if (gorig[nod][0].second / 2 != indm / 2)
        return gorig[nod][0].second;
    else
        return gorig[nod][1].second;
}

void fa_graf (int N, int &M, int P, int R[][2], int Q, int G[]) {
    for (int i = 0; i < M; ++ i) {
        gorig[R[i][0]].push_back({R[i][1], 2 * i});
        gorig[R[i][1]].push_back({R[i][0], 2 * i + 1});

        if (R[i][1] == P)
            finalok[2 * i] = 1;
        if (R[i][0] == P)
            finalok[2 * i + 1] = 1;
    }

    for (int i = 0; i < N; ++ i) {
        sort(gorig[i].begin(), gorig[i].end(), cmp);
    }

    // muchiile de functie
    for (int i = 0; i < M; ++ i) {
        int find_edge = best(R[i][1], 2 * i);
        g[2 * i].push_back(find_edge);

        find_edge = best(R[i][0], 2 * i + 1);
        g[2 * i + 1].push_back(find_edge);
    }

    // 0 ... 2M - 1 sunt muchiile
    // 2M... 2M + N - 1 sunt nodurile de start

    // adauga noduri suplimentare de start
    for (int i = 0; i < N; ++ i) {
        if (gorig[i].size())
            g[2 * M + i].push_back(gorig[i][0].second);
    }

    M *= 2;
}

void ciclu (int nod, int tata, int ind) {
    c[ind].push_back(nod);
    pe_ciclu[nod] = {ind, c[ind].size() - 1};

    for (auto i : g[nod]) {
        if (pe_ciclu[i] != nul && pe_ciclu[i].first != ind) {
            ciclu(i, nod, ind);
        }
    }
}

int cauta_ciclii (int N) {
    // sortare topologica ca sa scot arborii
    vector<int> grad(N, 0);
    for (int i = 0; i < N; ++ i) {
        for (auto j : g[i])
            ++ grad[j];
    }

    queue<int> q;
    for (int i = 0; i < N; ++ i) {
        if (grad[i] == 0)
            q.push(i);
    }

    while (!q.empty()) {
        int x = q.front();
        q.pop();

        pe_ciclu[x] = {-1, -1};
        for (auto i : g[x]) {
            -- grad[i];
            if (grad[i] == 0)
                q.push(i);
        }
    }

    // determin care sunt ciclii
    int cycles = 0;
    for (int i = 0; i < N; ++ i) {
        if (pe_ciclu[i] != nul && pe_ciclu[i].first == 0) {
            ++ cycles;
            ciclu(i, -1, cycles);
        }
    }
    return cycles;
}

void dfs (int nod, int tata) {
    for (auto i : gt[nod]) {
        if (pe_ciclu[i] == nul && i != tata) {
            reprezentant[i] = reprezentant[nod];
            h[i] = h[nod] + 1;
            dfs(i, nod);
        }
    }
}

void det_arbori (int N, int cnt) {
    for (int i = 0; i < N; ++ i) {
        for (auto j : g[i]) {
            gt[j].push_back(i);
        }
    }

    for (int u = 1; u <= cnt; ++ u) {
        for (auto i : c[u]) {
            reprezentant[i] = i;
            dfs(i, -1);
        }
    }
}

void solve (int nod, int tata) {
    stk[top ++] = nod;
    for (auto i : qry[nod]) {
        rasp[i.second] += finalok[stk[i.first]];
    }

    for (auto i : gt[nod]) {
        if (pe_ciclu[i] == nul && i != tata) {
            solve(i, nod);
        }
    }

    -- top;
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
    fa_graf(N, M, P, R, Q, G);
    int cnt = cauta_ciclii(M + N);
    det_arbori(M + N, cnt);

    for (int i = 0; i < Q; ++ i) {
        for (int j = M; j < M + N; ++ j) {
            // pot ajunge de la j in g[i] pasi la o muchie care da in nodul P?

            if (G[i] >= h[j]) { // intru pe ciclu
                pair<int, int> pos = pe_ciclu[reprezentant[j]];
                int final_node = c[pos.first][(pos.second + G[i] - h[j]) % c[pos.first].size()];

                if (finalok[final_node] == 1)
                    ++ rasp[i];
            } else { // tre sa ridic j cu G[i]
                qry[j].push_back({h[j] - G[i], i});
            }
        }
    }

    for (int i = 1; i <= cnt; ++ i) {
        for (auto j : c[i]) {
            solve(j, -1);
        }
    }

    for(int i = 0; i < Q; i ++)
        answer(rasp[i]);
}
# Verdict Execution time Memory Grader output
1 Correct 45 ms 46492 KB Output is correct
2 Correct 43 ms 46456 KB Output is correct
3 Correct 46 ms 46416 KB Output is correct
4 Correct 55 ms 46224 KB Output is correct
5 Correct 51 ms 46160 KB Output is correct
6 Correct 48 ms 46708 KB Output is correct
7 Correct 46 ms 46172 KB Output is correct
8 Correct 46 ms 46400 KB Output is correct
9 Correct 53 ms 47420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 46492 KB Output is correct
2 Correct 43 ms 46456 KB Output is correct
3 Correct 46 ms 46416 KB Output is correct
4 Correct 55 ms 46224 KB Output is correct
5 Correct 51 ms 46160 KB Output is correct
6 Correct 48 ms 46708 KB Output is correct
7 Correct 46 ms 46172 KB Output is correct
8 Correct 46 ms 46400 KB Output is correct
9 Correct 53 ms 47420 KB Output is correct
10 Correct 45 ms 46240 KB Output is correct
11 Correct 74 ms 51760 KB Output is correct
12 Correct 146 ms 57416 KB Output is correct
13 Correct 136 ms 74880 KB Output is correct
14 Correct 451 ms 79068 KB Output is correct
15 Correct 479 ms 80124 KB Output is correct
16 Correct 414 ms 74448 KB Output is correct
17 Correct 395 ms 72912 KB Output is correct
18 Correct 154 ms 57336 KB Output is correct
19 Correct 459 ms 79088 KB Output is correct
20 Correct 465 ms 80240 KB Output is correct
21 Correct 430 ms 74336 KB Output is correct
22 Correct 398 ms 72844 KB Output is correct
23 Correct 480 ms 81720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 46492 KB Output is correct
2 Correct 43 ms 46456 KB Output is correct
3 Correct 46 ms 46416 KB Output is correct
4 Correct 55 ms 46224 KB Output is correct
5 Correct 51 ms 46160 KB Output is correct
6 Correct 48 ms 46708 KB Output is correct
7 Correct 46 ms 46172 KB Output is correct
8 Correct 46 ms 46400 KB Output is correct
9 Correct 53 ms 47420 KB Output is correct
10 Correct 45 ms 46240 KB Output is correct
11 Correct 74 ms 51760 KB Output is correct
12 Correct 146 ms 57416 KB Output is correct
13 Correct 136 ms 74880 KB Output is correct
14 Correct 451 ms 79068 KB Output is correct
15 Correct 479 ms 80124 KB Output is correct
16 Correct 414 ms 74448 KB Output is correct
17 Correct 395 ms 72912 KB Output is correct
18 Correct 154 ms 57336 KB Output is correct
19 Correct 459 ms 79088 KB Output is correct
20 Correct 465 ms 80240 KB Output is correct
21 Correct 430 ms 74336 KB Output is correct
22 Correct 398 ms 72844 KB Output is correct
23 Correct 480 ms 81720 KB Output is correct
24 Correct 51 ms 46200 KB Output is correct
25 Correct 740 ms 51852 KB Output is correct
26 Correct 1185 ms 57496 KB Output is correct
27 Correct 2399 ms 74912 KB Output is correct
28 Correct 4131 ms 79088 KB Output is correct
29 Correct 4391 ms 80252 KB Output is correct
30 Correct 2903 ms 74520 KB Output is correct
31 Correct 2603 ms 72920 KB Output is correct
32 Correct 1151 ms 58156 KB Output is correct
33 Correct 4211 ms 79088 KB Output is correct
34 Correct 4391 ms 80120 KB Output is correct
35 Correct 2873 ms 74316 KB Output is correct
36 Correct 2582 ms 72860 KB Output is correct
37 Execution timed out 5023 ms 82592 KB Time limit exceeded