Submission #7511

# Submission time Handle Problem Language Result Execution time Memory
7511 2014-08-10T07:03:43 Z baneling100 Tropical Garden (IOI11_garden) C++
100 / 100
4106 ms 9772 KB
#include "garden.h"
#include "gardenlib.h"
#include <vector>
#define INF 0x7fffffff

using namespace std;

int First[150001], Second[150001], A[300001], D[300001][2], Check[300001], K, Cycle1, Cycle2;

void DFS(int Now)
{
    if(!Check[A[Now]])
    {
        Check[A[Now]]=1;
        DFS(A[Now]);
    }
    if(D[A[Now]][K]!=INF)
        D[Now][K]=D[A[Now]][K]+1;
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
    int i, j, Cnt;

    for(i=0 ; i<N ; i++)
        First[i]=Second[i]=-1;
    for(i=0 ; i<M ; i++)
    {
        if(First[R[i][0]]==-1)
            First[R[i][0]]=R[i][1];
        else if(Second[R[i][0]]==-1)
            Second[R[i][0]]=R[i][1];
        if(First[R[i][1]]==-1)
            First[R[i][1]]=R[i][0];
        else if(Second[R[i][1]]==-1)
            Second[R[i][1]]=R[i][0];
    }
    for(i=0 ; i<N ; i++)
    {
        if(Second[i]==-1)
        {
            if(First[First[i]]==i)
                A[2*i]=2*First[i];
            else
                A[2*i]=2*First[i]+1;
            A[2*i+1]=-1;
        }
        else
        {
            if(First[Second[i]]==i)
                A[2*i]=2*Second[i];
            else
                A[2*i]=2*Second[i]+1;
            if(First[First[i]]==i)
                A[2*i+1]=2*First[i];
            else
                A[2*i+1]=2*First[i]+1;
        }
    }
    K=0;
    for(i=0 ; i<2*N ; i++)
    {
        Check[i]=0;
        D[i][0]=INF;
    }
    Check[2*P]=1;
    D[2*P][0]=0;
    for(i=0 ; i<2*N ; i++)
        if(A[i]!=-1 && !Check[i])
        {
            Check[i]=1;
            DFS(i);
        }
    if(A[2*P+1]!=-1)
    {
        K=1;
        for(i=0 ; i<2*N ; i++)
        {
            Check[i]=0;
            D[i][1]=INF;
        }
        Check[2*P+1]=1;
        D[2*P+1][1]=0;
        for(i=0 ; i<2*N ; i++)
            if(A[i]!=-1 && !Check[i])
            {
                Check[i]=1;
                DFS(i);
            }
    }
    if(D[A[2*P]][0]!=INF)
        Cycle1=D[A[2*P]][0]+1;
    if(Second[P]!=-1 && D[A[2*P+1]][1]!=INF)
        Cycle2=D[A[2*P+1]][1]+1;
    for(i=0 ; i<Q ; i++)
    {
        Cnt=0;
        for(j=0 ; j<2*N ; j++)
            if((j%2==0 && Second[j/2]==-1) || (j%2==1 && Second[j/2]!=-1))
            {
                if(Cycle1)
                {
                    if(G[i]>=D[j][0] && (G[i]-D[j][0])%Cycle1==0)
                    {
                        Cnt++;
                        continue;
                    }
                }
                else
                {
                    if(G[i]==D[j][0])
                    {
                        Cnt++;
                        continue;
                    }
                }
                if(Second[P]!=-1)
                {
                    if(Cycle2)
                    {
                        if(G[i]>=D[j][1] && (G[i]-D[j][1])%Cycle2==0)
                        {
                            Cnt++;
                            continue;
                        }
                    }
                    else
                        if(G[i]==D[j][1])
                        {
                            Cnt++;
                            continue;
                        }
                }
            }
        answer(Cnt);
    }
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 380 KB Output is correct
9 Correct 4 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 380 KB Output is correct
9 Correct 4 ms 504 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 9 ms 1400 KB Output is correct
12 Correct 19 ms 2260 KB Output is correct
13 Correct 30 ms 7036 KB Output is correct
14 Correct 55 ms 6748 KB Output is correct
15 Correct 63 ms 6880 KB Output is correct
16 Correct 50 ms 4984 KB Output is correct
17 Correct 48 ms 4344 KB Output is correct
18 Correct 20 ms 2268 KB Output is correct
19 Correct 58 ms 6792 KB Output is correct
20 Correct 66 ms 6888 KB Output is correct
21 Correct 50 ms 4984 KB Output is correct
22 Correct 51 ms 4472 KB Output is correct
23 Correct 57 ms 7416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 380 KB Output is correct
9 Correct 4 ms 504 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 9 ms 1400 KB Output is correct
12 Correct 19 ms 2260 KB Output is correct
13 Correct 30 ms 7036 KB Output is correct
14 Correct 55 ms 6748 KB Output is correct
15 Correct 63 ms 6880 KB Output is correct
16 Correct 50 ms 4984 KB Output is correct
17 Correct 48 ms 4344 KB Output is correct
18 Correct 20 ms 2268 KB Output is correct
19 Correct 58 ms 6792 KB Output is correct
20 Correct 66 ms 6888 KB Output is correct
21 Correct 50 ms 4984 KB Output is correct
22 Correct 51 ms 4472 KB Output is correct
23 Correct 57 ms 7416 KB Output is correct
24 Correct 4 ms 376 KB Output is correct
25 Correct 305 ms 1656 KB Output is correct
26 Correct 647 ms 2400 KB Output is correct
27 Correct 2138 ms 7068 KB Output is correct
28 Correct 3437 ms 6796 KB Output is correct
29 Correct 4106 ms 6892 KB Output is correct
30 Correct 2372 ms 5092 KB Output is correct
31 Correct 1695 ms 4472 KB Output is correct
32 Correct 644 ms 2396 KB Output is correct
33 Correct 3305 ms 6800 KB Output is correct
34 Correct 4014 ms 6900 KB Output is correct
35 Correct 2337 ms 5052 KB Output is correct
36 Correct 1840 ms 4848 KB Output is correct
37 Correct 2994 ms 7880 KB Output is correct
38 Correct 3708 ms 9772 KB Output is correct