Submission #335840

# Submission time Handle Problem Language Result Execution time Memory
335840 2020-12-14T05:42:25 Z aanastasov Stations (IOI20_stations) C++17
100 / 100
988 ms 1208 KB
#include "stations.h"
#include <algorithm>
#include <cassert>
//#include <iostream>
#include <vector>

void dfs(int src, int parent, int depth, std::vector<std::vector<int>>& adj,
        int& nextLabel, std::vector<int>& labels) {
    if (depth % 2 == 0) {
        labels[src] = nextLabel++;
    }
    for (int dst : adj[src]) {
        if (dst == parent) continue;
        dfs(dst, src, depth + 1, adj, nextLabel, labels);
    }
    if (depth % 2 == 1) {
        labels[src] = nextLabel++;
    }
}

std::vector<int> label(int n, int k, std::vector<int> u, std::vector<int> v) {
    assert(n <= k);
    auto adj = std::vector<std::vector<int>>(n, std::vector<int>());
    assert(u.size() == v.size());
    for (int index = 0; index < u.size(); ++index) {
        adj[u[index]].push_back(v[index]);
        adj[v[index]].push_back(u[index]);
    }
    auto labels = std::vector<int>(n, -1);
    int nextLabel = 0;
    dfs(0, -1, 0, adj, nextLabel, labels);
//    for (auto &x : labels) std::cout << x << ", "; std::cout << std::endl;
    return labels;
}

int find_next_station(int s, int t, std::vector<int> c) {
    if (s == t) return t;
    for (int neighbour : c) if (neighbour == t) return t;
    std::sort(c.begin(), c.end());
    auto rc = std::vector<int>(c.begin(), c.end());
    std::reverse(rc.begin(), rc.end());
    if (s == 0) { // we are at the root => there's no parent node
        for (int neighbour : c) if (t <= neighbour) return neighbour;
    } else {
        if (s < c[0]) { // even depth
            if (t > s && t < c.back()) { // go to a child
                for (int neighbour : c) if (t <= neighbour) return neighbour;
            } else { // go to parent
                return c.back();
            }
        } else { // odd depth
            assert(c.back() < s);
            if (t > c[0] && t < s) { // go to a child
                for (int neighbour : rc) if (t >= neighbour) return neighbour;
            } else { // go to parent
                return c[0];
            }
        }
    }
    assert(false);
}

Compilation message

stations.cpp: In function 'std::vector<int> label(int, int, std::vector<int>, std::vector<int>)':
stations.cpp:25:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |     for (int index = 0; index < u.size(); ++index) {
      |                         ~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 548 ms 972 KB Output is correct
2 Correct 444 ms 972 KB Output is correct
3 Correct 872 ms 736 KB Output is correct
4 Correct 597 ms 736 KB Output is correct
5 Correct 567 ms 888 KB Output is correct
6 Correct 436 ms 980 KB Output is correct
7 Correct 422 ms 864 KB Output is correct
8 Correct 3 ms 888 KB Output is correct
9 Correct 4 ms 796 KB Output is correct
10 Correct 1 ms 756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 433 ms 804 KB Output is correct
2 Correct 528 ms 808 KB Output is correct
3 Correct 850 ms 736 KB Output is correct
4 Correct 657 ms 880 KB Output is correct
5 Correct 590 ms 736 KB Output is correct
6 Correct 502 ms 808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 529 ms 884 KB Output is correct
2 Correct 523 ms 976 KB Output is correct
3 Correct 849 ms 1008 KB Output is correct
4 Correct 689 ms 880 KB Output is correct
5 Correct 567 ms 804 KB Output is correct
6 Correct 421 ms 1012 KB Output is correct
7 Correct 434 ms 1004 KB Output is correct
8 Correct 3 ms 888 KB Output is correct
9 Correct 3 ms 880 KB Output is correct
10 Correct 2 ms 736 KB Output is correct
11 Correct 595 ms 736 KB Output is correct
12 Correct 445 ms 992 KB Output is correct
13 Correct 424 ms 1072 KB Output is correct
14 Correct 448 ms 748 KB Output is correct
15 Correct 53 ms 872 KB Output is correct
16 Correct 74 ms 736 KB Output is correct
17 Correct 113 ms 808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 862 ms 736 KB Output is correct
2 Correct 703 ms 756 KB Output is correct
3 Correct 725 ms 800 KB Output is correct
4 Correct 3 ms 736 KB Output is correct
5 Correct 5 ms 756 KB Output is correct
6 Correct 1 ms 736 KB Output is correct
7 Correct 751 ms 756 KB Output is correct
8 Correct 988 ms 884 KB Output is correct
9 Correct 669 ms 804 KB Output is correct
10 Correct 609 ms 804 KB Output is correct
11 Correct 7 ms 756 KB Output is correct
12 Correct 7 ms 880 KB Output is correct
13 Correct 6 ms 756 KB Output is correct
14 Correct 3 ms 756 KB Output is correct
15 Correct 2 ms 736 KB Output is correct
16 Correct 674 ms 748 KB Output is correct
17 Correct 618 ms 880 KB Output is correct
18 Correct 652 ms 888 KB Output is correct
19 Correct 494 ms 880 KB Output is correct
20 Correct 518 ms 880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 553 ms 1012 KB Output is correct
2 Correct 426 ms 984 KB Output is correct
3 Correct 830 ms 880 KB Output is correct
4 Correct 843 ms 736 KB Output is correct
5 Correct 608 ms 1008 KB Output is correct
6 Correct 455 ms 1140 KB Output is correct
7 Correct 459 ms 760 KB Output is correct
8 Correct 3 ms 736 KB Output is correct
9 Correct 4 ms 756 KB Output is correct
10 Correct 0 ms 880 KB Output is correct
11 Correct 515 ms 736 KB Output is correct
12 Correct 587 ms 800 KB Output is correct
13 Correct 864 ms 1008 KB Output is correct
14 Correct 733 ms 896 KB Output is correct
15 Correct 739 ms 756 KB Output is correct
16 Correct 497 ms 736 KB Output is correct
17 Correct 587 ms 756 KB Output is correct
18 Correct 599 ms 1008 KB Output is correct
19 Correct 489 ms 1208 KB Output is correct
20 Correct 438 ms 736 KB Output is correct
21 Correct 57 ms 756 KB Output is correct
22 Correct 91 ms 736 KB Output is correct
23 Correct 130 ms 928 KB Output is correct
24 Correct 9 ms 880 KB Output is correct
25 Correct 5 ms 888 KB Output is correct
26 Correct 3 ms 756 KB Output is correct
27 Correct 4 ms 880 KB Output is correct
28 Correct 1 ms 864 KB Output is correct
29 Correct 539 ms 756 KB Output is correct
30 Correct 500 ms 736 KB Output is correct
31 Correct 492 ms 1008 KB Output is correct
32 Correct 482 ms 880 KB Output is correct
33 Correct 503 ms 756 KB Output is correct
34 Correct 303 ms 992 KB Output is correct
35 Correct 429 ms 952 KB Output is correct
36 Correct 444 ms 884 KB Output is correct
37 Correct 455 ms 864 KB Output is correct
38 Correct 458 ms 864 KB Output is correct
39 Correct 463 ms 1000 KB Output is correct
40 Correct 453 ms 864 KB Output is correct
41 Correct 453 ms 992 KB Output is correct
42 Correct 60 ms 820 KB Output is correct
43 Correct 105 ms 756 KB Output is correct
44 Correct 132 ms 884 KB Output is correct
45 Correct 156 ms 756 KB Output is correct
46 Correct 299 ms 800 KB Output is correct
47 Correct 312 ms 764 KB Output is correct
48 Correct 63 ms 904 KB Output is correct
49 Correct 67 ms 736 KB Output is correct