Submission #1027659

# Submission time Handle Problem Language Result Execution time Memory
1027659 2024-07-19T08:37:57 Z 우민규(#10951) None (JOI16_dungeon2) C++14
100 / 100
16 ms 1752 KB
#include "dungeon2.h"

#include <bits/stdc++.h>
using namespace std;
// edge no, is back-edge?, is going back (depth decreasing)?
vector<tuple<int, bool, bool>> dfs_travel;
vector<pair<int, int>> saw;

enum DfsState {
  Unvisited = 1,
  Visiting,
  Left,
};

void dfs() {
  int back = LastRoad();
  int num_roads = NumberOfRoads();
  for (int i = 0; i < num_roads; ++i) {
    if (i + 1 == back) continue;
    dfs_travel.push_back({i, false, false});
    Move(i + 1, Visiting);

    int back2 = LastRoad();

    switch (Color()) {
      case Unvisited:
        dfs();
        dfs_travel.push_back({back2 - 1, false, true});
        Move(back2, Left);
        break;
      case Visiting:
        dfs_travel.back() = {i, true, true};
        dfs_travel.push_back({back2 - 1, true, false});
        Move(back2, Visiting);
        break;
      case Left:
        // going to already-visited child
        dfs_travel.pop_back();
        Move(back2, Left);
        break;
    }
  }
}

void Inspect(int r) {
  dfs();
  saw.assign(dfs_travel.size(), {0, 0});
  int n;
  for (int chunk = 1; chunk < 200; chunk *= 3) {
    int cur = 0;
    int travel_idx = 0;
    bool is_new_node = true;
    for (auto [edge, is_back_edge, is_going_back] : dfs_travel) {
      int color = is_new_node ? 1 + (cur / chunk) % 3 : Color();
      if (!is_back_edge && !is_going_back) {
        // going to new node, so assign
        is_new_node = true;
        cur += 1;
      } else {
        is_new_node = false;
      }

      if (is_going_back) {
        int u_color = color - 1;
        Move(edge + 1, color);
        int v_color = Color() - 1;
        saw[travel_idx].first += u_color * chunk;
        saw[travel_idx].second += v_color * chunk;
      } else {
        // depth increasing, could be back edge
        Move(edge + 1, color);
      }

      travel_idx += 1;
    }
    n = cur + 1;
  }
  vector<vector<int>> adj(n);
  for (int i = 0; i < saw.size(); ++i) {
    // is_going_back
    if (get<2>(dfs_travel[i])) {
      adj[saw[i].first].push_back(saw[i].second);
      adj[saw[i].second].push_back(saw[i].first);
    }
  }
  vector<int> answers(max(n, r + 1));
  //   vector<int> answers(n);
  for (int src = 0; src < n; ++src) {
    // do bfs on src
    queue<pair<int, int>> q;
    vector<int> dist(n, INT_MAX);
    dist[src] = 0;
    q.push({0, src});
    while (!q.empty()) {
      auto [d, node] = q.front();
      q.pop();
      for (auto v : adj[node]) {
        if (dist[v] > d + 1) {
          dist[v] = d + 1;
          q.push({d + 1, v});
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      assert(dist[i] != INT_MAX);
      answers[dist[i]] += 1;
    }
  }
  for (int i = 1; i <= r; ++i) {
    Answer(i, answers[i] / 2);
  }
}

#ifndef EVAL
#include "grader.cpp"
#endif

Compilation message

dungeon2.cpp: In function 'void Inspect(int)':
dungeon2.cpp:53:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   53 |     for (auto [edge, is_back_edge, is_going_back] : dfs_travel) {
      |               ^
dungeon2.cpp:79:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |   for (int i = 0; i < saw.size(); ++i) {
      |                   ~~^~~~~~~~~~~~
dungeon2.cpp:95:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   95 |       auto [d, node] = q.front();
      |            ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 444 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 448 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 440 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 360 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 872 KB Output is correct
3 Correct 2 ms 860 KB Output is correct
4 Correct 16 ms 1752 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 704 KB Output is correct
8 Correct 1 ms 668 KB Output is correct
9 Correct 2 ms 860 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 888 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 3 ms 948 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 2 ms 860 KB Output is correct
17 Correct 12 ms 1748 KB Output is correct
18 Correct 11 ms 1748 KB Output is correct
19 Correct 11 ms 1748 KB Output is correct