Submission #238177

# Submission time Handle Problem Language Result Execution time Memory
238177 2020-06-10T06:25:37 Z Ruxandra985 Tropical Garden (IOI11_garden) C++14
100 / 100
2824 ms 31904 KB
#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
#define DIMN 150010
using namespace std;


int f[2][DIMN] , taken[2][DIMN] , poz[2][DIMN] , taken_2[2][DIMN];
int dist[2][DIMN] , far[2][DIMN] , sol[2010];
vector < pair <int,int> > tt[2][DIMN];
pair <int , int> urm[2][DIMN];
int prm[DIMN] , doi[DIMN];
vector <int> v[DIMN];
deque <pair <int,int> > dq;

void calcul (int state , int curr , int n , int q , int g[]){
    int si , ci , dis , i , new_state , vecin , len1;

    for (i = 1 ; i <= n ; i++){
        dist[0][i] = dist[1][i] = taken[0][i] = taken[1][i] = 0;
    }

    si = state;
    ci = curr;

    dis = 0;

    while (!taken[state][curr]){
        taken[state][curr] = ++dis;
        new_state = urm[state][curr].first;
        vecin = urm[state][curr].second;
        state = new_state;
        curr = vecin;
    }
    if (state == si && curr == ci){ /// exista ciclu care il contine pe 0 , p
        len1 = dis; /// in muchii

        for (i = 1 ; i <= n ; i++){

            if (taken[0][i]){

                dist[0][i] = (dis - taken[0][i] + 1) % dis;
                dq.push_back(make_pair(0 , i));

            }

            if (taken[1][i]){

                dist[1][i] = (dis - taken[1][i] + 1) % dis;
                dq.push_back(make_pair(1 , i));

            }

        }

        while (!dq.empty()){

            state = dq.front().first;
            curr = dq.front().second;
            dq.pop_front();

            if (state == 0){
                for (i = 0 ; i < q ; i++){
                    if (dist[state][curr] % len1 == g[i] % len1 && dist[state][curr] <= g[i]){
                        sol[i]++;
                        //if (si == 1)
                          //  printf ("%d %d\n" , state , curr);
                    }
                }
            }

            for (i = 0 ; i < tt[state][curr].size() ; i++){

                new_state = tt[state][curr][i].first;
                vecin = tt[state][curr][i].second;

                if ( taken[new_state][vecin] == 0 ){
                    dist[new_state][vecin] = 1 + dist[state][curr];
                    dq.push_back(make_pair(new_state , vecin));
                }

            }


        }



    }
    else { /// stare curr nu face parte din ciclu

        state = si;
        curr = ci;

        dq.push_back(make_pair(state , curr));
        dist[state][curr] = 1;

        while (!dq.empty()){

            state = dq.front().first;
            curr = dq.front().second;
            dq.pop_front();

            if (state == 0){
                for (i = 0 ; i < q ; i++){
                    if (dist[state][curr] - 1 == g[i])
                        sol[i]++;
                }
            }

            for (i = 0 ; i < tt[state][curr].size() ; i++){

                new_state = tt[state][curr][i].first;
                vecin = tt[state][curr][i].second;

                dist[new_state][vecin] = 1 + dist[state][curr];
                dq.push_back(make_pair(new_state , vecin));

            }


        }

    }



}

void count_routes(int n, int m, int p, int r[][2], int q, int g[]){
    int i , curr , state , vecin , new_state , pc;

    p++;

    for (i = 0 ; i < m ; i++){
        r[i][0]++;
        r[i][1]++;
    }

    for (i = 1 ; i <= n ; i++){
        prm[i] = doi[i] = -1;
        poz[0][i] = poz[1][i] = 0;
    }

    for (i = 0 ; i < m ; i++){

        if (prm[r[i][0]] == -1)
            prm[r[i][0]] = r[i][1];
        else if (doi[r[i][0]] == -1)
            doi[r[i][0]] = r[i][1];

        if (prm[r[i][1]] == -1)
            prm[r[i][1]] = r[i][0];
        else if (doi[r[i][1]] == -1)
            doi[r[i][1]] = r[i][0];
    }

    /// daca in nod ajungi pe muchia prm[nod] , o iei pe doi[nod], daca ea exista
    /// state = 0, esti libera sa o iei pe prm

    for (i = 1 ; i <= n ; i++){

        curr = i;
        state = 0;
        pc = 1;


        while (!poz[state][curr]){

            poz[state][curr] = pc;
            pc++;

            if (state == 0 || doi[curr] == -1){
                vecin = prm[curr];

                if (prm[vecin] == curr && doi[vecin] != -1)
                    new_state = 1;
                else new_state = 0;
            }
            else {
                vecin = doi[curr];

                if (prm[vecin] == curr && doi[vecin] != -1)
                    new_state = 1;
                else new_state = 0;
            }


            /// din state, curr ----> new_state , vecin
            urm[state][curr] = make_pair(new_state , vecin);

            tt[new_state][vecin].push_back(make_pair(state , curr));

            //printf ("%d %d  %d %d\n" , state , curr , new_state , vecin);

            state = new_state;
            curr = vecin;
        }

    }

    calcul (0 , p , n , q , g);

    calcul (1 , p , n , q , g);



    for (i = 0 ; i < q ; i++)
        answer(sol[i]);


}

Compilation message

garden.cpp: In function 'void calcul(int, int, int, int, int*)':
garden.cpp:72:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (i = 0 ; i < tt[state][curr].size() ; i++){
                          ~~^~~~~~~~~~~~~~~~~~~~~~~~
garden.cpp:111:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (i = 0 ; i < tt[state][curr].size() ; i++){
                          ~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 11008 KB Output is correct
2 Correct 10 ms 11008 KB Output is correct
3 Correct 10 ms 11136 KB Output is correct
4 Correct 10 ms 10880 KB Output is correct
5 Correct 10 ms 11008 KB Output is correct
6 Correct 11 ms 11136 KB Output is correct
7 Correct 10 ms 11008 KB Output is correct
8 Correct 10 ms 11008 KB Output is correct
9 Correct 12 ms 11136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 11008 KB Output is correct
2 Correct 10 ms 11008 KB Output is correct
3 Correct 10 ms 11136 KB Output is correct
4 Correct 10 ms 10880 KB Output is correct
5 Correct 10 ms 11008 KB Output is correct
6 Correct 11 ms 11136 KB Output is correct
7 Correct 10 ms 11008 KB Output is correct
8 Correct 10 ms 11008 KB Output is correct
9 Correct 12 ms 11136 KB Output is correct
10 Correct 10 ms 11008 KB Output is correct
11 Correct 19 ms 13312 KB Output is correct
12 Correct 28 ms 14328 KB Output is correct
13 Correct 50 ms 23288 KB Output is correct
14 Correct 72 ms 21496 KB Output is correct
15 Correct 84 ms 21880 KB Output is correct
16 Correct 62 ms 19192 KB Output is correct
17 Correct 60 ms 18040 KB Output is correct
18 Correct 30 ms 14584 KB Output is correct
19 Correct 67 ms 22520 KB Output is correct
20 Correct 83 ms 23032 KB Output is correct
21 Correct 73 ms 20344 KB Output is correct
22 Correct 72 ms 19448 KB Output is correct
23 Correct 73 ms 23032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 11008 KB Output is correct
2 Correct 10 ms 11008 KB Output is correct
3 Correct 10 ms 11136 KB Output is correct
4 Correct 10 ms 10880 KB Output is correct
5 Correct 10 ms 11008 KB Output is correct
6 Correct 11 ms 11136 KB Output is correct
7 Correct 10 ms 11008 KB Output is correct
8 Correct 10 ms 11008 KB Output is correct
9 Correct 12 ms 11136 KB Output is correct
10 Correct 10 ms 11008 KB Output is correct
11 Correct 19 ms 13312 KB Output is correct
12 Correct 28 ms 14328 KB Output is correct
13 Correct 50 ms 23288 KB Output is correct
14 Correct 72 ms 21496 KB Output is correct
15 Correct 84 ms 21880 KB Output is correct
16 Correct 62 ms 19192 KB Output is correct
17 Correct 60 ms 18040 KB Output is correct
18 Correct 30 ms 14584 KB Output is correct
19 Correct 67 ms 22520 KB Output is correct
20 Correct 83 ms 23032 KB Output is correct
21 Correct 73 ms 20344 KB Output is correct
22 Correct 72 ms 19448 KB Output is correct
23 Correct 73 ms 23032 KB Output is correct
24 Correct 12 ms 11008 KB Output is correct
25 Correct 49 ms 13312 KB Output is correct
26 Correct 44 ms 14336 KB Output is correct
27 Correct 2512 ms 23544 KB Output is correct
28 Correct 554 ms 21624 KB Output is correct
29 Correct 2824 ms 22008 KB Output is correct
30 Correct 1580 ms 19248 KB Output is correct
31 Correct 1599 ms 18216 KB Output is correct
32 Correct 32 ms 14584 KB Output is correct
33 Correct 560 ms 22776 KB Output is correct
34 Correct 2809 ms 23288 KB Output is correct
35 Correct 1707 ms 20480 KB Output is correct
36 Correct 1678 ms 19624 KB Output is correct
37 Correct 320 ms 23220 KB Output is correct
38 Correct 2191 ms 31904 KB Output is correct