답안 #650042

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
650042 2022-10-12T06:09:27 Z alvinpiter 열대 식물원 (Tropical Garden) (IOI11_garden) C++17
100 / 100
2641 ms 59132 KB
/*
u -> second best and others
u + n -> best
*/

#include "garden.h"
#include "gardenlib.h"
#include<bits/stdc++.h>
using namespace std;
#define INF 2000000000
#define MAXN 150000

int N;
vector<pair<int, int> > oriAdj[MAXN + 3]; // {beauty, v}
vector<int> adj[2 * MAXN + 3], revAdj[2 * MAXN + 3];
int dist[2 * MAXN + 3][2], cycleLength[2];
bool visited[2 * MAXN + 3][2];

void addEdgeToNewGraph(int u, int v, int beauty) {
  bool isBestForV = (beauty == oriAdj[v][0].first);
  int resolvedV = (isBestForV ? v + N : v);
  adj[u].push_back(resolvedV);
}

void dfs(int u, int t, int step, int origin) {
  dist[u][t] = step;
  visited[u][t] = true;

  for (auto v: revAdj[u]) {
    if (v == origin) {
      cycleLength[t] = step + 1;
    }

    if (!visited[v][t]) {
      dfs(v, t, step + 1, origin);
    }
  }
}

void count_routes(int n, int m, int p, int r[][2], int q, int g[]) {
  N = n;

  for (int i = 0; i < m; i++) {
    int u = r[i][0], v = r[i][1], beauty = i;
    oriAdj[u].push_back({beauty, v});
    oriAdj[v].push_back({beauty, u});
  }

  for (int u = 0; u < n; u++) {
    sort(oriAdj[u].begin(), oriAdj[u].end());
  }

  for (int u = 0; u < n; u++) {
    // Add directed edge from u to v or (v + n).
    if (true) {
      auto [beauty, v] = oriAdj[u][0];
      addEdgeToNewGraph(u, v, beauty);
    }

    // Add directed edge from (u + n) to v or (v + n).
    if (true) {
      auto [beauty, v] = oriAdj[u].size() > 1 ? oriAdj[u][1] : oriAdj[u][0];
      addEdgeToNewGraph(u + n, v, beauty);
    }
  }

  // cout <<"\nadj\n";
  // for (int u = 0; u < 2 * n; u++) {
  //   cout << u << ":";
  //   for (auto v: adj[u]) {
  //     cout << " " << v;
  //   }
  //   cout << endl;
  // }

  /*
  Reverse the edges of the new graph.
  This could be done previously, but to make it more understandable, I do it separately.
  */
  for (int u = 0; u < 2 * n; u++) {
    for (auto v: adj[u]) {
      revAdj[v].push_back(u);
    }
  }

  // cout <<"\nrevAdj\n";
  // for (int u = 0; u < 2 * n; u++) {
  //   cout << u << ":";
  //   for (auto v: revAdj[u]) {
  //     cout << " " << v;
  //   }
  //   cout << endl;
  // }

  for (int t = 0; t < 2; t++) {
    cycleLength[t] = -1;
    for (int i = 0; i < 2 * n; i++) {
      dist[i][t] = INF;
      visited[i][t] = false;
    }
  }

  dfs(p, 0, 0, p);
  dfs(p + n, 1, 0, p + n);

  // cout << "\ncycleLength[0]: " << cycleLength[0] << endl;
  // cout << "cycleLength[1]: " << cycleLength[1] << endl;

  // cout << "\ndist0:\n";
  // for (int u = 0; u < 2 * n; u++) {
  //   cout << u << ": " << dist[u][0] << endl;
  // }

  // cout << "\ndist1:\n";
  // for (int u = 0; u < 2 * n; u++) {
  //   cout << u << ": " << dist[u][1] << endl;
  // }

  for (int query = 0; query < q; query++) {
    int ans = 0;
    for (int u = 0; u < n; u++) {
      bool possible = false;
      for (int t = 0; t < 2; t++) {
        if (dist[u][t] == g[query] || (cycleLength[t] != -1 && g[query] >= dist[u][t] && (g[query] - dist[u][t])%cycleLength[t] == 0)) {
          possible = true;
        }

        // if (oriAdj[u].size() == 1) {
        //   if (dist[u + n][t] == g[query] || (cycleLength[t] != -1 && dist[u + n][t] == g[query]%cycleLength[t])) {
        //     possible = true;
        //   }
        // }
      }

      if (possible) {
        ans += 1;
      }
    }

    answer(ans);
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 18132 KB Output is correct
2 Correct 10 ms 18132 KB Output is correct
3 Correct 10 ms 18160 KB Output is correct
4 Correct 10 ms 17876 KB Output is correct
5 Correct 10 ms 17876 KB Output is correct
6 Correct 11 ms 18260 KB Output is correct
7 Correct 10 ms 17908 KB Output is correct
8 Correct 11 ms 18024 KB Output is correct
9 Correct 14 ms 18232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 18132 KB Output is correct
2 Correct 10 ms 18132 KB Output is correct
3 Correct 10 ms 18160 KB Output is correct
4 Correct 10 ms 17876 KB Output is correct
5 Correct 10 ms 17876 KB Output is correct
6 Correct 11 ms 18260 KB Output is correct
7 Correct 10 ms 17908 KB Output is correct
8 Correct 11 ms 18024 KB Output is correct
9 Correct 14 ms 18232 KB Output is correct
10 Correct 9 ms 17876 KB Output is correct
11 Correct 20 ms 21716 KB Output is correct
12 Correct 38 ms 25016 KB Output is correct
13 Correct 62 ms 47688 KB Output is correct
14 Correct 137 ms 40604 KB Output is correct
15 Correct 155 ms 41040 KB Output is correct
16 Correct 108 ms 35088 KB Output is correct
17 Correct 109 ms 32864 KB Output is correct
18 Correct 40 ms 24992 KB Output is correct
19 Correct 121 ms 40580 KB Output is correct
20 Correct 163 ms 40912 KB Output is correct
21 Correct 134 ms 34884 KB Output is correct
22 Correct 104 ms 32796 KB Output is correct
23 Correct 139 ms 42868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 18132 KB Output is correct
2 Correct 10 ms 18132 KB Output is correct
3 Correct 10 ms 18160 KB Output is correct
4 Correct 10 ms 17876 KB Output is correct
5 Correct 10 ms 17876 KB Output is correct
6 Correct 11 ms 18260 KB Output is correct
7 Correct 10 ms 17908 KB Output is correct
8 Correct 11 ms 18024 KB Output is correct
9 Correct 14 ms 18232 KB Output is correct
10 Correct 9 ms 17876 KB Output is correct
11 Correct 20 ms 21716 KB Output is correct
12 Correct 38 ms 25016 KB Output is correct
13 Correct 62 ms 47688 KB Output is correct
14 Correct 137 ms 40604 KB Output is correct
15 Correct 155 ms 41040 KB Output is correct
16 Correct 108 ms 35088 KB Output is correct
17 Correct 109 ms 32864 KB Output is correct
18 Correct 40 ms 24992 KB Output is correct
19 Correct 121 ms 40580 KB Output is correct
20 Correct 163 ms 40912 KB Output is correct
21 Correct 134 ms 34884 KB Output is correct
22 Correct 104 ms 32796 KB Output is correct
23 Correct 139 ms 42868 KB Output is correct
24 Correct 11 ms 17940 KB Output is correct
25 Correct 107 ms 22068 KB Output is correct
26 Correct 137 ms 25064 KB Output is correct
27 Correct 2321 ms 47932 KB Output is correct
28 Correct 948 ms 41548 KB Output is correct
29 Correct 2641 ms 42008 KB Output is correct
30 Correct 1464 ms 35936 KB Output is correct
31 Correct 1494 ms 33772 KB Output is correct
32 Correct 141 ms 25068 KB Output is correct
33 Correct 932 ms 41532 KB Output is correct
34 Correct 2635 ms 41992 KB Output is correct
35 Correct 1593 ms 35648 KB Output is correct
36 Correct 1517 ms 33748 KB Output is correct
37 Correct 700 ms 43908 KB Output is correct
38 Correct 2028 ms 59132 KB Output is correct