Submission #655730

# Submission time Handle Problem Language Result Execution time Memory
655730 2022-11-05T11:50:26 Z 600Mihnea From Hacks to Snitches (BOI21_watchmen) C++17
25 / 100
6000 ms 221696 KB
// pana la ora 1
bool home = 0;
bool verbose = 1;

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
#include <cassert>

using namespace std;

struct T {
  int v;
  int r;
};

const int N = 250000 + 7;
const int V = (int)1e7 + 7;
const int INF = (int)1e9 + 7;
int n;
int m;
int cnt_cycles;
int suml;
vector<pair<int, int>> edges;
vector<int> cycles[N];
int cycle_id[N];
int cycle_location[N];
int single_vertex_id[N];
vector<int> cycle_vertex_id[N];
vector<int> splash_vertex_id[N];
int dp[V];
T what[V];
int y;
vector<int> g[N];

priority_queue<pair<int, int>> pq;

void relax(int id, int d) {
  if (d < dp[id]) {
    dp[id] = d;
    pq.push({ -dp[id], id });
  }
}

int main() {
  if (!home) {
    verbose = 0;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
  }
  if (home) {
    verbose = 1;
   // FILE* stream;
//freopen_s(&stream, "input.txt", "r", stdin);
  }

  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
    edges.push_back({ a, b });
  }

  cin >> cnt_cycles;

  for (int i = 1; i <= cnt_cycles; i++) {
    int l;
    cin >> l;
    suml += l;
    cycles[i].resize(l);
    for (int j = 0; j < (int)cycles[i].size(); j++) {
      cin >> cycles[i][j];
      cycle_id[cycles[i][j]] = i;
      cycle_location[cycles[i][j]] = j;
    }
    if (verbose) {
      cout << " ---> ";
      for (auto& v : cycles[i]) {
        cout << v << " ";
      }
      cout << "\n";
    }
  }
  for (int a = 1; a <= n; a++) {
    if (cycle_id[a] == 0) {
      y++;
      single_vertex_id[a] = y;
      what[y] = { a, 0 };
    }
    else {
      int id = cycle_id[a];
      cycle_vertex_id[a].resize((int)cycles[id].size());
      splash_vertex_id[a].resize((int)cycles[id].size());
      for (int j = 0; j < (int)cycles[id].size(); j++) {
        y++;
        cycle_vertex_id[a][j] = y;
        what[y] = { a, j };
      }
      for (int j = 0; j < (int)cycles[id].size(); j++) {
        y++;
        splash_vertex_id[a][j] = y;
        what[y] = { a, -(j + 1) };
      }
    }
  }
  for (int i = 1; i <= y; i++) {
    dp[i] = INF;
  }
  if (verbose) {
    cout << "################################################################################### \n";
  }

  dp[single_vertex_id[1]] = 0;
  pq.push({ 0, single_vertex_id[1] });

  map<pair<int, int>, int> spec;
  vector<pair<int, int>> all;

  for (int vertex = 1; vertex <= n; vertex++) {
    if (int cycle = cycle_id[vertex]) {
      for (auto& vecin : g[vertex]) {
        if (int cycle_vecin = cycle_id[vecin]) {
          // vecin is on a cycle
          if (cycle_vecin != cycle) {
            // vecin is on a different cycle
            spec[{vertex, vecin}] = 0;
          }
        }
      }
    }
  }
  vector<vector<int>> ids;
  for (auto& it : spec) {
    it.second = (int)all.size();
    all.push_back(it.first);
    int vertex = it.first.first, vecin = it.first.second;
    int l_vecin = cycles[cycle_id[vecin]].size();
    ids.push_back({});
    ids.back().resize(l_vecin);
    for (int r = 0; r < l_vecin; r++) {
      y++;
      ids.back()[r] = y;
      what[y] = { -(int)ids.size(), r };
    }
  }

  while (!pq.empty()) {
    int d = -pq.top().first;
    int idv = pq.top().second;
    pq.pop();
    if (dp[idv] != d) {
      continue;
    }
    int vertex = what[idv].v;
    int r = what[idv].r;
    if (vertex < 0) {
      int p = vertex;
      p *= -1;
      p--;
      assert(0 <= p && p < (int)ids.size());
      int vertex = all[p].first, vecin = all[p].second;
      int l = cycles[cycle_id[vertex]].size();
      int l_vecin = cycles[cycle_id[vecin]].size();
      assert((int)ids[p].size() == l_vecin);
      relax(ids[p][(d + l) % l_vecin], d + l);

      if (d % l_vecin == cycle_location[vecin]) {
        continue;
      }
      relax(cycle_vertex_id[vecin][d % l_vecin], d);
      continue;
    }
    if (r < 0) {
      r *= -1;
      r--;

      int cycle = cycle_id[vertex];
      int l = cycles[cycle].size();
      relax(splash_vertex_id[vertex][(d + 1) % l], d + 1);
      if (d % l != cycle_location[vertex]) {
        relax(cycle_vertex_id[vertex][d % l], d);
      }
      continue;
    }
    if (cycle_id[vertex] == 0) {
      for (auto& vecin : g[vertex]) {
        if (cycle_id[vecin] == 0) {
          // vecin is a single vertex
          relax(single_vertex_id[vecin], d + 1);
        }
        else {
          // vecin is on a cycle
          int cycle_vecin = cycle_id[vecin];
          int l_vecin = cycles[cycle_vecin].size();
          relax(splash_vertex_id[vecin][(d + 1) % l_vecin], d + 1);
          continue;
          for (int twait = 0; twait < l_vecin; twait++) {
            // la momentul d + 1 + twait
            if ((d + 1 + twait) % l_vecin == cycle_location[vecin]) {
              continue;
            }
            relax(cycle_vertex_id[vecin][(d + 1 + twait) % l_vecin], d + 1 + twait);
          }
        }
      }
      continue;
    }
    if (cycle_id[vertex] != 0) {
      int cycle = cycle_id[vertex];
      int l = cycles[cycle].size();

      /*
        wait a moment here
      */

      if ((d + 1) % l != cycle_location[vertex]) {
        relax(cycle_vertex_id[vertex][(d + 1) % l], d + 1);
      }

      /*
        don't wait a moment here
      */

      for (auto& vecin : g[vertex]) {
        if (cycle_id[vecin] == 0) {
          // vecin is a single vertex
          relax(single_vertex_id[vecin], d + 1);
        }
        else {
          // vecin is on a cycle
          int cycle_vecin = cycle_id[vecin];
          int l_vecin = cycles[cycle_vecin].size();
          if (cycle_vecin == cycle) {
            // vecin is on the same cycle
            if ((d + 1) % l_vecin == cycle_location[vecin]) {
              continue;
            }
            if (d % l_vecin == cycle_location[vecin] && (d + 1) % l_vecin == cycle_location[vertex]) {
              continue;
            }

            relax(cycle_vertex_id[vecin][(d + 1) % l_vecin], d + 1);
            /*

            vertex -> vecin <- vecin2

            */
          }
          else {
            // vecin is on a different cycle
            assert(spec.count({ vertex, vecin }));
            int p = spec[{vertex, vecin}];
            relax(ids[p][(d + 1) % l_vecin], d + 1);
            continue;
            for (int j = 0; j <= l_vecin; j++) {
              int twait = j * l;
              // la momentul d + 1 + twait
              if ((d + 1 + twait) % l_vecin == cycle_location[vecin]) {
                continue;
              }
              relax(cycle_vertex_id[vecin][(d + 1 + twait) % l_vecin], d + 1 + twait);
            }
          }
        }
      }
      continue;
    }
  }

  int sol = dp[single_vertex_id[n]];
  if (sol == INF) {
    cout << "impossible\n";
    return 0;
  }
  cout << sol << "\n";
  return 0;
}

Compilation message

watchmen.cpp: In function 'int main()':
watchmen.cpp:141:9: warning: unused variable 'vertex' [-Wunused-variable]
  141 |     int vertex = it.first.first, vecin = it.first.second;
      |         ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 83 ms 26368 KB Output is correct
2 Correct 68 ms 30772 KB Output is correct
3 Correct 68 ms 30496 KB Output is correct
4 Correct 97 ms 30532 KB Output is correct
5 Correct 14 ms 24204 KB Output is correct
6 Correct 92 ms 30388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 26312 KB Output is correct
2 Correct 70 ms 30852 KB Output is correct
3 Correct 73 ms 30528 KB Output is correct
4 Correct 115 ms 30616 KB Output is correct
5 Correct 15 ms 24276 KB Output is correct
6 Correct 74 ms 30488 KB Output is correct
7 Correct 60 ms 30172 KB Output is correct
8 Correct 62 ms 30140 KB Output is correct
9 Correct 60 ms 30088 KB Output is correct
10 Correct 99 ms 30328 KB Output is correct
11 Correct 66 ms 30072 KB Output is correct
12 Correct 67 ms 30180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 26312 KB Output is correct
2 Correct 70 ms 30852 KB Output is correct
3 Correct 73 ms 30528 KB Output is correct
4 Correct 115 ms 30616 KB Output is correct
5 Correct 15 ms 24276 KB Output is correct
6 Correct 74 ms 30488 KB Output is correct
7 Correct 60 ms 30172 KB Output is correct
8 Correct 62 ms 30140 KB Output is correct
9 Correct 60 ms 30088 KB Output is correct
10 Correct 99 ms 30328 KB Output is correct
11 Correct 66 ms 30072 KB Output is correct
12 Correct 67 ms 30180 KB Output is correct
13 Correct 77 ms 26268 KB Output is correct
14 Correct 85 ms 30844 KB Output is correct
15 Correct 73 ms 30452 KB Output is correct
16 Correct 100 ms 30656 KB Output is correct
17 Correct 15 ms 24276 KB Output is correct
18 Correct 93 ms 30508 KB Output is correct
19 Correct 81 ms 30344 KB Output is correct
20 Correct 63 ms 30300 KB Output is correct
21 Correct 67 ms 30304 KB Output is correct
22 Correct 89 ms 30432 KB Output is correct
23 Correct 74 ms 30168 KB Output is correct
24 Correct 66 ms 30316 KB Output is correct
25 Correct 1496 ms 98048 KB Output is correct
26 Correct 1445 ms 104600 KB Output is correct
27 Correct 1248 ms 97572 KB Output is correct
28 Correct 1046 ms 101440 KB Output is correct
29 Correct 5581 ms 91624 KB Output is correct
30 Correct 4918 ms 95692 KB Output is correct
31 Correct 1525 ms 105504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 26312 KB Output is correct
2 Correct 70 ms 30852 KB Output is correct
3 Correct 73 ms 30528 KB Output is correct
4 Correct 115 ms 30616 KB Output is correct
5 Correct 15 ms 24276 KB Output is correct
6 Correct 74 ms 30488 KB Output is correct
7 Correct 60 ms 30172 KB Output is correct
8 Correct 62 ms 30140 KB Output is correct
9 Correct 60 ms 30088 KB Output is correct
10 Correct 99 ms 30328 KB Output is correct
11 Correct 66 ms 30072 KB Output is correct
12 Correct 67 ms 30180 KB Output is correct
13 Correct 77 ms 26268 KB Output is correct
14 Correct 85 ms 30844 KB Output is correct
15 Correct 73 ms 30452 KB Output is correct
16 Correct 100 ms 30656 KB Output is correct
17 Correct 15 ms 24276 KB Output is correct
18 Correct 93 ms 30508 KB Output is correct
19 Correct 81 ms 30344 KB Output is correct
20 Correct 63 ms 30300 KB Output is correct
21 Correct 67 ms 30304 KB Output is correct
22 Correct 89 ms 30432 KB Output is correct
23 Correct 74 ms 30168 KB Output is correct
24 Correct 66 ms 30316 KB Output is correct
25 Correct 1496 ms 98048 KB Output is correct
26 Correct 1445 ms 104600 KB Output is correct
27 Correct 1248 ms 97572 KB Output is correct
28 Correct 1046 ms 101440 KB Output is correct
29 Correct 5581 ms 91624 KB Output is correct
30 Correct 4918 ms 95692 KB Output is correct
31 Correct 1525 ms 105504 KB Output is correct
32 Correct 83 ms 26336 KB Output is correct
33 Correct 91 ms 30980 KB Output is correct
34 Correct 73 ms 30552 KB Output is correct
35 Correct 107 ms 30692 KB Output is correct
36 Correct 15 ms 24276 KB Output is correct
37 Correct 112 ms 30488 KB Output is correct
38 Correct 88 ms 30272 KB Output is correct
39 Correct 68 ms 30276 KB Output is correct
40 Correct 62 ms 30104 KB Output is correct
41 Correct 79 ms 30464 KB Output is correct
42 Correct 69 ms 30248 KB Output is correct
43 Correct 64 ms 30400 KB Output is correct
44 Correct 1504 ms 98228 KB Output is correct
45 Correct 1418 ms 104676 KB Output is correct
46 Correct 1323 ms 97512 KB Output is correct
47 Correct 1053 ms 101504 KB Output is correct
48 Correct 5593 ms 91756 KB Output is correct
49 Correct 5462 ms 95900 KB Output is correct
50 Correct 1494 ms 105612 KB Output is correct
51 Correct 2202 ms 168192 KB Output is correct
52 Correct 2715 ms 221696 KB Output is correct
53 Correct 2739 ms 175104 KB Output is correct
54 Correct 1057 ms 97044 KB Output is correct
55 Execution timed out 6064 ms 166908 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 83 ms 26368 KB Output is correct
2 Correct 68 ms 30772 KB Output is correct
3 Correct 68 ms 30496 KB Output is correct
4 Correct 97 ms 30532 KB Output is correct
5 Correct 14 ms 24204 KB Output is correct
6 Correct 92 ms 30388 KB Output is correct
7 Correct 73 ms 26312 KB Output is correct
8 Correct 70 ms 30852 KB Output is correct
9 Correct 73 ms 30528 KB Output is correct
10 Correct 115 ms 30616 KB Output is correct
11 Correct 15 ms 24276 KB Output is correct
12 Correct 74 ms 30488 KB Output is correct
13 Correct 60 ms 30172 KB Output is correct
14 Correct 62 ms 30140 KB Output is correct
15 Correct 60 ms 30088 KB Output is correct
16 Correct 99 ms 30328 KB Output is correct
17 Correct 66 ms 30072 KB Output is correct
18 Correct 67 ms 30180 KB Output is correct
19 Correct 13 ms 23764 KB Output is correct
20 Correct 14 ms 23728 KB Output is correct
21 Correct 13 ms 23764 KB Output is correct
22 Correct 79 ms 26404 KB Output is correct
23 Correct 85 ms 30932 KB Output is correct
24 Correct 76 ms 30668 KB Output is correct
25 Correct 101 ms 30764 KB Output is correct
26 Correct 15 ms 24276 KB Output is correct
27 Correct 99 ms 30596 KB Output is correct
28 Correct 73 ms 30272 KB Output is correct
29 Correct 63 ms 30268 KB Output is correct
30 Correct 60 ms 30144 KB Output is correct
31 Correct 83 ms 30456 KB Output is correct
32 Correct 86 ms 30144 KB Output is correct
33 Correct 67 ms 30288 KB Output is correct
34 Incorrect 1397 ms 97980 KB Output isn't correct
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 83 ms 26368 KB Output is correct
2 Correct 68 ms 30772 KB Output is correct
3 Correct 68 ms 30496 KB Output is correct
4 Correct 97 ms 30532 KB Output is correct
5 Correct 14 ms 24204 KB Output is correct
6 Correct 92 ms 30388 KB Output is correct
7 Correct 73 ms 26312 KB Output is correct
8 Correct 70 ms 30852 KB Output is correct
9 Correct 73 ms 30528 KB Output is correct
10 Correct 115 ms 30616 KB Output is correct
11 Correct 15 ms 24276 KB Output is correct
12 Correct 74 ms 30488 KB Output is correct
13 Correct 60 ms 30172 KB Output is correct
14 Correct 62 ms 30140 KB Output is correct
15 Correct 60 ms 30088 KB Output is correct
16 Correct 99 ms 30328 KB Output is correct
17 Correct 66 ms 30072 KB Output is correct
18 Correct 67 ms 30180 KB Output is correct
19 Correct 77 ms 26268 KB Output is correct
20 Correct 85 ms 30844 KB Output is correct
21 Correct 73 ms 30452 KB Output is correct
22 Correct 100 ms 30656 KB Output is correct
23 Correct 15 ms 24276 KB Output is correct
24 Correct 93 ms 30508 KB Output is correct
25 Correct 81 ms 30344 KB Output is correct
26 Correct 63 ms 30300 KB Output is correct
27 Correct 67 ms 30304 KB Output is correct
28 Correct 89 ms 30432 KB Output is correct
29 Correct 74 ms 30168 KB Output is correct
30 Correct 66 ms 30316 KB Output is correct
31 Correct 1496 ms 98048 KB Output is correct
32 Correct 1445 ms 104600 KB Output is correct
33 Correct 1248 ms 97572 KB Output is correct
34 Correct 1046 ms 101440 KB Output is correct
35 Correct 5581 ms 91624 KB Output is correct
36 Correct 4918 ms 95692 KB Output is correct
37 Correct 1525 ms 105504 KB Output is correct
38 Correct 13 ms 23764 KB Output is correct
39 Correct 14 ms 23728 KB Output is correct
40 Correct 13 ms 23764 KB Output is correct
41 Correct 79 ms 26404 KB Output is correct
42 Correct 85 ms 30932 KB Output is correct
43 Correct 76 ms 30668 KB Output is correct
44 Correct 101 ms 30764 KB Output is correct
45 Correct 15 ms 24276 KB Output is correct
46 Correct 99 ms 30596 KB Output is correct
47 Correct 73 ms 30272 KB Output is correct
48 Correct 63 ms 30268 KB Output is correct
49 Correct 60 ms 30144 KB Output is correct
50 Correct 83 ms 30456 KB Output is correct
51 Correct 86 ms 30144 KB Output is correct
52 Correct 67 ms 30288 KB Output is correct
53 Incorrect 1397 ms 97980 KB Output isn't correct
54 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 83 ms 26368 KB Output is correct
2 Correct 68 ms 30772 KB Output is correct
3 Correct 68 ms 30496 KB Output is correct
4 Correct 97 ms 30532 KB Output is correct
5 Correct 14 ms 24204 KB Output is correct
6 Correct 92 ms 30388 KB Output is correct
7 Correct 73 ms 26312 KB Output is correct
8 Correct 70 ms 30852 KB Output is correct
9 Correct 73 ms 30528 KB Output is correct
10 Correct 115 ms 30616 KB Output is correct
11 Correct 15 ms 24276 KB Output is correct
12 Correct 74 ms 30488 KB Output is correct
13 Correct 60 ms 30172 KB Output is correct
14 Correct 62 ms 30140 KB Output is correct
15 Correct 60 ms 30088 KB Output is correct
16 Correct 99 ms 30328 KB Output is correct
17 Correct 66 ms 30072 KB Output is correct
18 Correct 67 ms 30180 KB Output is correct
19 Correct 77 ms 26268 KB Output is correct
20 Correct 85 ms 30844 KB Output is correct
21 Correct 73 ms 30452 KB Output is correct
22 Correct 100 ms 30656 KB Output is correct
23 Correct 15 ms 24276 KB Output is correct
24 Correct 93 ms 30508 KB Output is correct
25 Correct 81 ms 30344 KB Output is correct
26 Correct 63 ms 30300 KB Output is correct
27 Correct 67 ms 30304 KB Output is correct
28 Correct 89 ms 30432 KB Output is correct
29 Correct 74 ms 30168 KB Output is correct
30 Correct 66 ms 30316 KB Output is correct
31 Correct 1496 ms 98048 KB Output is correct
32 Correct 1445 ms 104600 KB Output is correct
33 Correct 1248 ms 97572 KB Output is correct
34 Correct 1046 ms 101440 KB Output is correct
35 Correct 5581 ms 91624 KB Output is correct
36 Correct 4918 ms 95692 KB Output is correct
37 Correct 1525 ms 105504 KB Output is correct
38 Correct 83 ms 26336 KB Output is correct
39 Correct 91 ms 30980 KB Output is correct
40 Correct 73 ms 30552 KB Output is correct
41 Correct 107 ms 30692 KB Output is correct
42 Correct 15 ms 24276 KB Output is correct
43 Correct 112 ms 30488 KB Output is correct
44 Correct 88 ms 30272 KB Output is correct
45 Correct 68 ms 30276 KB Output is correct
46 Correct 62 ms 30104 KB Output is correct
47 Correct 79 ms 30464 KB Output is correct
48 Correct 69 ms 30248 KB Output is correct
49 Correct 64 ms 30400 KB Output is correct
50 Correct 1504 ms 98228 KB Output is correct
51 Correct 1418 ms 104676 KB Output is correct
52 Correct 1323 ms 97512 KB Output is correct
53 Correct 1053 ms 101504 KB Output is correct
54 Correct 5593 ms 91756 KB Output is correct
55 Correct 5462 ms 95900 KB Output is correct
56 Correct 1494 ms 105612 KB Output is correct
57 Correct 2202 ms 168192 KB Output is correct
58 Correct 2715 ms 221696 KB Output is correct
59 Correct 2739 ms 175104 KB Output is correct
60 Correct 1057 ms 97044 KB Output is correct
61 Execution timed out 6064 ms 166908 KB Time limit exceeded
62 Halted 0 ms 0 KB -