Submission #968509

# Submission time Handle Problem Language Result Execution time Memory
968509 2024-04-23T14:01:31 Z GrindMachine Highway Tolls (IOI18_highway) C++17
100 / 100
195 ms 13900 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
 
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)
 
template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}
 
template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}
 
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
 
/*
 
refs:
http://www.algonotes.com/en/solutions-ioi2018/
edi
 
*/
 
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
 
#include "highway.h"
 
vector<pii> adj[N];
 
void find_pair(int n, std::vector<int> U, std::vector<int> V, int A, int B) {
    int m = sz(U);
    vector<pii> edges;
    rep(i,m){
        int u = U[i], v = V[i];
        edges.pb({u,v});
        adj[u].pb({v,i}), adj[v].pb({u,i});
    }
 
    // find an edge that lies on at least one sp from s to t
    ll all_light = -1;
 
    {
        vector<int> w(m);
        all_light = ask(w);
    }
 
    int lo = 0, hi = m-1;
    int first = -1;
 
    while(lo <= hi){
        int mid = (lo+hi) >> 1;
        vector<int> w(m);
        rep(i,mid+1){
            w[i] = 1;
        }
        auto res = ask(w);
 
        if(res != all_light){
            first = mid;
            hi = mid-1;
        }
        else{
            lo = mid+1;
        }
    }
        
    assert(first != -1);
 
    // split into 2 sets: closer to x and closer to y
    auto bfs = [&](int r){
        queue<int> q;
        vector<int> dis(n,-1);
        q.push(r);
        dis[r] = 0;

        while(!q.empty()){
            int u = q.front();
            q.pop();
 
            for(auto [v,id] : adj[u]){
                if(dis[v] != -1) conts;
                q.push(v);
                dis[v] = dis[u]+1;
            }
        }

        return dis;
    };
 
    auto [x,y] = edges[first];
    auto xy_edge = first;
    vector<int> node_type(n); // 0 = none, 1 = closer to x, 2 = closer to y
    auto disx = bfs(x);
    auto disy = bfs(y);
    rep(i,n){
        if(disx[i] < disy[i]) node_type[i] = 1;
        else if(disy[i] < disx[i]) node_type[i] = 2;
    }
 
    // bfs tree of x (ignore nodes with node_type[i] != 1)
    queue<int> q;
    vector<int> dis(n,-1);
    q.push(x);
    dis[x] = 0;
    vector<int> ordx;
 
    while(!q.empty()){
        int u = q.front();
        q.pop();
 
        for(auto [v,id] : adj[u]){
            if(dis[v] != -1) conts;
            if(node_type[v] != 1) conts;
            q.push(v);
            dis[v] = dis[u]+1;
            ordx.pb(id);
        }
    }
    
    // bfs tree of y (ignore nodes with node_type[i] != 2)
    fill(all(dis),-1);
    q.push(y);
    dis[y] = 0;
    vector<int> ordy;
 
    while(!q.empty()){
        int u = q.front();
        q.pop();
 
        for(auto [v,id] : adj[u]){
            if(dis[v] != -1) conts;
            if(node_type[v] != 2) conts;
            q.push(v);
            dis[v] = dis[u]+1;
            ordy.pb(id);
        }
    }

    // find s
    lo = 0, hi = sz(ordx);
    first = -1;
 
    while(lo <= hi){
        int mid = (lo+hi) >> 1;
        vector<int> w(m,1);
        
        w[xy_edge] = 0;
        trav(id,ordy){
            w[id] = 0;
        }
        rep(i,mid){
            w[ordx[i]] = 0;
        }
 
        auto res = ask(w);
 
        if(res == all_light){
            first = mid;
            hi = mid-1;
        }
        else{
            lo = mid+1;
        }
    }
 
    assert(first != -1);
    first--;
 
    int s = -1;
 
    if(first == -1){
        s = x;
    }
    else{
        auto [u,v] = edges[ordx[first]];
        if(disx[u] > disx[v]) s = u;
        else s = v;
    }
    
    // find t
    lo = 0, hi = sz(ordy);
    first = -1;
 
    while(lo <= hi){
        int mid = (lo+hi) >> 1;
        vector<int> w(m,1);
        
        w[xy_edge] = 0;
        trav(id,ordx){
            w[id] = 0;
        }
        rep(i,mid){
            w[ordy[i]] = 0;
        }
 
        auto res = ask(w);
 
        if(res == all_light){
            first = mid;
            hi = mid-1;
        }
        else{
            lo = mid+1;
        }
    }
 
    assert(first != -1);
    first--;
 
    int t = -1;
    
    if(first == -1){
        t = y;
    }
    else{
        auto [u,v] = edges[ordy[first]];
        if(disy[u] > disy[v]) t = u;
        else t = v;
    }

    answer(s,t);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2800 KB Output is correct
2 Correct 1 ms 2796 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 2792 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2800 KB Output is correct
7 Correct 1 ms 2648 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
9 Correct 1 ms 2796 KB Output is correct
10 Correct 1 ms 2648 KB Output is correct
11 Correct 1 ms 2648 KB Output is correct
12 Correct 1 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2852 KB Output is correct
2 Correct 11 ms 3808 KB Output is correct
3 Correct 89 ms 10808 KB Output is correct
4 Correct 124 ms 10848 KB Output is correct
5 Correct 97 ms 10984 KB Output is correct
6 Correct 101 ms 10804 KB Output is correct
7 Correct 91 ms 10828 KB Output is correct
8 Correct 85 ms 10992 KB Output is correct
9 Correct 76 ms 10564 KB Output is correct
10 Correct 117 ms 10800 KB Output is correct
11 Correct 103 ms 10580 KB Output is correct
12 Correct 111 ms 10616 KB Output is correct
13 Correct 110 ms 10228 KB Output is correct
14 Correct 133 ms 9964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 3616 KB Output is correct
2 Correct 15 ms 4692 KB Output is correct
3 Correct 23 ms 5408 KB Output is correct
4 Correct 86 ms 10088 KB Output is correct
5 Correct 69 ms 10096 KB Output is correct
6 Correct 70 ms 10372 KB Output is correct
7 Correct 66 ms 10228 KB Output is correct
8 Correct 84 ms 10092 KB Output is correct
9 Correct 115 ms 10488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3052 KB Output is correct
2 Correct 17 ms 3684 KB Output is correct
3 Correct 101 ms 8968 KB Output is correct
4 Correct 104 ms 10652 KB Output is correct
5 Correct 102 ms 10644 KB Output is correct
6 Correct 102 ms 10824 KB Output is correct
7 Correct 82 ms 10568 KB Output is correct
8 Correct 82 ms 10560 KB Output is correct
9 Correct 130 ms 11000 KB Output is correct
10 Correct 97 ms 10820 KB Output is correct
11 Correct 88 ms 10912 KB Output is correct
12 Correct 85 ms 10220 KB Output is correct
13 Correct 144 ms 10496 KB Output is correct
14 Correct 104 ms 9980 KB Output is correct
15 Correct 72 ms 10568 KB Output is correct
16 Correct 103 ms 10572 KB Output is correct
17 Correct 116 ms 10424 KB Output is correct
18 Correct 104 ms 9968 KB Output is correct
19 Correct 86 ms 10572 KB Output is correct
20 Correct 119 ms 10468 KB Output is correct
21 Correct 99 ms 11356 KB Output is correct
22 Correct 87 ms 11320 KB Output is correct
23 Correct 82 ms 11092 KB Output is correct
24 Correct 98 ms 11040 KB Output is correct
25 Correct 127 ms 10056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 3764 KB Output is correct
2 Correct 18 ms 3740 KB Output is correct
3 Correct 121 ms 11188 KB Output is correct
4 Correct 128 ms 11728 KB Output is correct
5 Correct 188 ms 13900 KB Output is correct
6 Correct 145 ms 13104 KB Output is correct
7 Correct 195 ms 13144 KB Output is correct
8 Correct 134 ms 13132 KB Output is correct
9 Correct 91 ms 10260 KB Output is correct
10 Correct 147 ms 10512 KB Output is correct
11 Correct 174 ms 11284 KB Output is correct
12 Correct 138 ms 12444 KB Output is correct
13 Correct 133 ms 13040 KB Output is correct
14 Correct 149 ms 13148 KB Output is correct
15 Correct 154 ms 12880 KB Output is correct
16 Correct 153 ms 11160 KB Output is correct
17 Correct 84 ms 11108 KB Output is correct
18 Correct 81 ms 11568 KB Output is correct
19 Correct 99 ms 11480 KB Output is correct
20 Correct 81 ms 11364 KB Output is correct
21 Correct 153 ms 13484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 3764 KB Output is correct
2 Correct 13 ms 3768 KB Output is correct
3 Correct 114 ms 11252 KB Output is correct
4 Correct 119 ms 11764 KB Output is correct
5 Correct 144 ms 11816 KB Output is correct
6 Correct 177 ms 13012 KB Output is correct
7 Correct 95 ms 11364 KB Output is correct
8 Correct 116 ms 11176 KB Output is correct
9 Correct 112 ms 12352 KB Output is correct
10 Correct 118 ms 13584 KB Output is correct
11 Correct 169 ms 13356 KB Output is correct
12 Correct 156 ms 13224 KB Output is correct
13 Correct 105 ms 11316 KB Output is correct
14 Correct 150 ms 10520 KB Output is correct
15 Correct 103 ms 11380 KB Output is correct
16 Correct 100 ms 10568 KB Output is correct
17 Correct 161 ms 11288 KB Output is correct
18 Correct 141 ms 10508 KB Output is correct
19 Correct 170 ms 12208 KB Output is correct
20 Correct 128 ms 12816 KB Output is correct
21 Correct 136 ms 13672 KB Output is correct
22 Correct 162 ms 13380 KB Output is correct
23 Correct 148 ms 13144 KB Output is correct
24 Correct 135 ms 12940 KB Output is correct
25 Correct 146 ms 13740 KB Output is correct
26 Correct 146 ms 13076 KB Output is correct
27 Correct 101 ms 11528 KB Output is correct
28 Correct 74 ms 11188 KB Output is correct
29 Correct 117 ms 11732 KB Output is correct
30 Correct 69 ms 11316 KB Output is correct
31 Correct 130 ms 11452 KB Output is correct
32 Correct 120 ms 11376 KB Output is correct
33 Correct 83 ms 11460 KB Output is correct
34 Correct 92 ms 11340 KB Output is correct
35 Correct 89 ms 11224 KB Output is correct
36 Correct 77 ms 11148 KB Output is correct
37 Correct 81 ms 11360 KB Output is correct
38 Correct 79 ms 11360 KB Output is correct
39 Correct 148 ms 13504 KB Output is correct
40 Correct 131 ms 13208 KB Output is correct
41 Correct 190 ms 13468 KB Output is correct
42 Correct 135 ms 13084 KB Output is correct