답안 #501184

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
501184 2022-01-02T14:44:23 Z doowey 열대 식물원 (Tropical Garden) (IOI11_garden) C++14
100 / 100
2616 ms 35320 KB
#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair

const int N = 151515;
const int M = N * 2;

vector<int> T[N];

int id[M];
int ban[M];
int idx = 0;

int n, m, target;

int f[M]; // functional graph

int ret[N][2];

vector<int> qr;
vector<int> res;
int vis[M];
vector<int> TE[M];

vector<int> ord;
bool cyc[M];

vector<int> lis;

void dfs(int u, int par, int ii){
    lis.push_back(u);
    if(ban[u] == false){
        int Z;
        int reach;
        for(int v = 0 ; v < qr.size(); v ++ ){
            Z = qr[v];
            if((int)lis.size() - 1 - Z >= 0){
                reach = lis[lis.size() - 1 - Z];
            }
            else{
                Z -= (int)lis.size() - 1;
                reach = ord[(ii + Z) % ord.size()];
            }
            if(id[reach] == target){
                res[v] ++ ;
            }
        }
    }
    for(auto x : TE[u]){
        if(cyc[x] || par == x) continue;
        dfs(x, u, ii);
    }
    lis.pop_back();
}

void count_routes(int _N, int _M, int _P, int R[][2], int Q, int G[]){
    n = _N;
    m = _M;
    target = _P;
    for(int i = 0 ; i < Q; i ++ ){
        qr.push_back(G[i]);
    }
    res.resize(Q);
    for(int i = 0 ; i < m ; i ++ ){
        T[R[i][0]].push_back(R[i][1]);
        T[R[i][1]].push_back(R[i][0]);
    }
    for(int i = 0; i < n; i ++ ){
        id[idx] = i;
        ban[idx] = 0;
        ret[i][0] = idx;
        idx ++ ;
        id[idx] = i;
        ban[idx] = 1;
        ret[i][1] = idx;
        idx ++ ;
    }
    int nex;
    int ban_nex;
    int node;
    int rq;
    for(int i = 0 ; i < idx; i ++ ){
        node = id[i];
        if(ban[i] == 0){
            nex = T[node][0];
            ban_nex = (T[nex][0] == node);
            f[i] = ret[nex][ban_nex];
        }
        else{

            if(T[node].size() == 1) nex = T[node][0];
            else nex = T[node][1];
            ban_nex = (T[nex][0] == node);
            f[i] = ret[nex][ban_nex];
        }
        TE[f[i]].push_back(i);
        vis[i] = -1;
    }
    int ff;
    int ni;
    for(int i = 0 ; i < idx; i ++ ){
        ff = i;
        while(vis[ff] == -1){
            vis[ff] = i;
            ff = f[ff];
        }
        if(vis[ff] == i){
            ni = ff;
            rq = 0;
            ord.clear();
            do{
                ord.push_back(ni);
                cyc[ni]=true;
                ni = f[ni];
            }while(ni != ff);
            for(int j = 0 ; j < ord.size(); j ++ ){
                dfs(ord[j], -1, j);
            }
        }
    }
    for(int i = 0 ; i < Q; i ++ ){
        answer(res[i]);
    }
}


Compilation message

garden.cpp: In function 'void dfs(int, int, int)':
garden.cpp:44:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |         for(int v = 0 ; v < qr.size(); v ++ ){
      |                         ~~^~~~~~~~~~~
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:125:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |             for(int j = 0 ; j < ord.size(); j ++ ){
      |                             ~~^~~~~~~~~~~~
garden.cpp:90:9: warning: variable 'rq' set but not used [-Wunused-but-set-variable]
   90 |     int rq;
      |         ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11212 KB Output is correct
2 Correct 6 ms 11120 KB Output is correct
3 Correct 6 ms 11120 KB Output is correct
4 Correct 6 ms 11000 KB Output is correct
5 Correct 6 ms 11116 KB Output is correct
6 Correct 9 ms 11264 KB Output is correct
7 Correct 6 ms 10996 KB Output is correct
8 Correct 6 ms 11084 KB Output is correct
9 Correct 7 ms 11264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11212 KB Output is correct
2 Correct 6 ms 11120 KB Output is correct
3 Correct 6 ms 11120 KB Output is correct
4 Correct 6 ms 11000 KB Output is correct
5 Correct 6 ms 11116 KB Output is correct
6 Correct 9 ms 11264 KB Output is correct
7 Correct 6 ms 10996 KB Output is correct
8 Correct 6 ms 11084 KB Output is correct
9 Correct 7 ms 11264 KB Output is correct
10 Correct 5 ms 10996 KB Output is correct
11 Correct 16 ms 13948 KB Output is correct
12 Correct 50 ms 16084 KB Output is correct
13 Correct 41 ms 25320 KB Output is correct
14 Correct 178 ms 28056 KB Output is correct
15 Correct 153 ms 28348 KB Output is correct
16 Correct 110 ms 23672 KB Output is correct
17 Correct 134 ms 22264 KB Output is correct
18 Correct 37 ms 16152 KB Output is correct
19 Correct 147 ms 27896 KB Output is correct
20 Correct 205 ms 28224 KB Output is correct
21 Correct 110 ms 23748 KB Output is correct
22 Correct 117 ms 22212 KB Output is correct
23 Correct 163 ms 29732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11212 KB Output is correct
2 Correct 6 ms 11120 KB Output is correct
3 Correct 6 ms 11120 KB Output is correct
4 Correct 6 ms 11000 KB Output is correct
5 Correct 6 ms 11116 KB Output is correct
6 Correct 9 ms 11264 KB Output is correct
7 Correct 6 ms 10996 KB Output is correct
8 Correct 6 ms 11084 KB Output is correct
9 Correct 7 ms 11264 KB Output is correct
10 Correct 5 ms 10996 KB Output is correct
11 Correct 16 ms 13948 KB Output is correct
12 Correct 50 ms 16084 KB Output is correct
13 Correct 41 ms 25320 KB Output is correct
14 Correct 178 ms 28056 KB Output is correct
15 Correct 153 ms 28348 KB Output is correct
16 Correct 110 ms 23672 KB Output is correct
17 Correct 134 ms 22264 KB Output is correct
18 Correct 37 ms 16152 KB Output is correct
19 Correct 147 ms 27896 KB Output is correct
20 Correct 205 ms 28224 KB Output is correct
21 Correct 110 ms 23748 KB Output is correct
22 Correct 117 ms 22212 KB Output is correct
23 Correct 163 ms 29732 KB Output is correct
24 Correct 6 ms 11004 KB Output is correct
25 Correct 267 ms 14020 KB Output is correct
26 Correct 492 ms 16200 KB Output is correct
27 Correct 1142 ms 25284 KB Output is correct
28 Correct 2485 ms 28052 KB Output is correct
29 Correct 2616 ms 28400 KB Output is correct
30 Correct 1444 ms 23868 KB Output is correct
31 Correct 1491 ms 22376 KB Output is correct
32 Correct 519 ms 16196 KB Output is correct
33 Correct 2435 ms 28112 KB Output is correct
34 Correct 2570 ms 28384 KB Output is correct
35 Correct 1530 ms 23876 KB Output is correct
36 Correct 1469 ms 22576 KB Output is correct
37 Correct 2289 ms 29696 KB Output is correct
38 Correct 1929 ms 35320 KB Output is correct