Submission #347853

# Submission time Handle Problem Language Result Execution time Memory
347853 2021-01-13T16:00:48 Z idk321 Tropical Garden (IOI11_garden) C++11
100 / 100
4317 ms 43488 KB
#include "garden.h"
#include "gardenlib.h"

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 150001;
vector<int> adj[N];
int cNum = 1;
int cSize[N][2];
int best[N][2];
int dist[N][2][2];
vector<array<int, 2>> st;
int onCycle[N][2];
int onSt[N][2];
int p;

void dfs(int node, int type)
{
    st.push_back({node, type});
    onSt[node][type] = 1;
    array<int, 2> next = {-1, -1};
    int other = -1;
    int eVal = -1;
    if (type == 0)
    {
        other = adj[node][0];
        eVal = best[node][0];
    } else
    {
        other = adj[node][1];
        eVal = best[node][1];
    }

    if (best[other][0] == eVal && best[other][1] != 0)
    {
        next = {other, 1};
    } else
    {
        next = {other, 0};
    }

    if (cSize[next[0]][next[1]])
    {
        cSize[node][type] = cSize[next[0]][next[1]];
        for (int k = 0; k <= 1; k++)
        {
            if (dist[next[0]][next[1]][k] == -1) dist[node][type][k] = -1;
            else dist[node][type][k] = dist[next[0]][next[1]][k] + 1;
        }
    } else if (onSt[next[0]][next[1]])
    {
        auto it = st.rbegin();
        int siz = 0;
        array<int, 2> good = {-1, -1};
        int cdist = 0;
        //cout << node << " " << type << " " << next[0] << " " << next[1] << endl;
        while (true)
        {
            onCycle[(*it)[0]][(*it)[1]] = 1;
            //cout <<(*it)[0] << " " << (*it)[1] << endl;
            siz++;
            array<int, 2> ar1 = {p, 0};
            array<int, 2> ar2 = {p, 1};
            if (*it == ar1)
            {
                good[0] = cdist;
            }
            if (*it == ar2)
            {
                good[1] = cdist;
            }
            cdist++;
            if (*it == next) break;
            it++;
        }

        //cout <<(*it)[0] << " " << (*it)[1] << endl;
        //cout << "b " << cdist << endl;
        for (int k = 0; k <= 1; k++)
        {
            if (good[k] != -1)
            {
                dist[node][type][k] = siz - good[k];
            }
        }
        cSize[node][type] = siz;
    } else
    {
        dfs(next[0], next[1]);
        cSize[node][type] = cSize[next[0]][next[1]];
        for (int k = 0; k <= 1; k++)
        {
            if (dist[next[0]][next[1]][k] == -1) dist[node][type][k] = -1;
            else dist[node][type][k] = dist[next[0]][next[1]][k] + 1;
        }
    }

    if (node == p) dist[node][type][type] = 0;
    st.pop_back();
    onSt[node][type] = 0;
}

void count_routes(int n, int m, int pa, int R[][2], int Q, int G[])
{

    p = pa;
  for (int i = 0; i < m; i++)
  {
    for (int j = 0; j <= 1; j++)
    {
        if (best[R[i][j]][0] == 0) best[R[i][j]][0] = i + 1;
        else if (best[R[i][j]][1] == 0) best[R[i][j]][1] = i + 1;
    }
    adj[R[i][0]].push_back(R[i][1]);
    adj[R[i][1]].push_back(R[i][0]);
  }

  for (int i = 0; i < N; i++)
  {
        for (int j = 0; j <= 1; j++) for (int l = 0; l <= 1; l++) dist[i][j][l] = -1;
    }


    for (int i = 0; i < n; i++)
    {
        if (!cSize[i][0]) dfs(i, 0);
    }



  for(int i=0; i<Q; i++)
  {
    int sum = 0;
    int cdist = G[i];
    for (int j = 0; j < n; j++)
    {
        //if (i == 0) cout << "a " << cSize[j][0]<< " " << dist[j][0][0] << " " << dist[j][0][1] << " " << j <<  endl;
        for (int k = 0; k <= 1; k++)
        {
            if (dist[j][0][k] != -1)
            {
                if (dist[j][0][k] == cdist) sum++;
                else if (onCycle[p][k] && cdist > dist[j][0][k] && dist[j][0][k] % cSize[j][0] == cdist % cSize[j][0])
                {
                    sum++;
                }
            }
        }
    }
      answer(sum);
  }
}

/*
4 4 0
1 2
2 3
3 1
0 1
20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

*/


# Verdict Execution time Memory Grader output
1 Correct 4 ms 6252 KB Output is correct
2 Correct 4 ms 6252 KB Output is correct
3 Correct 4 ms 6252 KB Output is correct
4 Correct 4 ms 6252 KB Output is correct
5 Correct 4 ms 6252 KB Output is correct
6 Correct 4 ms 6252 KB Output is correct
7 Correct 4 ms 6252 KB Output is correct
8 Correct 4 ms 6252 KB Output is correct
9 Correct 6 ms 6508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6252 KB Output is correct
2 Correct 4 ms 6252 KB Output is correct
3 Correct 4 ms 6252 KB Output is correct
4 Correct 4 ms 6252 KB Output is correct
5 Correct 4 ms 6252 KB Output is correct
6 Correct 4 ms 6252 KB Output is correct
7 Correct 4 ms 6252 KB Output is correct
8 Correct 4 ms 6252 KB Output is correct
9 Correct 6 ms 6508 KB Output is correct
10 Correct 4 ms 6252 KB Output is correct
11 Correct 13 ms 8684 KB Output is correct
12 Correct 24 ms 9836 KB Output is correct
13 Correct 58 ms 40412 KB Output is correct
14 Correct 83 ms 14956 KB Output is correct
15 Correct 79 ms 15188 KB Output is correct
16 Correct 68 ms 13420 KB Output is correct
17 Correct 65 ms 12268 KB Output is correct
18 Correct 25 ms 9196 KB Output is correct
19 Correct 77 ms 14956 KB Output is correct
20 Correct 83 ms 15084 KB Output is correct
21 Correct 75 ms 13292 KB Output is correct
22 Correct 72 ms 12140 KB Output is correct
23 Correct 81 ms 16748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6252 KB Output is correct
2 Correct 4 ms 6252 KB Output is correct
3 Correct 4 ms 6252 KB Output is correct
4 Correct 4 ms 6252 KB Output is correct
5 Correct 4 ms 6252 KB Output is correct
6 Correct 4 ms 6252 KB Output is correct
7 Correct 4 ms 6252 KB Output is correct
8 Correct 4 ms 6252 KB Output is correct
9 Correct 6 ms 6508 KB Output is correct
10 Correct 4 ms 6252 KB Output is correct
11 Correct 13 ms 8684 KB Output is correct
12 Correct 24 ms 9836 KB Output is correct
13 Correct 58 ms 40412 KB Output is correct
14 Correct 83 ms 14956 KB Output is correct
15 Correct 79 ms 15188 KB Output is correct
16 Correct 68 ms 13420 KB Output is correct
17 Correct 65 ms 12268 KB Output is correct
18 Correct 25 ms 9196 KB Output is correct
19 Correct 77 ms 14956 KB Output is correct
20 Correct 83 ms 15084 KB Output is correct
21 Correct 75 ms 13292 KB Output is correct
22 Correct 72 ms 12140 KB Output is correct
23 Correct 81 ms 16748 KB Output is correct
24 Correct 5 ms 6252 KB Output is correct
25 Correct 167 ms 8812 KB Output is correct
26 Correct 236 ms 10092 KB Output is correct
27 Correct 4317 ms 41440 KB Output is correct
28 Correct 1441 ms 16812 KB Output is correct
29 Correct 4140 ms 17056 KB Output is correct
30 Correct 2543 ms 15064 KB Output is correct
31 Correct 2350 ms 13972 KB Output is correct
32 Correct 225 ms 9580 KB Output is correct
33 Correct 1453 ms 16704 KB Output is correct
34 Correct 4145 ms 17036 KB Output is correct
35 Correct 2659 ms 14792 KB Output is correct
36 Correct 2350 ms 13804 KB Output is correct
37 Correct 1217 ms 18796 KB Output is correct
38 Correct 3833 ms 43488 KB Output is correct