답안 #588622

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
588622 2022-07-03T17:30:13 Z MilosMilutinovic 열대 식물원 (Tropical Garden) (IOI11_garden) C++14
100 / 100
4593 ms 74412 KB
/**
 *    author:  wxhtzdy
 *    created: 03.07.2022 18:28:39
**/
#include "garden.h"
#include "gardenlib.h"

#include <bits/stdc++.h>

using namespace std;

void count_routes(int n, int m, int p, int r[][2], int q, int k[]) {
  vector<vector<pair<int, int>>> e(n);
  for (int i = 0; i < m; i++) {
    if ((int) e[r[i][0]].size() < 2) {
      e[r[i][0]].emplace_back(i, r[i][1]);
    }
    if ((int) e[r[i][1]].size() < 2) {
      e[r[i][1]].emplace_back(i, r[i][0]);
    }
  }
  for (int i = 0; i < n; i++) {
    if ((int) e[i].size() == 1) {
      e[i].push_back(e[i][0]);
    }
    assert((int) e[i].size() == 2);
  }
  vector<vector<int>> g(2 * n);
  vector<vector<int>> f(2 * n);
  function<void(int, int)> Add = [&](int v, int u) {
    g[v].push_back(u);
    f[u].push_back(v);
  };
  for (int i = 0; i < n; i++) {    
    if (e[i][0].first == e[e[i][0].second][0].first) {
      Add(i * 2, e[i][0].second * 2 + 1);
    } else {
      Add(i * 2, e[i][0].second * 2);
    }          
    if (e[i][1].first == e[e[i][1].second][0].first) {
      Add(i * 2 + 1, e[i][1].second * 2 + 1);
    } else {
      Add(i * 2 + 1, e[i][1].second * 2);
    }
  }
  vector<int> in(2 * n);
  for (int i = 0; i < 2 * n; i++) {
    in[g[i][0]] += 1;
  }        
  vector<int> que;
  for (int i = 0; i < 2 * n; i++) {
    if (in[i] == 0) {
      que.push_back(i);
    }                        
  }      
  for (int b = 0; b < (int) que.size(); b++) {
    int i = que[b];
    for (int j : g[i]) {
      in[j] -= 1;
      if (in[j] == 0) {
        que.push_back(j);
      }                    
    }
  }
  vector<vector<pair<int, int>>> opt(2 * n);
  if (in[2 * p] == 0) {              
    function<void(int, int)> Dfs = [&](int v, int dep) {
      opt[v].emplace_back(dep, 1e9 + 1);
      for (int u : f[v]) {
        Dfs(u, dep + 1);
      }
    };
    Dfs(2 * p, 0);
  }
  if (in[2 * p + 1] == 0) {              
    function<void(int, int)> Dfs = [&](int v, int dep) {
      opt[v].emplace_back(dep, 1e9 + 1);
      for (int u : f[v]) {
        Dfs(u, dep + 1);
      }
    };
    Dfs(2 * p + 1, 0);
  }
  for (int ver = 0; ver < 2 * n; ver++) {
    if (in[ver] == 0) {
      continue;
    }   
    que = vector<int>(1, ver);
    for (int b = 0; b < (int) que.size(); b++) {
      int i = que[b];
      for (int j : g[i]) {
        in[j] -= 1;
        if (in[j] == 0 && j != ver) {
          que.push_back(j);
        }
      }
    }       
    for (int i : que) {
      if (i == 2 * p) {
        function<void(int, int)> Dfs = [&](int v, int dep) {
          opt[v].emplace_back(dep, (int) que.size());
          for (int u : f[v]) {
            if (u != 2 * p) {
              Dfs(u, dep + 1);  
            }  
          } 
        };
        Dfs(i, 0);
      }
    }
    for (int i : que) {
      if (i == 2 * p + 1) {
        function<void(int, int)> Dfs = [&](int v, int dep) {
          opt[v].emplace_back(dep, (int) que.size());
          for (int u : f[v]) {
            if (u != 2 * p + 1) {
              Dfs(u, dep + 1);  
            }  
          } 
        };
        Dfs(i, 0);
      }
    }
  }
  for (int i = 0; i < q; i++) {
    int cnt = 0;
    for (int j = 0; j < n; j++) {
      int add = 0;
      for (auto& p : opt[2 * j]) {
        if (p.first > k[i]) {
          continue;
        }
        if ((k[i] - p.first) % p.second == 0) {
          add = 1;          
        }
      }
      cnt += add;
    }
    answer(cnt);
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 15 ms 7688 KB Output is correct
12 Correct 39 ms 12444 KB Output is correct
13 Correct 91 ms 48328 KB Output is correct
14 Correct 219 ms 43544 KB Output is correct
15 Correct 274 ms 50932 KB Output is correct
16 Correct 182 ms 35036 KB Output is correct
17 Correct 210 ms 29408 KB Output is correct
18 Correct 38 ms 12508 KB Output is correct
19 Correct 206 ms 43184 KB Output is correct
20 Correct 273 ms 50916 KB Output is correct
21 Correct 234 ms 34784 KB Output is correct
22 Correct 148 ms 29416 KB Output is correct
23 Correct 211 ms 47572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 15 ms 7688 KB Output is correct
12 Correct 39 ms 12444 KB Output is correct
13 Correct 91 ms 48328 KB Output is correct
14 Correct 219 ms 43544 KB Output is correct
15 Correct 274 ms 50932 KB Output is correct
16 Correct 182 ms 35036 KB Output is correct
17 Correct 210 ms 29408 KB Output is correct
18 Correct 38 ms 12508 KB Output is correct
19 Correct 206 ms 43184 KB Output is correct
20 Correct 273 ms 50916 KB Output is correct
21 Correct 234 ms 34784 KB Output is correct
22 Correct 148 ms 29416 KB Output is correct
23 Correct 211 ms 47572 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 90 ms 7688 KB Output is correct
26 Correct 139 ms 12988 KB Output is correct
27 Correct 2298 ms 49420 KB Output is correct
28 Correct 1163 ms 45208 KB Output is correct
29 Correct 4593 ms 52748 KB Output is correct
30 Correct 2289 ms 36604 KB Output is correct
31 Correct 1703 ms 31120 KB Output is correct
32 Correct 161 ms 13076 KB Output is correct
33 Correct 1329 ms 45132 KB Output is correct
34 Correct 4454 ms 52908 KB Output is correct
35 Correct 2460 ms 36600 KB Output is correct
36 Correct 2216 ms 31044 KB Output is correct
37 Correct 1155 ms 49332 KB Output is correct
38 Correct 2175 ms 74412 KB Output is correct