답안 #30559

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
30559 2017-07-24T16:39:25 Z top34051 열대 식물원 (Tropical Garden) (IOI11_garden) C++14
100 / 100
3151 ms 40932 KB
#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
using namespace std;

#define maxn 150005

int p;
int sz0,sz1;
int from[maxn*2];
int vis[maxn*2];
int len[maxn*2][2];
vector<int> mn[maxn];
vector<int> re[maxn*2];
stack<int> st;

void dfs(int x,int last) {
    int t,cnt,ok0,ok1;
    if(vis[x]==1) {
//        printf("cycle ");
        cnt = ok0 = ok1 = 0;
        while(!st.empty()) {
            t = st.top(); st.pop();
//            printf("%d ",t);
            cnt++;
            if(t==p*2) ok0 = 1;
            if(t==p*2+1) ok1 = 1;
            if(t==x) break;
        }
        if(ok0) sz0 = cnt;
        if(ok1) sz1 = cnt;
//        printf("\n");
        return ;
    }
    vis[x] = 1;
    st.push(x);
    if(vis[from[x]]<=1) dfs(from[x],x);
    vis[x] = 2;
    if(!st.empty()) st.pop();
}

void dfs2(int x,int now) {
    int i,y;
    for(i=0;i<re[x].size();i++) {
        y = re[x][i];
        if(len[y][now]==-1) {
            len[y][now] = len[x][now] + 1;
            dfs2(y,now);
        }
    }
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
    int i,x,y,res;
    p = P;
    for(i=0;i<M;i++) {
        x = R[i][0]; y = R[i][1];
        if(mn[x].size()<2) mn[x].push_back(y);
        if(mn[y].size()<2) mn[y].push_back(x);
    }
    for(x=0;x<N;x++) if(mn[x].size()<2) mn[x].push_back(mn[x][0]);
    for(x=0;x<N;x++) {
        y = mn[x][1];
        if(mn[y][0]==x) from[x*2] = y*2;
        else from[x*2] = y*2+1;
        y = mn[x][0];
        if(mn[y][0]==x) from[x*2+1] = y*2;
        else from[x*2+1] = y*2+1;
//        printf("edge %d(%d) => %d(%d)\n",x,0,from[x*2]/2,from[x*2]%2);
//        printf("edge %d(%d) => %d(%d)\n",x,1,from[x*2+1]/2,from[x*2+1]%2);
    }
    for(x=0;x<2*N;x++) re[from[x]].push_back(x);
    for(x=0;x<2*N;x++) if(!vis[x]) dfs(x,-1);
    for(x=0;x<2*N;x++) len[x][0] = len[x][1] = -1;
    len[p*2][0] = 0;
    dfs2(p*2,0);
    len[p*2+1][1] = 0;
    dfs2(p*2+1,1);
//    printf("sz0 = %d sz1 = %d\n",sz0,sz1);
//    for(x=0;x<2*N;x++) printf("len %d 0 = %d   len %d 1 = %d\n",x,len[x][0],x,len[x][1]);
    for(i=0;i<Q;i++) {
        res = 0;
        for(x=1;x<2*N;x+=2) {
            if(len[x][0]!=-1 && G[i]==len[x][0]) res++;
            else if(len[x][1]!=-1 && G[i]==len[x][1]) res++;
            else if(sz0 && len[x][0]!=-1 && G[i]>=len[x][0] && (G[i]-len[x][0])%sz0==0) res++;
            else if(sz1 && len[x][1]!=-1 && G[i]>=len[x][1] && (G[i]-len[x][1])%sz1==0) res++;
        }
        answer(res);
    }
}

Compilation message

garden.cpp: In function 'void dfs2(int, int)':
garden.cpp:44:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0;i<re[x].size();i++) {
             ~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 11156 KB Output is correct
2 Correct 13 ms 11060 KB Output is correct
3 Correct 12 ms 11128 KB Output is correct
4 Correct 12 ms 10856 KB Output is correct
5 Correct 12 ms 10872 KB Output is correct
6 Correct 13 ms 11132 KB Output is correct
7 Correct 12 ms 11000 KB Output is correct
8 Correct 12 ms 11000 KB Output is correct
9 Correct 15 ms 11108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 11156 KB Output is correct
2 Correct 13 ms 11060 KB Output is correct
3 Correct 12 ms 11128 KB Output is correct
4 Correct 12 ms 10856 KB Output is correct
5 Correct 12 ms 10872 KB Output is correct
6 Correct 13 ms 11132 KB Output is correct
7 Correct 12 ms 11000 KB Output is correct
8 Correct 12 ms 11000 KB Output is correct
9 Correct 15 ms 11108 KB Output is correct
10 Correct 12 ms 10940 KB Output is correct
11 Correct 26 ms 14044 KB Output is correct
12 Correct 46 ms 15940 KB Output is correct
13 Correct 81 ms 34060 KB Output is correct
14 Correct 152 ms 25968 KB Output is correct
15 Correct 184 ms 26396 KB Output is correct
16 Correct 140 ms 22348 KB Output is correct
17 Correct 117 ms 20732 KB Output is correct
18 Correct 47 ms 15516 KB Output is correct
19 Correct 152 ms 26104 KB Output is correct
20 Correct 183 ms 26468 KB Output is correct
21 Correct 142 ms 22088 KB Output is correct
22 Correct 131 ms 20884 KB Output is correct
23 Correct 161 ms 28160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 11156 KB Output is correct
2 Correct 13 ms 11060 KB Output is correct
3 Correct 12 ms 11128 KB Output is correct
4 Correct 12 ms 10856 KB Output is correct
5 Correct 12 ms 10872 KB Output is correct
6 Correct 13 ms 11132 KB Output is correct
7 Correct 12 ms 11000 KB Output is correct
8 Correct 12 ms 11000 KB Output is correct
9 Correct 15 ms 11108 KB Output is correct
10 Correct 12 ms 10940 KB Output is correct
11 Correct 26 ms 14044 KB Output is correct
12 Correct 46 ms 15940 KB Output is correct
13 Correct 81 ms 34060 KB Output is correct
14 Correct 152 ms 25968 KB Output is correct
15 Correct 184 ms 26396 KB Output is correct
16 Correct 140 ms 22348 KB Output is correct
17 Correct 117 ms 20732 KB Output is correct
18 Correct 47 ms 15516 KB Output is correct
19 Correct 152 ms 26104 KB Output is correct
20 Correct 183 ms 26468 KB Output is correct
21 Correct 142 ms 22088 KB Output is correct
22 Correct 131 ms 20884 KB Output is correct
23 Correct 161 ms 28160 KB Output is correct
24 Correct 13 ms 10972 KB Output is correct
25 Correct 109 ms 14072 KB Output is correct
26 Correct 145 ms 16008 KB Output is correct
27 Correct 1979 ms 34200 KB Output is correct
28 Correct 1346 ms 26368 KB Output is correct
29 Correct 2257 ms 26840 KB Output is correct
30 Correct 1436 ms 22792 KB Output is correct
31 Correct 1099 ms 21016 KB Output is correct
32 Correct 152 ms 15716 KB Output is correct
33 Correct 1363 ms 26388 KB Output is correct
34 Correct 2172 ms 26804 KB Output is correct
35 Correct 1450 ms 22604 KB Output is correct
36 Correct 1825 ms 21160 KB Output is correct
37 Correct 964 ms 28596 KB Output is correct
38 Correct 3151 ms 40932 KB Output is correct