Submission #426383

# Submission time Handle Problem Language Result Execution time Memory
426383 2021-06-13T21:48:56 Z duality From Hacks to Snitches (BOI21_watchmen) C++11
25 / 100
6000 ms 135452 KB
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
typedef long long int LLI;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
int readInt() {
    char c;
    int n = 0;
    while ((c=getchar()) < '0');
    n = c-'0';
    while ((c=getchar()) >= '0') n = n*10+c-'0';
    return n;
}

vi adjList[250000];
int pos[250000];
pair<int,pii> p[250000];
bool comp(int a,int b) {
    return p[a] < p[b];
}
vi dist[250000];
int done[250000],done2[250000];
vi Q[1000000];
int main() {
    int i,j;
    int N,M,K;
    int u,v,l;
    scanf("%d %d",&N,&M);
    for (i = 0; i < M; i++) {
        u = readInt();
        v = readInt();
        u--,v--;
        adjList[u].pb(v);
        adjList[v].pb(u);
    }
    scanf("%d",&K);
    for (i = 0; i < K; i++) {
        scanf("%d",&l);
        for (j = 0; j < l; j++) scanf("%d",&v),p[v-1] = mp(l,mp(j,i));
    }

    for (i = 0; i < N; i++) {
        if (p[i].first == 0) p[i].first = 1;
        dist[i].resize(p[i].first);
        fill(dist[i].begin(),dist[i].end(),-1);
        sort(adjList[i].begin(),adjList[i].end(),comp);
        while ((pos[i] < adjList[i].size()) && (p[adjList[i][pos[i]]].first <= 1)) pos[i]++;
    }
    int pp = 0;
    dist[0][0] = 0,Q[0].pb(0);
    while (1) {
        while ((pp < 1000000) && (Q[pp].empty())) pp++;
        if (pp == 1000000) break;
        int u = Q[pp].back();
        int d = pp;
        Q[pp].pop_back();

        if (d > dist[u][d % p[u].first]) continue;
        else if (u == N-1) {
            printf("%d\n",d);
            return 0;
        }
        i = 0;
        if (done2[u]) i = pos[u];
        for (; i < adjList[u].size(); i++) {
            int v = adjList[u][i],t = d;
            for (j = 0; j < ((p[u].first == 1) ? p[v].first:((p[u].second.second == p[v].second.second) ? 1:p[v].first)); j++) {
                if (done[v] && (p[u].first == 1)) break;
                int bad = 0;
                if (p[v].first != 1) {
                    if (((d+1) % p[v].first) == p[v].second.first) bad = 1;
                    if (((d % p[v].first) == p[v].second.first) && (p[u].second.second == p[v].second.second) \
                        && (p[u].second.first == ((p[v].second.first+1) % p[v].first))) bad = 1;
                }
                if (!bad) {
                    if ((dist[v][(d+1) % p[v].first] == -1) || (d+1 < dist[v][(d+1) % p[v].first])) {
                        dist[v][(d+1) % p[v].first] = d+1;
                        Q[d+1].pb(v);
                    }
                }
                if ((p[u].first > 1) && (p[v].first > 1) && (p[u].second.second != p[v].second.second)) {
                    int k;
                    for (k = 0; k < p[v].first; k++) {
                        d += p[u].first;
                        if (d > 999990) break;
                        bad = 0;
                        if (p[v].first != 1) {
                            if (((d+1) % p[v].first) == p[v].second.first) bad = 1;
                            if (((d % p[v].first) == p[v].second.first) && (p[u].second.second == p[v].second.second) \
                                && (p[u].second.first == ((p[v].second.first+1) % p[v].first))) bad = 1;
                        }
                        if (!bad) {
                            if ((dist[v][(d+1) % p[v].first] == -1) || (d+1 < dist[v][(d+1) % p[v].first])) {
                                dist[v][(d+1) % p[v].first] = d+1;
                                Q[d+1].pb(v);
                            }
                        }
                    }
                    d -= (k+1)*p[u].first;
                }
                d++;
                if ((p[u].first != 1) && ((d % p[u].first) == p[u].second.first)) break;
            }
            if ((p[u].first == 1) && (p[v].first != 1)) done[v] = 1;
            d = t+p[u].first;
            int bad = 0;
            if (p[v].first != 1) {
                if (((d+1) % p[v].first) == p[v].second.first) bad = 1;
                if (((d % p[v].first) == p[v].second.first) && (p[u].second.second == p[v].second.second) \
                    && (p[u].second.first == ((p[v].second.first+1) % p[v].first))) bad = 1;
            }
            if (!bad) {
                if ((dist[v][(d+1) % p[v].first] == -1) || (d+1 < dist[v][(d+1) % p[v].first])) {
                    dist[v][(d+1) % p[v].first] = d+1;
                    Q[d+1].pb(v);
                }
            }
            d = t;
        }
        done2[u] = 1;
    }
    printf("impossible\n");

    return 0;
}

Compilation message

watchmen.cpp: In function 'int main()':
watchmen.cpp:50:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         while ((pos[i] < adjList[i].size()) && (p[adjList[i][pos[i]]].first <= 1)) pos[i]++;
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~
watchmen.cpp:68:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |         for (; i < adjList[u].size(); i++) {
      |                ~~^~~~~~~~~~~~~~~~~~~
watchmen.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
watchmen.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |     scanf("%d",&K);
      |     ~~~~~^~~~~~~~~
watchmen.cpp:41:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         scanf("%d",&l);
      |         ~~~~~^~~~~~~~~
watchmen.cpp:42:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         for (j = 0; j < l; j++) scanf("%d",&v),p[v-1] = mp(l,mp(j,i));
      |                                 ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 40 ms 36556 KB Output is correct
2 Correct 112 ms 47268 KB Output is correct
3 Correct 115 ms 45848 KB Output is correct
4 Correct 159 ms 45344 KB Output is correct
5 Correct 27 ms 35616 KB Output is correct
6 Correct 125 ms 45520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 36668 KB Output is correct
2 Correct 117 ms 47224 KB Output is correct
3 Correct 130 ms 45764 KB Output is correct
4 Correct 215 ms 45332 KB Output is correct
5 Correct 28 ms 35592 KB Output is correct
6 Correct 107 ms 45508 KB Output is correct
7 Correct 101 ms 45728 KB Output is correct
8 Correct 112 ms 46016 KB Output is correct
9 Correct 123 ms 46100 KB Output is correct
10 Correct 126 ms 44612 KB Output is correct
11 Correct 95 ms 44096 KB Output is correct
12 Correct 99 ms 45556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 36668 KB Output is correct
2 Correct 117 ms 47224 KB Output is correct
3 Correct 130 ms 45764 KB Output is correct
4 Correct 215 ms 45332 KB Output is correct
5 Correct 28 ms 35592 KB Output is correct
6 Correct 107 ms 45508 KB Output is correct
7 Correct 101 ms 45728 KB Output is correct
8 Correct 112 ms 46016 KB Output is correct
9 Correct 123 ms 46100 KB Output is correct
10 Correct 126 ms 44612 KB Output is correct
11 Correct 95 ms 44096 KB Output is correct
12 Correct 99 ms 45556 KB Output is correct
13 Correct 38 ms 36628 KB Output is correct
14 Correct 109 ms 47276 KB Output is correct
15 Correct 108 ms 45864 KB Output is correct
16 Correct 185 ms 45360 KB Output is correct
17 Correct 26 ms 35536 KB Output is correct
18 Correct 101 ms 45516 KB Output is correct
19 Correct 100 ms 45632 KB Output is correct
20 Correct 114 ms 46056 KB Output is correct
21 Correct 115 ms 46020 KB Output is correct
22 Correct 129 ms 44672 KB Output is correct
23 Correct 100 ms 44128 KB Output is correct
24 Correct 97 ms 45508 KB Output is correct
25 Correct 2512 ms 94304 KB Output is correct
26 Correct 2119 ms 99464 KB Output is correct
27 Correct 2084 ms 96112 KB Output is correct
28 Correct 1442 ms 100452 KB Output is correct
29 Correct 2348 ms 87964 KB Output is correct
30 Correct 2473 ms 88856 KB Output is correct
31 Correct 2654 ms 97696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 36668 KB Output is correct
2 Correct 117 ms 47224 KB Output is correct
3 Correct 130 ms 45764 KB Output is correct
4 Correct 215 ms 45332 KB Output is correct
5 Correct 28 ms 35592 KB Output is correct
6 Correct 107 ms 45508 KB Output is correct
7 Correct 101 ms 45728 KB Output is correct
8 Correct 112 ms 46016 KB Output is correct
9 Correct 123 ms 46100 KB Output is correct
10 Correct 126 ms 44612 KB Output is correct
11 Correct 95 ms 44096 KB Output is correct
12 Correct 99 ms 45556 KB Output is correct
13 Correct 38 ms 36628 KB Output is correct
14 Correct 109 ms 47276 KB Output is correct
15 Correct 108 ms 45864 KB Output is correct
16 Correct 185 ms 45360 KB Output is correct
17 Correct 26 ms 35536 KB Output is correct
18 Correct 101 ms 45516 KB Output is correct
19 Correct 100 ms 45632 KB Output is correct
20 Correct 114 ms 46056 KB Output is correct
21 Correct 115 ms 46020 KB Output is correct
22 Correct 129 ms 44672 KB Output is correct
23 Correct 100 ms 44128 KB Output is correct
24 Correct 97 ms 45508 KB Output is correct
25 Correct 2512 ms 94304 KB Output is correct
26 Correct 2119 ms 99464 KB Output is correct
27 Correct 2084 ms 96112 KB Output is correct
28 Correct 1442 ms 100452 KB Output is correct
29 Correct 2348 ms 87964 KB Output is correct
30 Correct 2473 ms 88856 KB Output is correct
31 Correct 2654 ms 97696 KB Output is correct
32 Correct 39 ms 36728 KB Output is correct
33 Correct 112 ms 47216 KB Output is correct
34 Correct 113 ms 45852 KB Output is correct
35 Correct 180 ms 45324 KB Output is correct
36 Correct 26 ms 35644 KB Output is correct
37 Correct 100 ms 45604 KB Output is correct
38 Correct 98 ms 45636 KB Output is correct
39 Correct 97 ms 46052 KB Output is correct
40 Correct 110 ms 46024 KB Output is correct
41 Correct 109 ms 44612 KB Output is correct
42 Correct 100 ms 44136 KB Output is correct
43 Correct 97 ms 45496 KB Output is correct
44 Correct 2393 ms 94112 KB Output is correct
45 Correct 2353 ms 99056 KB Output is correct
46 Correct 2094 ms 95976 KB Output is correct
47 Correct 1425 ms 100036 KB Output is correct
48 Correct 2164 ms 87624 KB Output is correct
49 Correct 2311 ms 89100 KB Output is correct
50 Correct 2360 ms 97820 KB Output is correct
51 Correct 2858 ms 114960 KB Output is correct
52 Correct 2973 ms 135452 KB Output is correct
53 Correct 2631 ms 117284 KB Output is correct
54 Correct 1462 ms 93504 KB Output is correct
55 Execution timed out 6056 ms 110660 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 40 ms 36556 KB Output is correct
2 Correct 112 ms 47268 KB Output is correct
3 Correct 115 ms 45848 KB Output is correct
4 Correct 159 ms 45344 KB Output is correct
5 Correct 27 ms 35616 KB Output is correct
6 Correct 125 ms 45520 KB Output is correct
7 Correct 36 ms 36668 KB Output is correct
8 Correct 117 ms 47224 KB Output is correct
9 Correct 130 ms 45764 KB Output is correct
10 Correct 215 ms 45332 KB Output is correct
11 Correct 28 ms 35592 KB Output is correct
12 Correct 107 ms 45508 KB Output is correct
13 Correct 101 ms 45728 KB Output is correct
14 Correct 112 ms 46016 KB Output is correct
15 Correct 123 ms 46100 KB Output is correct
16 Correct 126 ms 44612 KB Output is correct
17 Correct 95 ms 44096 KB Output is correct
18 Correct 99 ms 45556 KB Output is correct
19 Correct 26 ms 35520 KB Output is correct
20 Correct 22 ms 35420 KB Output is correct
21 Correct 27 ms 35528 KB Output is correct
22 Correct 40 ms 36828 KB Output is correct
23 Correct 126 ms 47328 KB Output is correct
24 Correct 102 ms 45812 KB Output is correct
25 Correct 157 ms 45396 KB Output is correct
26 Correct 21 ms 35584 KB Output is correct
27 Correct 116 ms 45540 KB Output is correct
28 Correct 118 ms 45748 KB Output is correct
29 Correct 102 ms 46020 KB Output is correct
30 Correct 114 ms 45976 KB Output is correct
31 Correct 124 ms 44628 KB Output is correct
32 Correct 104 ms 44100 KB Output is correct
33 Correct 119 ms 45500 KB Output is correct
34 Correct 2489 ms 92528 KB Output is correct
35 Correct 2655 ms 87480 KB Output is correct
36 Correct 2496 ms 89696 KB Output is correct
37 Incorrect 2239 ms 92552 KB Output isn't correct
38 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 40 ms 36556 KB Output is correct
2 Correct 112 ms 47268 KB Output is correct
3 Correct 115 ms 45848 KB Output is correct
4 Correct 159 ms 45344 KB Output is correct
5 Correct 27 ms 35616 KB Output is correct
6 Correct 125 ms 45520 KB Output is correct
7 Correct 36 ms 36668 KB Output is correct
8 Correct 117 ms 47224 KB Output is correct
9 Correct 130 ms 45764 KB Output is correct
10 Correct 215 ms 45332 KB Output is correct
11 Correct 28 ms 35592 KB Output is correct
12 Correct 107 ms 45508 KB Output is correct
13 Correct 101 ms 45728 KB Output is correct
14 Correct 112 ms 46016 KB Output is correct
15 Correct 123 ms 46100 KB Output is correct
16 Correct 126 ms 44612 KB Output is correct
17 Correct 95 ms 44096 KB Output is correct
18 Correct 99 ms 45556 KB Output is correct
19 Correct 38 ms 36628 KB Output is correct
20 Correct 109 ms 47276 KB Output is correct
21 Correct 108 ms 45864 KB Output is correct
22 Correct 185 ms 45360 KB Output is correct
23 Correct 26 ms 35536 KB Output is correct
24 Correct 101 ms 45516 KB Output is correct
25 Correct 100 ms 45632 KB Output is correct
26 Correct 114 ms 46056 KB Output is correct
27 Correct 115 ms 46020 KB Output is correct
28 Correct 129 ms 44672 KB Output is correct
29 Correct 100 ms 44128 KB Output is correct
30 Correct 97 ms 45508 KB Output is correct
31 Correct 2512 ms 94304 KB Output is correct
32 Correct 2119 ms 99464 KB Output is correct
33 Correct 2084 ms 96112 KB Output is correct
34 Correct 1442 ms 100452 KB Output is correct
35 Correct 2348 ms 87964 KB Output is correct
36 Correct 2473 ms 88856 KB Output is correct
37 Correct 2654 ms 97696 KB Output is correct
38 Correct 26 ms 35520 KB Output is correct
39 Correct 22 ms 35420 KB Output is correct
40 Correct 27 ms 35528 KB Output is correct
41 Correct 40 ms 36828 KB Output is correct
42 Correct 126 ms 47328 KB Output is correct
43 Correct 102 ms 45812 KB Output is correct
44 Correct 157 ms 45396 KB Output is correct
45 Correct 21 ms 35584 KB Output is correct
46 Correct 116 ms 45540 KB Output is correct
47 Correct 118 ms 45748 KB Output is correct
48 Correct 102 ms 46020 KB Output is correct
49 Correct 114 ms 45976 KB Output is correct
50 Correct 124 ms 44628 KB Output is correct
51 Correct 104 ms 44100 KB Output is correct
52 Correct 119 ms 45500 KB Output is correct
53 Correct 2489 ms 92528 KB Output is correct
54 Correct 2655 ms 87480 KB Output is correct
55 Correct 2496 ms 89696 KB Output is correct
56 Incorrect 2239 ms 92552 KB Output isn't correct
57 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 40 ms 36556 KB Output is correct
2 Correct 112 ms 47268 KB Output is correct
3 Correct 115 ms 45848 KB Output is correct
4 Correct 159 ms 45344 KB Output is correct
5 Correct 27 ms 35616 KB Output is correct
6 Correct 125 ms 45520 KB Output is correct
7 Correct 36 ms 36668 KB Output is correct
8 Correct 117 ms 47224 KB Output is correct
9 Correct 130 ms 45764 KB Output is correct
10 Correct 215 ms 45332 KB Output is correct
11 Correct 28 ms 35592 KB Output is correct
12 Correct 107 ms 45508 KB Output is correct
13 Correct 101 ms 45728 KB Output is correct
14 Correct 112 ms 46016 KB Output is correct
15 Correct 123 ms 46100 KB Output is correct
16 Correct 126 ms 44612 KB Output is correct
17 Correct 95 ms 44096 KB Output is correct
18 Correct 99 ms 45556 KB Output is correct
19 Correct 38 ms 36628 KB Output is correct
20 Correct 109 ms 47276 KB Output is correct
21 Correct 108 ms 45864 KB Output is correct
22 Correct 185 ms 45360 KB Output is correct
23 Correct 26 ms 35536 KB Output is correct
24 Correct 101 ms 45516 KB Output is correct
25 Correct 100 ms 45632 KB Output is correct
26 Correct 114 ms 46056 KB Output is correct
27 Correct 115 ms 46020 KB Output is correct
28 Correct 129 ms 44672 KB Output is correct
29 Correct 100 ms 44128 KB Output is correct
30 Correct 97 ms 45508 KB Output is correct
31 Correct 2512 ms 94304 KB Output is correct
32 Correct 2119 ms 99464 KB Output is correct
33 Correct 2084 ms 96112 KB Output is correct
34 Correct 1442 ms 100452 KB Output is correct
35 Correct 2348 ms 87964 KB Output is correct
36 Correct 2473 ms 88856 KB Output is correct
37 Correct 2654 ms 97696 KB Output is correct
38 Correct 39 ms 36728 KB Output is correct
39 Correct 112 ms 47216 KB Output is correct
40 Correct 113 ms 45852 KB Output is correct
41 Correct 180 ms 45324 KB Output is correct
42 Correct 26 ms 35644 KB Output is correct
43 Correct 100 ms 45604 KB Output is correct
44 Correct 98 ms 45636 KB Output is correct
45 Correct 97 ms 46052 KB Output is correct
46 Correct 110 ms 46024 KB Output is correct
47 Correct 109 ms 44612 KB Output is correct
48 Correct 100 ms 44136 KB Output is correct
49 Correct 97 ms 45496 KB Output is correct
50 Correct 2393 ms 94112 KB Output is correct
51 Correct 2353 ms 99056 KB Output is correct
52 Correct 2094 ms 95976 KB Output is correct
53 Correct 1425 ms 100036 KB Output is correct
54 Correct 2164 ms 87624 KB Output is correct
55 Correct 2311 ms 89100 KB Output is correct
56 Correct 2360 ms 97820 KB Output is correct
57 Correct 2858 ms 114960 KB Output is correct
58 Correct 2973 ms 135452 KB Output is correct
59 Correct 2631 ms 117284 KB Output is correct
60 Correct 1462 ms 93504 KB Output is correct
61 Execution timed out 6056 ms 110660 KB Time limit exceeded
62 Halted 0 ms 0 KB -