Submission #421459

# Submission time Handle Problem Language Result Execution time Memory
421459 2021-06-09T07:46:34 Z usachevd0 Stations (IOI20_stations) C++17
100 / 100
1178 ms 828 KB
#include <bits/stdc++.h>
#ifndef LOCAL
    #include "stations.h"
#endif

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define Time (clock() * 1.0 / CLOCKS_PER_SEC)
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1& x, T2 y) {
    return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1& x, T2 y) {
    return y > x ? (x = y, true) : false;
}
void debug_out() {
    cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
    cerr << ' ' << A;
    debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
    for (int i = 0; i < n; ++i)
        cerr << a[i] << ' ';
    cerr << endl;
}
#ifdef LOCAL
    #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
    #define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
    #define debug(...) 1337
    #define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T>& v) {
    for (auto& e : v)
        stream << e << ' ';
    return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
    return stream << p.first << ' ' << p.second;
}

const int maxN = 1e3 + 3;
vector<int> G[maxN];
vector<int> ord;

void dfs(int v, int rev, int p = -1) {
    if (rev == 0) {
        ord.push_back(v);
        for (int u : G[v])
            if (u != p)
                dfs(u, 1, v);
    } else {
        for (int u : G[v])
            if (u != p)
                dfs(u, 0, v);
        ord.push_back(v);
    }
}


vector<int> label(int n, int __, vector<int> eu, vector<int> ev) {
    for (int v = 0; v < n; ++v) G[v].clear();
    for (int i = 0; i < n - 1; ++i) {
        int u = eu[i], v = ev[i];
        G[u].push_back(v);
        G[v].push_back(u);
    }
    ord.clear();
    dfs(0, 0);
    vector<int> pos(n);
    for (int i = 0; i < n; ++i)
        pos[ord[i]] = i;
    return pos;
}

int find_next_station(int s, int t, vector<int> g) {
    // debug(s, t, g);
    int rev;
    sort(all(g));
    if (s == 0 || s < g[0]) {
        int par = -1;
        if (s != 0) {
            par = g.back();
            g.pop_back();
        }
        if (!g.empty() && s < t && t <= g.back()) {
            return *lower_bound(all(g), t);
        } else return par;
    } else {
        int par = g[0];
        g.erase(g.begin());
        if (!g.empty() && g[0] <= t && t < s) {
            return *(upper_bound(all(g), t) - 1);
        } else return par;
    }
}

#ifdef LOCAL

static int max_label = 0;
static int r, n, k, q;
static std::vector<int> u, v, labels, answers;
static std::map<int, int> reverse_labels;
static std::vector<std::vector<int>> adjlist;
static int s, t, w;
static std::vector<int> c;

int main() {
    freopen("in", "r", stdin);
    assert(scanf("%d", &r) == 1);
    for (int tc = 0; tc < r; tc++) {
        assert(scanf("%d%d", &n, &k) == 2);
        u.resize(n - 1);
        v.resize(n - 1);
        adjlist.clear();
        adjlist.resize(n);
        for (int i = 0; i < n - 1; i++) {
            assert(scanf("%d%d", &u[i], &v[i]) == 2);
            adjlist[u[i]].push_back(v[i]);
            adjlist[v[i]].push_back(u[i]);
        }
        labels = label(n, k, u, v);
        debug(labels);
        if ((int)labels.size() != n) {
            printf("Number of labels not equal to %d\n", n);
            exit(0);
        }
        reverse_labels.clear();
        for (int i = 0; i < n; i++) {
            if (labels[i] < 0 || labels[i] > k) {
                printf("Label not in range 0 to %d\n", k);
                exit(0);
            }
            if (reverse_labels.find(labels[i]) != reverse_labels.end()) {
                printf("Labels not unique\n");
                exit(0);
            }
            reverse_labels[labels[i]] = i;
            if (labels[i] > max_label) {
                max_label = labels[i];
            }
        }
        assert(scanf("%d", &q) == 1);
        for (int i = 0; i < q; i++) {
            assert(scanf("%d%d%d", &s, &t, &w) == 3);
            c.clear();
            for (int v : adjlist[s]) {
                c.push_back(labels[v]);
            }
            std::sort(c.begin(), c.end());
            int answer = find_next_station(labels[s], labels[t], c);
            if (!std::binary_search(c.begin(), c.end(), answer)) {
                printf("Label %d returned by find_next_station not found in c\n", answer);
                exit(0);
            }
            answers.push_back(reverse_labels[answer]);
        }
    }
    printf("%d\n", max_label);
    for (int index : answers) {
        printf("%d\n", index);
    }
    exit(0);
}


// int32_t main() {
//     #ifdef LOCAL
//         freopen("in", "r", stdin);
//     #endif
//     ios::sync_with_stdio(0);
//     cin.tie(0);
    
    
    
    
//     return 0;
// }
#endif

Compilation message

stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:91:9: warning: unused variable 'rev' [-Wunused-variable]
   91 |     int rev;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 535 ms 508 KB Output is correct
2 Correct 593 ms 596 KB Output is correct
3 Correct 1000 ms 400 KB Output is correct
4 Correct 722 ms 512 KB Output is correct
5 Correct 742 ms 400 KB Output is correct
6 Correct 493 ms 604 KB Output is correct
7 Correct 641 ms 528 KB Output is correct
8 Correct 2 ms 540 KB Output is correct
9 Correct 5 ms 468 KB Output is correct
10 Correct 2 ms 400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 531 ms 512 KB Output is correct
2 Correct 648 ms 528 KB Output is correct
3 Correct 1178 ms 400 KB Output is correct
4 Correct 963 ms 492 KB Output is correct
5 Correct 827 ms 492 KB Output is correct
6 Correct 687 ms 500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 751 ms 508 KB Output is correct
2 Correct 535 ms 492 KB Output is correct
3 Correct 909 ms 400 KB Output is correct
4 Correct 910 ms 400 KB Output is correct
5 Correct 764 ms 512 KB Output is correct
6 Correct 492 ms 584 KB Output is correct
7 Correct 530 ms 512 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 4 ms 468 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 693 ms 412 KB Output is correct
12 Correct 532 ms 608 KB Output is correct
13 Correct 526 ms 708 KB Output is correct
14 Correct 540 ms 528 KB Output is correct
15 Correct 59 ms 400 KB Output is correct
16 Correct 93 ms 648 KB Output is correct
17 Correct 142 ms 508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 971 ms 400 KB Output is correct
2 Correct 696 ms 404 KB Output is correct
3 Correct 665 ms 520 KB Output is correct
4 Correct 2 ms 476 KB Output is correct
5 Correct 3 ms 544 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 796 ms 400 KB Output is correct
8 Correct 1065 ms 428 KB Output is correct
9 Correct 691 ms 516 KB Output is correct
10 Correct 628 ms 528 KB Output is correct
11 Correct 6 ms 472 KB Output is correct
12 Correct 5 ms 464 KB Output is correct
13 Correct 6 ms 464 KB Output is correct
14 Correct 5 ms 468 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 649 ms 512 KB Output is correct
17 Correct 562 ms 512 KB Output is correct
18 Correct 618 ms 512 KB Output is correct
19 Correct 629 ms 400 KB Output is correct
20 Correct 745 ms 516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 740 ms 528 KB Output is correct
2 Correct 552 ms 512 KB Output is correct
3 Correct 1103 ms 492 KB Output is correct
4 Correct 900 ms 472 KB Output is correct
5 Correct 714 ms 512 KB Output is correct
6 Correct 483 ms 512 KB Output is correct
7 Correct 610 ms 520 KB Output is correct
8 Correct 4 ms 468 KB Output is correct
9 Correct 5 ms 468 KB Output is correct
10 Correct 1 ms 476 KB Output is correct
11 Correct 523 ms 512 KB Output is correct
12 Correct 669 ms 504 KB Output is correct
13 Correct 1081 ms 400 KB Output is correct
14 Correct 721 ms 504 KB Output is correct
15 Correct 715 ms 400 KB Output is correct
16 Correct 511 ms 520 KB Output is correct
17 Correct 666 ms 400 KB Output is correct
18 Correct 528 ms 624 KB Output is correct
19 Correct 601 ms 664 KB Output is correct
20 Correct 610 ms 508 KB Output is correct
21 Correct 57 ms 444 KB Output is correct
22 Correct 91 ms 544 KB Output is correct
23 Correct 131 ms 528 KB Output is correct
24 Correct 4 ms 468 KB Output is correct
25 Correct 7 ms 448 KB Output is correct
26 Correct 5 ms 468 KB Output is correct
27 Correct 4 ms 468 KB Output is correct
28 Correct 2 ms 400 KB Output is correct
29 Correct 514 ms 508 KB Output is correct
30 Correct 590 ms 400 KB Output is correct
31 Correct 588 ms 400 KB Output is correct
32 Correct 474 ms 528 KB Output is correct
33 Correct 754 ms 508 KB Output is correct
34 Correct 452 ms 596 KB Output is correct
35 Correct 538 ms 696 KB Output is correct
36 Correct 529 ms 588 KB Output is correct
37 Correct 477 ms 692 KB Output is correct
38 Correct 481 ms 580 KB Output is correct
39 Correct 531 ms 612 KB Output is correct
40 Correct 430 ms 632 KB Output is correct
41 Correct 459 ms 680 KB Output is correct
42 Correct 79 ms 532 KB Output is correct
43 Correct 116 ms 656 KB Output is correct
44 Correct 129 ms 576 KB Output is correct
45 Correct 184 ms 528 KB Output is correct
46 Correct 337 ms 600 KB Output is correct
47 Correct 341 ms 636 KB Output is correct
48 Correct 52 ms 768 KB Output is correct
49 Correct 57 ms 828 KB Output is correct