Submission #805957

# Submission time Handle Problem Language Result Execution time Memory
805957 2023-08-04T03:22:58 Z fatemetmhr Highway Tolls (IOI18_highway) C++17
100 / 100
216 ms 33748 KB
//  ~ Be Name Khoda ~  //
 
#include "highway.h"
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")
 
using namespace std;
 
typedef long long ll;
 
#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second
 
const int maxn  =  1e6   + 10;
const int maxn5 =  2e5   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   + 7;
const ll  inf   =  1e18;
 
vector <int> tok, adj[maxn5], v, u, ver[2], ed[2], jda[maxn5];
ll len, h[maxn5];
int m, q[maxn5], par[maxn5], kh[maxn5], gooded;
int kparid[maxn5], parid[maxn5], kpar[maxn5];
ll pas[2][maxn5];
bool mark[maxn5], good[maxn5];
 
void bfs(int rt){
    par[rt] = -1;
    memset(h, -1, sizeof h);
    int l = 0, r = 1;
    q[0] = rt;
    h[rt] = 0;
    while(l < r){
        int a = q[l++];
        for(auto id : adj[a]){
            int b = u[id] == a ? v[id] : u[id];
            if(h[b] != -1)
                continue;
            //cout << "here " << a << ' ' << b << ' ' << id << endl;
            q[r++] = b;
            par[b] = a;
            h[b] = h[a] + 1;
            parid[b] = id;
        }
    }
}
 
void dfs(int v, int ty){
    mark[v] = true;
    //cout << v << ' ' << par[v] << endl;
    for(auto id : jda[v]){
        int u = (::u[id]) == v ? (::v[id]) : (::u[id]);
        if(mark[u])
            continue;
        //cout << "in " << v << ' ' << u << ' ' << ty << endl;
        dfs(u, ty);
        ver[ty].pb(u);
        ed[ty].pb(id);
    }
}
 
ll get(int id, int ty){
    if(pas[ty][id] == -1){
        fill(all(tok), 1);
        tok[gooded] = false;
        for(auto u : ed[ty ^ 1])
            tok[u] = false;
        for(int i = id + 1; i < int(ed[ty].size()); i++)
            tok[ed[ty][i]] = false;
        pas[ty][id] = ask(tok);
    }
    return pas[ty][id];
}
 
 
void find_pair(int n, std::vector<int> U, std::vector<int> V, int a, int b){
    memset(pas, -1, sizeof pas);
    u = U;
    v = V;
    m = u.size();
    for(int i = 0; i < m; i++){
        adj[u[i]].pb(i);
        adj[v[i]].pb(i);
    }
    int lo = -1, hi = m;
    tok.resize(m);
    fill(all(tok), 0);
    len = ask(tok) / a;
    while(hi - lo > 1){
        int mid = (lo + hi) >> 1;
        fill(all(tok), 0);
        fill(tok.begin(), tok.begin() + mid + 1, 1);
        ll cost = ask(tok);
        if(cost == len * a)
            lo = mid;
        else
            hi = mid;
    }
    gooded = hi;
    //cout << "ok " << hi << ' ' << u[hi] << ' ' << v[hi] << endl;
    bfs(u[hi]);
    for(int i = 0; i < n; i++){
        kh[i] = h[i];
        kpar[i] = par[i];
        kparid[i] = parid[i];
    }
    bfs(v[hi]);
    for(int i = 0; i < n; i++){
        //cout << i << ' ' << kh[i] << ' ' << h[i] << ' ' << par[i] << ' ' << kpar[i] << endl;
        if(h[i] < kh[i] && par[i] != -1)
            jda[par[i]].pb(parid[i]);
        if(h[i] > kh[i] && kpar[i] != -1){
            //cout << "Pushing " << i << ' ' << kpar[i] << endl;
            jda[kpar[i]].pb(kparid[i]);
        }
    }
    int rt[2] = {u[hi], v[hi]};
    memset(mark, false, sizeof mark);
    //cout << rt[0] << ' ' << rt[1] << endl;
    dfs(rt[0], 0);
    dfs(rt[1], 1);
    /*
    for(int i = 0; i < n - 1; i++)
        //cout << ed[i] << ' ' << ver[i] << endl;
    //*/
    ////cout << "its " << hi << endl;
    lo = -1; hi = ed[0].size();
    while(hi - lo > 1){
        int mid = (lo + hi) >> 1;
        if(get(mid, 0) == len * a)
            lo = mid;
        else
            hi = mid;
    }
    int s = (hi == int(ed[0].size()) ? rt[0] : ver[0][hi]);
    //cout << lo << ' ' << hi << ' ' << ed[0].size() << ' ' << s << endl;
    lo = -1; hi = ed[1].size();
    while(hi - lo > 1){
        int mid = (lo + hi) >> 1;
        if(get(mid, 1) == len * a)
            lo = mid;
        else
            hi = mid;
    }
    int t = (hi == int(ed[1].size()) ? rt[1] : ver[1][hi]);
    //cout << t << ' ' << lo << ' ' << hi << ' ' << ed[1].size() << endl;
    answer(s, t); 
}
 
 
 
 
 
 
 
 
 
 
 
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14520 KB Output is correct
2 Correct 6 ms 14520 KB Output is correct
3 Correct 6 ms 14544 KB Output is correct
4 Correct 6 ms 14544 KB Output is correct
5 Correct 6 ms 14544 KB Output is correct
6 Correct 6 ms 14544 KB Output is correct
7 Correct 6 ms 14544 KB Output is correct
8 Correct 6 ms 14544 KB Output is correct
9 Correct 7 ms 14568 KB Output is correct
10 Correct 6 ms 14520 KB Output is correct
11 Correct 6 ms 14544 KB Output is correct
12 Correct 7 ms 14544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14672 KB Output is correct
2 Correct 16 ms 15696 KB Output is correct
3 Correct 122 ms 24424 KB Output is correct
4 Correct 104 ms 24456 KB Output is correct
5 Correct 112 ms 24432 KB Output is correct
6 Correct 116 ms 24424 KB Output is correct
7 Correct 131 ms 24436 KB Output is correct
8 Correct 143 ms 24456 KB Output is correct
9 Correct 125 ms 24424 KB Output is correct
10 Correct 116 ms 24468 KB Output is correct
11 Correct 137 ms 27448 KB Output is correct
12 Correct 148 ms 28600 KB Output is correct
13 Correct 107 ms 27864 KB Output is correct
14 Correct 101 ms 27984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 16720 KB Output is correct
2 Correct 24 ms 18628 KB Output is correct
3 Correct 42 ms 20748 KB Output is correct
4 Correct 111 ms 31028 KB Output is correct
5 Correct 112 ms 31108 KB Output is correct
6 Correct 80 ms 32736 KB Output is correct
7 Correct 113 ms 33748 KB Output is correct
8 Correct 100 ms 31736 KB Output is correct
9 Correct 76 ms 32064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14672 KB Output is correct
2 Correct 15 ms 15696 KB Output is correct
3 Correct 95 ms 22568 KB Output is correct
4 Correct 125 ms 24420 KB Output is correct
5 Correct 106 ms 24488 KB Output is correct
6 Correct 103 ms 24412 KB Output is correct
7 Correct 89 ms 24424 KB Output is correct
8 Correct 151 ms 24420 KB Output is correct
9 Correct 108 ms 24376 KB Output is correct
10 Correct 100 ms 24416 KB Output is correct
11 Correct 103 ms 27504 KB Output is correct
12 Correct 126 ms 28628 KB Output is correct
13 Correct 126 ms 28112 KB Output is correct
14 Correct 113 ms 27864 KB Output is correct
15 Correct 93 ms 24412 KB Output is correct
16 Correct 132 ms 24424 KB Output is correct
17 Correct 106 ms 27600 KB Output is correct
18 Correct 97 ms 28184 KB Output is correct
19 Correct 106 ms 24460 KB Output is correct
20 Correct 134 ms 28884 KB Output is correct
21 Correct 125 ms 23940 KB Output is correct
22 Correct 103 ms 23944 KB Output is correct
23 Correct 83 ms 23880 KB Output is correct
24 Correct 98 ms 24592 KB Output is correct
25 Correct 113 ms 32672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 15660 KB Output is correct
2 Correct 41 ms 15784 KB Output is correct
3 Correct 113 ms 24804 KB Output is correct
4 Correct 151 ms 24956 KB Output is correct
5 Correct 195 ms 25668 KB Output is correct
6 Correct 192 ms 25568 KB Output is correct
7 Correct 182 ms 25344 KB Output is correct
8 Correct 141 ms 25276 KB Output is correct
9 Correct 97 ms 21108 KB Output is correct
10 Correct 130 ms 21308 KB Output is correct
11 Correct 105 ms 21864 KB Output is correct
12 Correct 188 ms 23832 KB Output is correct
13 Correct 169 ms 24568 KB Output is correct
14 Correct 145 ms 25488 KB Output is correct
15 Correct 144 ms 25492 KB Output is correct
16 Correct 156 ms 21888 KB Output is correct
17 Correct 111 ms 23900 KB Output is correct
18 Correct 99 ms 24100 KB Output is correct
19 Correct 97 ms 24008 KB Output is correct
20 Correct 84 ms 24068 KB Output is correct
21 Correct 155 ms 26616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 15668 KB Output is correct
2 Correct 21 ms 15784 KB Output is correct
3 Correct 123 ms 24604 KB Output is correct
4 Correct 157 ms 24724 KB Output is correct
5 Correct 133 ms 24896 KB Output is correct
6 Correct 138 ms 25152 KB Output is correct
7 Correct 134 ms 24904 KB Output is correct
8 Correct 152 ms 24952 KB Output is correct
9 Correct 152 ms 24636 KB Output is correct
10 Correct 140 ms 25344 KB Output is correct
11 Correct 194 ms 25320 KB Output is correct
12 Correct 137 ms 25140 KB Output is correct
13 Correct 123 ms 21860 KB Output is correct
14 Correct 112 ms 21240 KB Output is correct
15 Correct 112 ms 21872 KB Output is correct
16 Correct 166 ms 21268 KB Output is correct
17 Correct 129 ms 21868 KB Output is correct
18 Correct 100 ms 21248 KB Output is correct
19 Correct 142 ms 23740 KB Output is correct
20 Correct 129 ms 24700 KB Output is correct
21 Correct 139 ms 25520 KB Output is correct
22 Correct 156 ms 25420 KB Output is correct
23 Correct 207 ms 25464 KB Output is correct
24 Correct 178 ms 25504 KB Output is correct
25 Correct 199 ms 25468 KB Output is correct
26 Correct 216 ms 25420 KB Output is correct
27 Correct 90 ms 24068 KB Output is correct
28 Correct 123 ms 23792 KB Output is correct
29 Correct 89 ms 24208 KB Output is correct
30 Correct 112 ms 24004 KB Output is correct
31 Correct 124 ms 23964 KB Output is correct
32 Correct 128 ms 23808 KB Output is correct
33 Correct 121 ms 24260 KB Output is correct
34 Correct 125 ms 23960 KB Output is correct
35 Correct 115 ms 23920 KB Output is correct
36 Correct 109 ms 23788 KB Output is correct
37 Correct 92 ms 24240 KB Output is correct
38 Correct 89 ms 24008 KB Output is correct
39 Correct 164 ms 27176 KB Output is correct
40 Correct 143 ms 27312 KB Output is correct
41 Correct 151 ms 26408 KB Output is correct
42 Correct 185 ms 26152 KB Output is correct