Submission #655836

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

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


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];
int maxd;
const int D = 3000;
vector<int> q[D];

priority_queue<pair<int, int>> pq;

void relax(int id, int d) {
  if (d < dp[id]) {
    dp[id] = d;
    q[d % D].push_back(id);
    maxd = max(maxd, d);
  }
}
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";
  }

  relax(single_vertex_id[1], 0);

  for (int cd = 0; cd <= maxd; cd++) {
    while (!q[cd % D].empty()) {
      int idv = q[cd % D].back(), d = cd;
      q[cd % D].pop_back();
      if (dp[idv] != d) {
        continue;
      }
      if (dp[idv] != d) {
        continue;
      }
      int vertex = what[idv].v;
      int r = what[idv].r;
      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
              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;
}
# Verdict Execution time Memory Grader output
1 Correct 64 ms 25916 KB Output is correct
2 Correct 65 ms 30540 KB Output is correct
3 Correct 59 ms 30288 KB Output is correct
4 Correct 85 ms 30520 KB Output is correct
5 Correct 12 ms 24468 KB Output is correct
6 Correct 61 ms 30228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 25928 KB Output is correct
2 Correct 61 ms 30500 KB Output is correct
3 Correct 60 ms 30252 KB Output is correct
4 Correct 90 ms 30540 KB Output is correct
5 Correct 13 ms 24428 KB Output is correct
6 Correct 63 ms 30308 KB Output is correct
7 Correct 59 ms 29808 KB Output is correct
8 Correct 53 ms 29760 KB Output is correct
9 Correct 81 ms 29704 KB Output is correct
10 Correct 67 ms 30032 KB Output is correct
11 Correct 71 ms 29896 KB Output is correct
12 Correct 58 ms 29888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 25928 KB Output is correct
2 Correct 61 ms 30500 KB Output is correct
3 Correct 60 ms 30252 KB Output is correct
4 Correct 90 ms 30540 KB Output is correct
5 Correct 13 ms 24428 KB Output is correct
6 Correct 63 ms 30308 KB Output is correct
7 Correct 59 ms 29808 KB Output is correct
8 Correct 53 ms 29760 KB Output is correct
9 Correct 81 ms 29704 KB Output is correct
10 Correct 67 ms 30032 KB Output is correct
11 Correct 71 ms 29896 KB Output is correct
12 Correct 58 ms 29888 KB Output is correct
13 Correct 70 ms 25940 KB Output is correct
14 Correct 62 ms 30516 KB Output is correct
15 Correct 76 ms 30220 KB Output is correct
16 Correct 86 ms 30592 KB Output is correct
17 Correct 13 ms 24404 KB Output is correct
18 Correct 71 ms 30288 KB Output is correct
19 Correct 54 ms 29892 KB Output is correct
20 Correct 54 ms 29760 KB Output is correct
21 Correct 53 ms 29752 KB Output is correct
22 Correct 85 ms 30016 KB Output is correct
23 Correct 60 ms 29824 KB Output is correct
24 Correct 64 ms 29904 KB Output is correct
25 Correct 1366 ms 97240 KB Output is correct
26 Correct 1301 ms 103616 KB Output is correct
27 Correct 1232 ms 96928 KB Output is correct
28 Correct 946 ms 100704 KB Output is correct
29 Correct 5057 ms 90172 KB Output is correct
30 Correct 4494 ms 94804 KB Output is correct
31 Correct 1419 ms 104444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 25928 KB Output is correct
2 Correct 61 ms 30500 KB Output is correct
3 Correct 60 ms 30252 KB Output is correct
4 Correct 90 ms 30540 KB Output is correct
5 Correct 13 ms 24428 KB Output is correct
6 Correct 63 ms 30308 KB Output is correct
7 Correct 59 ms 29808 KB Output is correct
8 Correct 53 ms 29760 KB Output is correct
9 Correct 81 ms 29704 KB Output is correct
10 Correct 67 ms 30032 KB Output is correct
11 Correct 71 ms 29896 KB Output is correct
12 Correct 58 ms 29888 KB Output is correct
13 Correct 70 ms 25940 KB Output is correct
14 Correct 62 ms 30516 KB Output is correct
15 Correct 76 ms 30220 KB Output is correct
16 Correct 86 ms 30592 KB Output is correct
17 Correct 13 ms 24404 KB Output is correct
18 Correct 71 ms 30288 KB Output is correct
19 Correct 54 ms 29892 KB Output is correct
20 Correct 54 ms 29760 KB Output is correct
21 Correct 53 ms 29752 KB Output is correct
22 Correct 85 ms 30016 KB Output is correct
23 Correct 60 ms 29824 KB Output is correct
24 Correct 64 ms 29904 KB Output is correct
25 Correct 1366 ms 97240 KB Output is correct
26 Correct 1301 ms 103616 KB Output is correct
27 Correct 1232 ms 96928 KB Output is correct
28 Correct 946 ms 100704 KB Output is correct
29 Correct 5057 ms 90172 KB Output is correct
30 Correct 4494 ms 94804 KB Output is correct
31 Correct 1419 ms 104444 KB Output is correct
32 Correct 71 ms 26052 KB Output is correct
33 Correct 68 ms 30428 KB Output is correct
34 Correct 61 ms 30212 KB Output is correct
35 Correct 93 ms 30492 KB Output is correct
36 Correct 13 ms 24404 KB Output is correct
37 Correct 63 ms 30264 KB Output is correct
38 Correct 56 ms 29876 KB Output is correct
39 Correct 58 ms 29760 KB Output is correct
40 Correct 56 ms 29724 KB Output is correct
41 Correct 92 ms 29956 KB Output is correct
42 Correct 56 ms 29896 KB Output is correct
43 Correct 53 ms 29988 KB Output is correct
44 Correct 1337 ms 97220 KB Output is correct
45 Correct 1285 ms 103552 KB Output is correct
46 Correct 1165 ms 96816 KB Output is correct
47 Correct 943 ms 100712 KB Output is correct
48 Correct 4150 ms 90244 KB Output is correct
49 Correct 4555 ms 94812 KB Output is correct
50 Correct 1400 ms 107500 KB Output is correct
51 Correct 1742 ms 182332 KB Output is correct
52 Correct 1943 ms 239152 KB Output is correct
53 Correct 2005 ms 196492 KB Output is correct
54 Correct 936 ms 100680 KB Output is correct
55 Execution timed out 6086 ms 168180 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 64 ms 25916 KB Output is correct
2 Correct 65 ms 30540 KB Output is correct
3 Correct 59 ms 30288 KB Output is correct
4 Correct 85 ms 30520 KB Output is correct
5 Correct 12 ms 24468 KB Output is correct
6 Correct 61 ms 30228 KB Output is correct
7 Correct 71 ms 25928 KB Output is correct
8 Correct 61 ms 30500 KB Output is correct
9 Correct 60 ms 30252 KB Output is correct
10 Correct 90 ms 30540 KB Output is correct
11 Correct 13 ms 24428 KB Output is correct
12 Correct 63 ms 30308 KB Output is correct
13 Correct 59 ms 29808 KB Output is correct
14 Correct 53 ms 29760 KB Output is correct
15 Correct 81 ms 29704 KB Output is correct
16 Correct 67 ms 30032 KB Output is correct
17 Correct 71 ms 29896 KB Output is correct
18 Correct 58 ms 29888 KB Output is correct
19 Correct 12 ms 23908 KB Output is correct
20 Correct 13 ms 23892 KB Output is correct
21 Correct 12 ms 23892 KB Output is correct
22 Correct 70 ms 25964 KB Output is correct
23 Correct 72 ms 30504 KB Output is correct
24 Correct 64 ms 30272 KB Output is correct
25 Correct 81 ms 30520 KB Output is correct
26 Correct 13 ms 24404 KB Output is correct
27 Correct 59 ms 30276 KB Output is correct
28 Correct 54 ms 29812 KB Output is correct
29 Correct 59 ms 29884 KB Output is correct
30 Correct 55 ms 29760 KB Output is correct
31 Correct 65 ms 29964 KB Output is correct
32 Correct 58 ms 29848 KB Output is correct
33 Correct 64 ms 29956 KB Output is correct
34 Correct 1346 ms 96880 KB Output is correct
35 Correct 1376 ms 92768 KB Output is correct
36 Correct 1384 ms 92704 KB Output is correct
37 Correct 1243 ms 100088 KB Output is correct
38 Correct 1299 ms 95772 KB Output is correct
39 Correct 2843 ms 90164 KB Output is correct
40 Correct 2250 ms 90356 KB Output is correct
41 Correct 1488 ms 90052 KB Output is correct
42 Correct 1335 ms 95904 KB Output is correct
43 Correct 1396 ms 101508 KB Output is correct
44 Correct 1409 ms 101684 KB Output is correct
45 Correct 1386 ms 94744 KB Output is correct
46 Correct 1314 ms 95768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 25916 KB Output is correct
2 Correct 65 ms 30540 KB Output is correct
3 Correct 59 ms 30288 KB Output is correct
4 Correct 85 ms 30520 KB Output is correct
5 Correct 12 ms 24468 KB Output is correct
6 Correct 61 ms 30228 KB Output is correct
7 Correct 71 ms 25928 KB Output is correct
8 Correct 61 ms 30500 KB Output is correct
9 Correct 60 ms 30252 KB Output is correct
10 Correct 90 ms 30540 KB Output is correct
11 Correct 13 ms 24428 KB Output is correct
12 Correct 63 ms 30308 KB Output is correct
13 Correct 59 ms 29808 KB Output is correct
14 Correct 53 ms 29760 KB Output is correct
15 Correct 81 ms 29704 KB Output is correct
16 Correct 67 ms 30032 KB Output is correct
17 Correct 71 ms 29896 KB Output is correct
18 Correct 58 ms 29888 KB Output is correct
19 Correct 70 ms 25940 KB Output is correct
20 Correct 62 ms 30516 KB Output is correct
21 Correct 76 ms 30220 KB Output is correct
22 Correct 86 ms 30592 KB Output is correct
23 Correct 13 ms 24404 KB Output is correct
24 Correct 71 ms 30288 KB Output is correct
25 Correct 54 ms 29892 KB Output is correct
26 Correct 54 ms 29760 KB Output is correct
27 Correct 53 ms 29752 KB Output is correct
28 Correct 85 ms 30016 KB Output is correct
29 Correct 60 ms 29824 KB Output is correct
30 Correct 64 ms 29904 KB Output is correct
31 Correct 1366 ms 97240 KB Output is correct
32 Correct 1301 ms 103616 KB Output is correct
33 Correct 1232 ms 96928 KB Output is correct
34 Correct 946 ms 100704 KB Output is correct
35 Correct 5057 ms 90172 KB Output is correct
36 Correct 4494 ms 94804 KB Output is correct
37 Correct 1419 ms 104444 KB Output is correct
38 Correct 12 ms 23908 KB Output is correct
39 Correct 13 ms 23892 KB Output is correct
40 Correct 12 ms 23892 KB Output is correct
41 Correct 70 ms 25964 KB Output is correct
42 Correct 72 ms 30504 KB Output is correct
43 Correct 64 ms 30272 KB Output is correct
44 Correct 81 ms 30520 KB Output is correct
45 Correct 13 ms 24404 KB Output is correct
46 Correct 59 ms 30276 KB Output is correct
47 Correct 54 ms 29812 KB Output is correct
48 Correct 59 ms 29884 KB Output is correct
49 Correct 55 ms 29760 KB Output is correct
50 Correct 65 ms 29964 KB Output is correct
51 Correct 58 ms 29848 KB Output is correct
52 Correct 64 ms 29956 KB Output is correct
53 Correct 1346 ms 96880 KB Output is correct
54 Correct 1376 ms 92768 KB Output is correct
55 Correct 1384 ms 92704 KB Output is correct
56 Correct 1243 ms 100088 KB Output is correct
57 Correct 1299 ms 95772 KB Output is correct
58 Correct 2843 ms 90164 KB Output is correct
59 Correct 2250 ms 90356 KB Output is correct
60 Correct 1488 ms 90052 KB Output is correct
61 Correct 1335 ms 95904 KB Output is correct
62 Correct 1396 ms 101508 KB Output is correct
63 Correct 1409 ms 101684 KB Output is correct
64 Correct 1386 ms 94744 KB Output is correct
65 Correct 1314 ms 95768 KB Output is correct
66 Correct 12 ms 23892 KB Output is correct
67 Correct 12 ms 23892 KB Output is correct
68 Correct 12 ms 23892 KB Output is correct
69 Correct 68 ms 25972 KB Output is correct
70 Correct 66 ms 30524 KB Output is correct
71 Correct 66 ms 30276 KB Output is correct
72 Correct 86 ms 30600 KB Output is correct
73 Correct 14 ms 24444 KB Output is correct
74 Correct 63 ms 30192 KB Output is correct
75 Correct 55 ms 29788 KB Output is correct
76 Correct 54 ms 29800 KB Output is correct
77 Correct 55 ms 29748 KB Output is correct
78 Correct 73 ms 29984 KB Output is correct
79 Correct 60 ms 29832 KB Output is correct
80 Correct 61 ms 29956 KB Output is correct
81 Correct 1335 ms 97244 KB Output is correct
82 Correct 1221 ms 103748 KB Output is correct
83 Correct 1189 ms 96888 KB Output is correct
84 Correct 959 ms 100856 KB Output is correct
85 Correct 4407 ms 90168 KB Output is correct
86 Correct 4348 ms 95000 KB Output is correct
87 Correct 1395 ms 104648 KB Output is correct
88 Correct 1343 ms 96912 KB Output is correct
89 Correct 1418 ms 92920 KB Output is correct
90 Correct 1383 ms 92736 KB Output is correct
91 Correct 1239 ms 99768 KB Output is correct
92 Correct 1321 ms 95776 KB Output is correct
93 Correct 2910 ms 89952 KB Output is correct
94 Correct 2314 ms 90396 KB Output is correct
95 Correct 1550 ms 90036 KB Output is correct
96 Correct 1369 ms 95748 KB Output is correct
97 Correct 1403 ms 101344 KB Output is correct
98 Correct 1381 ms 101780 KB Output is correct
99 Correct 1405 ms 94956 KB Output is correct
100 Correct 1310 ms 95664 KB Output is correct
101 Correct 1348 ms 97644 KB Output is correct
102 Correct 1227 ms 100960 KB Output is correct
103 Correct 1346 ms 97712 KB Output is correct
104 Execution timed out 6068 ms 99060 KB Time limit exceeded
105 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 64 ms 25916 KB Output is correct
2 Correct 65 ms 30540 KB Output is correct
3 Correct 59 ms 30288 KB Output is correct
4 Correct 85 ms 30520 KB Output is correct
5 Correct 12 ms 24468 KB Output is correct
6 Correct 61 ms 30228 KB Output is correct
7 Correct 71 ms 25928 KB Output is correct
8 Correct 61 ms 30500 KB Output is correct
9 Correct 60 ms 30252 KB Output is correct
10 Correct 90 ms 30540 KB Output is correct
11 Correct 13 ms 24428 KB Output is correct
12 Correct 63 ms 30308 KB Output is correct
13 Correct 59 ms 29808 KB Output is correct
14 Correct 53 ms 29760 KB Output is correct
15 Correct 81 ms 29704 KB Output is correct
16 Correct 67 ms 30032 KB Output is correct
17 Correct 71 ms 29896 KB Output is correct
18 Correct 58 ms 29888 KB Output is correct
19 Correct 70 ms 25940 KB Output is correct
20 Correct 62 ms 30516 KB Output is correct
21 Correct 76 ms 30220 KB Output is correct
22 Correct 86 ms 30592 KB Output is correct
23 Correct 13 ms 24404 KB Output is correct
24 Correct 71 ms 30288 KB Output is correct
25 Correct 54 ms 29892 KB Output is correct
26 Correct 54 ms 29760 KB Output is correct
27 Correct 53 ms 29752 KB Output is correct
28 Correct 85 ms 30016 KB Output is correct
29 Correct 60 ms 29824 KB Output is correct
30 Correct 64 ms 29904 KB Output is correct
31 Correct 1366 ms 97240 KB Output is correct
32 Correct 1301 ms 103616 KB Output is correct
33 Correct 1232 ms 96928 KB Output is correct
34 Correct 946 ms 100704 KB Output is correct
35 Correct 5057 ms 90172 KB Output is correct
36 Correct 4494 ms 94804 KB Output is correct
37 Correct 1419 ms 104444 KB Output is correct
38 Correct 71 ms 26052 KB Output is correct
39 Correct 68 ms 30428 KB Output is correct
40 Correct 61 ms 30212 KB Output is correct
41 Correct 93 ms 30492 KB Output is correct
42 Correct 13 ms 24404 KB Output is correct
43 Correct 63 ms 30264 KB Output is correct
44 Correct 56 ms 29876 KB Output is correct
45 Correct 58 ms 29760 KB Output is correct
46 Correct 56 ms 29724 KB Output is correct
47 Correct 92 ms 29956 KB Output is correct
48 Correct 56 ms 29896 KB Output is correct
49 Correct 53 ms 29988 KB Output is correct
50 Correct 1337 ms 97220 KB Output is correct
51 Correct 1285 ms 103552 KB Output is correct
52 Correct 1165 ms 96816 KB Output is correct
53 Correct 943 ms 100712 KB Output is correct
54 Correct 4150 ms 90244 KB Output is correct
55 Correct 4555 ms 94812 KB Output is correct
56 Correct 1400 ms 107500 KB Output is correct
57 Correct 1742 ms 182332 KB Output is correct
58 Correct 1943 ms 239152 KB Output is correct
59 Correct 2005 ms 196492 KB Output is correct
60 Correct 936 ms 100680 KB Output is correct
61 Execution timed out 6086 ms 168180 KB Time limit exceeded
62 Halted 0 ms 0 KB -