Submission #655835

# Submission time Handle Problem Language Result Execution time Memory
655835 2022-11-05T18:26:22 Z 600Mihnea From Hacks to Snitches (BOI21_watchmen) C++17
50 / 100
6000 ms 232516 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];
const int D = 3000;
vector<int> q[D];
int maxd = 0;
int curd = 0;

void relax(int id, int d) {
  if (!(d - curd < D)) {
    cout << " : " << d - curd << ", too big" << "\n";
    exit(0);
  }
  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) };
      }
    }
  }
  if (verbose) {
    cout << "y = " << y << "\n";
    cout << "################################################################################### \n";
  }



  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 };
    }
  }

  for (int i = 1; i <= y; i++) {
    dp[i] = INF;
  }
  relax(single_vertex_id[1], 0);

  for (int cd = 0; cd <= maxd; cd++) {
    curd = cd;
    while (!q[cd % D].empty()) {
      int idv = q[cd % D].back(), d = cd;
      q[cd % D].pop_back();
      if (dp[idv] != d) {
        continue;
      }
      int vertex = what[idv].v;
      int r = what[idv].r;
      if (vertex < 0) {
        //  cout << "aici\n";
        int p = vertex;
        p *= -1;
        p--;
        int vertex = all[p].first, vecin = all[p].second;
        int l = cycles[cycle_id[vertex]].size();
        int l_vecin = cycles[cycle_id[vecin]].size();
        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;
      }
      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
              int p = spec[{vertex, vecin}];
              //  cout << "gest:" << (int)pq.size()<<" "<< ids[p][(d + 1) % l_vecin]<< " vs "<<1<<"..."<<y << "\n";
              relax(ids[p][(d + 1) % l_vecin], d + 1);
            }
          }
        }
        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:145:9: warning: unused variable 'vertex' [-Wunused-variable]
  145 |     int vertex = it.first.first, vecin = it.first.second;
      |         ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 78 ms 26556 KB Output is correct
2 Correct 83 ms 31680 KB Output is correct
3 Correct 84 ms 31348 KB Output is correct
4 Correct 117 ms 31680 KB Output is correct
5 Correct 15 ms 24404 KB Output is correct
6 Correct 84 ms 31368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 26484 KB Output is correct
2 Correct 95 ms 31632 KB Output is correct
3 Correct 88 ms 31420 KB Output is correct
4 Correct 115 ms 31692 KB Output is correct
5 Correct 13 ms 24404 KB Output is correct
6 Correct 66 ms 31424 KB Output is correct
7 Correct 70 ms 30968 KB Output is correct
8 Correct 78 ms 30972 KB Output is correct
9 Correct 72 ms 30872 KB Output is correct
10 Correct 77 ms 31152 KB Output is correct
11 Correct 66 ms 31044 KB Output is correct
12 Correct 75 ms 31040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 26484 KB Output is correct
2 Correct 95 ms 31632 KB Output is correct
3 Correct 88 ms 31420 KB Output is correct
4 Correct 115 ms 31692 KB Output is correct
5 Correct 13 ms 24404 KB Output is correct
6 Correct 66 ms 31424 KB Output is correct
7 Correct 70 ms 30968 KB Output is correct
8 Correct 78 ms 30972 KB Output is correct
9 Correct 72 ms 30872 KB Output is correct
10 Correct 77 ms 31152 KB Output is correct
11 Correct 66 ms 31044 KB Output is correct
12 Correct 75 ms 31040 KB Output is correct
13 Correct 87 ms 26668 KB Output is correct
14 Correct 82 ms 31672 KB Output is correct
15 Correct 78 ms 31332 KB Output is correct
16 Correct 90 ms 31664 KB Output is correct
17 Correct 15 ms 24476 KB Output is correct
18 Correct 79 ms 31372 KB Output is correct
19 Correct 64 ms 30972 KB Output is correct
20 Correct 56 ms 30892 KB Output is correct
21 Correct 62 ms 30792 KB Output is correct
22 Correct 70 ms 31168 KB Output is correct
23 Correct 72 ms 31064 KB Output is correct
24 Correct 68 ms 31052 KB Output is correct
25 Correct 1457 ms 100028 KB Output is correct
26 Correct 1384 ms 106456 KB Output is correct
27 Correct 1289 ms 99660 KB Output is correct
28 Correct 1016 ms 103620 KB Output is correct
29 Correct 5300 ms 93000 KB Output is correct
30 Correct 5617 ms 97184 KB Output is correct
31 Correct 1511 ms 107532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 26484 KB Output is correct
2 Correct 95 ms 31632 KB Output is correct
3 Correct 88 ms 31420 KB Output is correct
4 Correct 115 ms 31692 KB Output is correct
5 Correct 13 ms 24404 KB Output is correct
6 Correct 66 ms 31424 KB Output is correct
7 Correct 70 ms 30968 KB Output is correct
8 Correct 78 ms 30972 KB Output is correct
9 Correct 72 ms 30872 KB Output is correct
10 Correct 77 ms 31152 KB Output is correct
11 Correct 66 ms 31044 KB Output is correct
12 Correct 75 ms 31040 KB Output is correct
13 Correct 87 ms 26668 KB Output is correct
14 Correct 82 ms 31672 KB Output is correct
15 Correct 78 ms 31332 KB Output is correct
16 Correct 90 ms 31664 KB Output is correct
17 Correct 15 ms 24476 KB Output is correct
18 Correct 79 ms 31372 KB Output is correct
19 Correct 64 ms 30972 KB Output is correct
20 Correct 56 ms 30892 KB Output is correct
21 Correct 62 ms 30792 KB Output is correct
22 Correct 70 ms 31168 KB Output is correct
23 Correct 72 ms 31064 KB Output is correct
24 Correct 68 ms 31052 KB Output is correct
25 Correct 1457 ms 100028 KB Output is correct
26 Correct 1384 ms 106456 KB Output is correct
27 Correct 1289 ms 99660 KB Output is correct
28 Correct 1016 ms 103620 KB Output is correct
29 Correct 5300 ms 93000 KB Output is correct
30 Correct 5617 ms 97184 KB Output is correct
31 Correct 1511 ms 107532 KB Output is correct
32 Correct 78 ms 26560 KB Output is correct
33 Correct 79 ms 31704 KB Output is correct
34 Correct 66 ms 31452 KB Output is correct
35 Correct 122 ms 31692 KB Output is correct
36 Correct 15 ms 24368 KB Output is correct
37 Correct 71 ms 31396 KB Output is correct
38 Correct 64 ms 30976 KB Output is correct
39 Correct 67 ms 30944 KB Output is correct
40 Correct 64 ms 30880 KB Output is correct
41 Correct 88 ms 31084 KB Output is correct
42 Correct 66 ms 31036 KB Output is correct
43 Correct 56 ms 31052 KB Output is correct
44 Correct 1471 ms 100088 KB Output is correct
45 Correct 1373 ms 106576 KB Output is correct
46 Correct 1352 ms 99900 KB Output is correct
47 Correct 1074 ms 103452 KB Output is correct
48 Correct 5782 ms 93236 KB Output is correct
49 Execution timed out 6068 ms 97412 KB Time limit exceeded
50 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 78 ms 26556 KB Output is correct
2 Correct 83 ms 31680 KB Output is correct
3 Correct 84 ms 31348 KB Output is correct
4 Correct 117 ms 31680 KB Output is correct
5 Correct 15 ms 24404 KB Output is correct
6 Correct 84 ms 31368 KB Output is correct
7 Correct 88 ms 26484 KB Output is correct
8 Correct 95 ms 31632 KB Output is correct
9 Correct 88 ms 31420 KB Output is correct
10 Correct 115 ms 31692 KB Output is correct
11 Correct 13 ms 24404 KB Output is correct
12 Correct 66 ms 31424 KB Output is correct
13 Correct 70 ms 30968 KB Output is correct
14 Correct 78 ms 30972 KB Output is correct
15 Correct 72 ms 30872 KB Output is correct
16 Correct 77 ms 31152 KB Output is correct
17 Correct 66 ms 31044 KB Output is correct
18 Correct 75 ms 31040 KB Output is correct
19 Correct 13 ms 23892 KB Output is correct
20 Correct 14 ms 23804 KB Output is correct
21 Correct 13 ms 24008 KB Output is correct
22 Correct 71 ms 26564 KB Output is correct
23 Correct 69 ms 31672 KB Output is correct
24 Correct 78 ms 31424 KB Output is correct
25 Correct 99 ms 31740 KB Output is correct
26 Correct 14 ms 24400 KB Output is correct
27 Correct 63 ms 31392 KB Output is correct
28 Correct 58 ms 31036 KB Output is correct
29 Correct 58 ms 30948 KB Output is correct
30 Correct 81 ms 30880 KB Output is correct
31 Correct 73 ms 31160 KB Output is correct
32 Correct 62 ms 31056 KB Output is correct
33 Correct 64 ms 31140 KB Output is correct
34 Correct 1425 ms 99648 KB Output is correct
35 Correct 1471 ms 96000 KB Output is correct
36 Correct 1432 ms 95788 KB Output is correct
37 Correct 1349 ms 102804 KB Output is correct
38 Correct 1378 ms 96292 KB Output is correct
39 Correct 3055 ms 91048 KB Output is correct
40 Correct 2412 ms 101164 KB Output is correct
41 Correct 1700 ms 95436 KB Output is correct
42 Correct 1337 ms 97608 KB Output is correct
43 Correct 1393 ms 101996 KB Output is correct
44 Correct 1361 ms 102288 KB Output is correct
45 Correct 1449 ms 95908 KB Output is correct
46 Correct 1324 ms 96980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 26556 KB Output is correct
2 Correct 83 ms 31680 KB Output is correct
3 Correct 84 ms 31348 KB Output is correct
4 Correct 117 ms 31680 KB Output is correct
5 Correct 15 ms 24404 KB Output is correct
6 Correct 84 ms 31368 KB Output is correct
7 Correct 88 ms 26484 KB Output is correct
8 Correct 95 ms 31632 KB Output is correct
9 Correct 88 ms 31420 KB Output is correct
10 Correct 115 ms 31692 KB Output is correct
11 Correct 13 ms 24404 KB Output is correct
12 Correct 66 ms 31424 KB Output is correct
13 Correct 70 ms 30968 KB Output is correct
14 Correct 78 ms 30972 KB Output is correct
15 Correct 72 ms 30872 KB Output is correct
16 Correct 77 ms 31152 KB Output is correct
17 Correct 66 ms 31044 KB Output is correct
18 Correct 75 ms 31040 KB Output is correct
19 Correct 87 ms 26668 KB Output is correct
20 Correct 82 ms 31672 KB Output is correct
21 Correct 78 ms 31332 KB Output is correct
22 Correct 90 ms 31664 KB Output is correct
23 Correct 15 ms 24476 KB Output is correct
24 Correct 79 ms 31372 KB Output is correct
25 Correct 64 ms 30972 KB Output is correct
26 Correct 56 ms 30892 KB Output is correct
27 Correct 62 ms 30792 KB Output is correct
28 Correct 70 ms 31168 KB Output is correct
29 Correct 72 ms 31064 KB Output is correct
30 Correct 68 ms 31052 KB Output is correct
31 Correct 1457 ms 100028 KB Output is correct
32 Correct 1384 ms 106456 KB Output is correct
33 Correct 1289 ms 99660 KB Output is correct
34 Correct 1016 ms 103620 KB Output is correct
35 Correct 5300 ms 93000 KB Output is correct
36 Correct 5617 ms 97184 KB Output is correct
37 Correct 1511 ms 107532 KB Output is correct
38 Correct 13 ms 23892 KB Output is correct
39 Correct 14 ms 23804 KB Output is correct
40 Correct 13 ms 24008 KB Output is correct
41 Correct 71 ms 26564 KB Output is correct
42 Correct 69 ms 31672 KB Output is correct
43 Correct 78 ms 31424 KB Output is correct
44 Correct 99 ms 31740 KB Output is correct
45 Correct 14 ms 24400 KB Output is correct
46 Correct 63 ms 31392 KB Output is correct
47 Correct 58 ms 31036 KB Output is correct
48 Correct 58 ms 30948 KB Output is correct
49 Correct 81 ms 30880 KB Output is correct
50 Correct 73 ms 31160 KB Output is correct
51 Correct 62 ms 31056 KB Output is correct
52 Correct 64 ms 31140 KB Output is correct
53 Correct 1425 ms 99648 KB Output is correct
54 Correct 1471 ms 96000 KB Output is correct
55 Correct 1432 ms 95788 KB Output is correct
56 Correct 1349 ms 102804 KB Output is correct
57 Correct 1378 ms 96292 KB Output is correct
58 Correct 3055 ms 91048 KB Output is correct
59 Correct 2412 ms 101164 KB Output is correct
60 Correct 1700 ms 95436 KB Output is correct
61 Correct 1337 ms 97608 KB Output is correct
62 Correct 1393 ms 101996 KB Output is correct
63 Correct 1361 ms 102288 KB Output is correct
64 Correct 1449 ms 95908 KB Output is correct
65 Correct 1324 ms 96980 KB Output is correct
66 Correct 14 ms 23892 KB Output is correct
67 Correct 12 ms 23892 KB Output is correct
68 Correct 13 ms 23892 KB Output is correct
69 Correct 71 ms 26536 KB Output is correct
70 Correct 79 ms 31632 KB Output is correct
71 Correct 68 ms 31336 KB Output is correct
72 Correct 91 ms 31684 KB Output is correct
73 Correct 14 ms 24404 KB Output is correct
74 Correct 65 ms 31372 KB Output is correct
75 Correct 56 ms 31040 KB Output is correct
76 Correct 63 ms 30912 KB Output is correct
77 Correct 58 ms 30820 KB Output is correct
78 Correct 68 ms 31164 KB Output is correct
79 Correct 60 ms 30952 KB Output is correct
80 Correct 67 ms 31152 KB Output is correct
81 Correct 1365 ms 99808 KB Output is correct
82 Correct 1313 ms 106172 KB Output is correct
83 Correct 1215 ms 100040 KB Output is correct
84 Correct 978 ms 103688 KB Output is correct
85 Correct 4966 ms 92880 KB Output is correct
86 Correct 5539 ms 97640 KB Output is correct
87 Correct 1397 ms 107376 KB Output is correct
88 Correct 1343 ms 99648 KB Output is correct
89 Correct 1391 ms 95776 KB Output is correct
90 Correct 1437 ms 95416 KB Output is correct
91 Correct 1286 ms 102788 KB Output is correct
92 Correct 1348 ms 98672 KB Output is correct
93 Correct 3735 ms 92960 KB Output is correct
94 Correct 2503 ms 102764 KB Output is correct
95 Correct 1680 ms 97364 KB Output is correct
96 Correct 1385 ms 99788 KB Output is correct
97 Correct 1418 ms 104536 KB Output is correct
98 Correct 1428 ms 104804 KB Output is correct
99 Correct 1426 ms 98164 KB Output is correct
100 Correct 1356 ms 98744 KB Output is correct
101 Correct 1349 ms 100816 KB Output is correct
102 Correct 1243 ms 103632 KB Output is correct
103 Correct 1369 ms 100064 KB Output is correct
104 Execution timed out 6101 ms 232516 KB Time limit exceeded
105 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 78 ms 26556 KB Output is correct
2 Correct 83 ms 31680 KB Output is correct
3 Correct 84 ms 31348 KB Output is correct
4 Correct 117 ms 31680 KB Output is correct
5 Correct 15 ms 24404 KB Output is correct
6 Correct 84 ms 31368 KB Output is correct
7 Correct 88 ms 26484 KB Output is correct
8 Correct 95 ms 31632 KB Output is correct
9 Correct 88 ms 31420 KB Output is correct
10 Correct 115 ms 31692 KB Output is correct
11 Correct 13 ms 24404 KB Output is correct
12 Correct 66 ms 31424 KB Output is correct
13 Correct 70 ms 30968 KB Output is correct
14 Correct 78 ms 30972 KB Output is correct
15 Correct 72 ms 30872 KB Output is correct
16 Correct 77 ms 31152 KB Output is correct
17 Correct 66 ms 31044 KB Output is correct
18 Correct 75 ms 31040 KB Output is correct
19 Correct 87 ms 26668 KB Output is correct
20 Correct 82 ms 31672 KB Output is correct
21 Correct 78 ms 31332 KB Output is correct
22 Correct 90 ms 31664 KB Output is correct
23 Correct 15 ms 24476 KB Output is correct
24 Correct 79 ms 31372 KB Output is correct
25 Correct 64 ms 30972 KB Output is correct
26 Correct 56 ms 30892 KB Output is correct
27 Correct 62 ms 30792 KB Output is correct
28 Correct 70 ms 31168 KB Output is correct
29 Correct 72 ms 31064 KB Output is correct
30 Correct 68 ms 31052 KB Output is correct
31 Correct 1457 ms 100028 KB Output is correct
32 Correct 1384 ms 106456 KB Output is correct
33 Correct 1289 ms 99660 KB Output is correct
34 Correct 1016 ms 103620 KB Output is correct
35 Correct 5300 ms 93000 KB Output is correct
36 Correct 5617 ms 97184 KB Output is correct
37 Correct 1511 ms 107532 KB Output is correct
38 Correct 78 ms 26560 KB Output is correct
39 Correct 79 ms 31704 KB Output is correct
40 Correct 66 ms 31452 KB Output is correct
41 Correct 122 ms 31692 KB Output is correct
42 Correct 15 ms 24368 KB Output is correct
43 Correct 71 ms 31396 KB Output is correct
44 Correct 64 ms 30976 KB Output is correct
45 Correct 67 ms 30944 KB Output is correct
46 Correct 64 ms 30880 KB Output is correct
47 Correct 88 ms 31084 KB Output is correct
48 Correct 66 ms 31036 KB Output is correct
49 Correct 56 ms 31052 KB Output is correct
50 Correct 1471 ms 100088 KB Output is correct
51 Correct 1373 ms 106576 KB Output is correct
52 Correct 1352 ms 99900 KB Output is correct
53 Correct 1074 ms 103452 KB Output is correct
54 Correct 5782 ms 93236 KB Output is correct
55 Execution timed out 6068 ms 97412 KB Time limit exceeded
56 Halted 0 ms 0 KB -