Submission #1105242

# Submission time Handle Problem Language Result Execution time Memory
1105242 2024-10-25T20:29:08 Z alexander707070 Highway Tolls (IOI18_highway) C++14
90 / 100
215 ms 23876 KB
#include<bits/stdc++.h>
#define MAXN 200007

#include "highway.h"

using namespace std;

int n,m,dep[MAXN],from,to,st,et,parent[MAXN];
bool ok,li[MAXN];

vector< pair<int,int> > v[MAXN],g[MAXN];
vector<int> euler,euler2;
vector<int> w;

void dfs(int x,int p){

    dep[x]=dep[p]+1;
    parent[x]=p;

    for(int i=0;i<v[x].size();i++){
        if(v[x][i].first==p)continue;

        euler.push_back(v[x][i].second);
        dfs(v[x][i].first,x);
    }
}

void dfss(int x,int p){
    for(int i=0;i<v[x].size();i++){
        if(v[x][i].first==p)continue;

        euler2.push_back(v[x][i].second);
        dfss(v[x][i].first,x);
    }
}

queue<int> q;

void bfs(int x){
    for(int i=0;i<g[x].size();i++){
        if(li[g[x][i].first])continue;

        li[g[x][i].first]=true;
        q.push(g[x][i].first);

        v[x].push_back(g[x][i]);
        v[g[x][i].first].push_back({x,g[x][i].second});
    }
}

void dobfs(int x){
    q.push(x);
    li[x]=true;

    while(!q.empty()){
        bfs(q.front());
        q.pop();
    }
}

long long check(int x){
    for(int i=0;i<m;i++)w[i]=1;

    for(int y=1;y<=x;y++){
        for(int i=0;i<g[y].size();i++){
            w[g[y][i].second]=0;
        }
    }

    return ask(w);
}

long long pref(int x){
    for(int i=0;i<m;i++)w[i]=1;

    for(int i=0;i<=x;i++)w[euler[i]]=0;

    return ask(w);
}

long long pref2(int x){
    for(int i=0;i<m;i++)w[i]=1;

    for(int i=0;i<=x;i++)w[euler2[i]]=0;

    return ask(w);
}

void find_pair(int N, vector<int> U, vector<int> V, int A, int B) {

    n=N; m=int(U.size());
    for(int i=1;i<=m;i++){
        g[U[i-1]+1].push_back({V[i-1]+1,i-1});
        g[V[i-1]+1].push_back({U[i-1]+1,i-1});
    }

    w.resize(m);
    for(int i=0;i<m;i++)w[i]=0;
    long long path=ask(w);

    int l=0,r=n+1,tt;
    while(l+1<r){
        tt=(l+r)/2;
        if(check(tt)==path){
            r=tt;
        }else{
            l=tt;
        }
    }

    dobfs(r);

    dfs(1,0);
    for(int i=1;i<=n;i++){
        reverse(v[i].begin(),v[i].end());
    }
    dfss(1,0);

    l=-1; r=int(euler.size());
    while(l+1<r){
        tt=(l+r)/2;
        if(pref(tt)==path){
            r=tt;
        }else{
            l=tt;
        }
    }
    to=r;

    l=-1; r=int(euler2.size());

    for(int i=0;i<euler2.size();i++){
        if(euler2[i]==euler[to]){
            l=i-1; break;
        }
    }

    while(l+1<r){
        tt=(l+r)/2;
        if(pref2(tt)==path){
            r=tt;
        }else{
            l=tt;
        }
    }
    from=r;

    st=euler2[from]; et=euler[to];

    if(st==et){
        if(dep[U[st]+1]>dep[V[st]+1])swap(U[st],V[st]);

        U[st]++;
        for(int i=0;i<path/A-1;i++)U[st]=parent[U[st]];
        U[st]--;

        answer(U[st],V[st]);
        return;
    }

    if(dep[U[st]+1]>dep[V[st]+1])swap(U[st],V[st]);
    if(dep[U[et]+1]>dep[V[et]+1])swap(U[et],V[et]);

    answer(V[st],V[et]);

    return;
}

Compilation message

highway.cpp: In function 'void dfs(int, int)':
highway.cpp:20:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
highway.cpp: In function 'void dfss(int, int)':
highway.cpp:29:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
highway.cpp: In function 'void bfs(int)':
highway.cpp:40:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for(int i=0;i<g[x].size();i++){
      |                 ~^~~~~~~~~~~~
highway.cpp: In function 'long long int check(int)':
highway.cpp:65:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |         for(int i=0;i<g[y].size();i++){
      |                     ~^~~~~~~~~~~~
highway.cpp: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:132:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  132 |     for(int i=0;i<euler2.size();i++){
      |                 ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10832 KB Output is correct
2 Correct 3 ms 10832 KB Output is correct
3 Correct 2 ms 10832 KB Output is correct
4 Correct 2 ms 10832 KB Output is correct
5 Correct 3 ms 10832 KB Output is correct
6 Correct 2 ms 10832 KB Output is correct
7 Correct 3 ms 10860 KB Output is correct
8 Correct 3 ms 10832 KB Output is correct
9 Correct 3 ms 10832 KB Output is correct
10 Correct 3 ms 10832 KB Output is correct
11 Correct 3 ms 10832 KB Output is correct
12 Correct 3 ms 10832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10832 KB Output is correct
2 Correct 14 ms 11856 KB Output is correct
3 Correct 137 ms 20728 KB Output is correct
4 Correct 148 ms 20808 KB Output is correct
5 Correct 121 ms 20636 KB Output is correct
6 Correct 143 ms 20836 KB Output is correct
7 Correct 136 ms 20704 KB Output is correct
8 Correct 154 ms 20724 KB Output is correct
9 Correct 130 ms 20612 KB Output is correct
10 Correct 131 ms 20696 KB Output is correct
11 Correct 148 ms 20604 KB Output is correct
12 Correct 124 ms 20900 KB Output is correct
13 Correct 123 ms 20852 KB Output is correct
14 Correct 120 ms 20188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 12360 KB Output is correct
2 Correct 18 ms 13756 KB Output is correct
3 Correct 33 ms 14948 KB Output is correct
4 Correct 105 ms 23832 KB Output is correct
5 Correct 94 ms 23752 KB Output is correct
6 Correct 103 ms 23660 KB Output is correct
7 Correct 100 ms 23708 KB Output is correct
8 Correct 100 ms 23876 KB Output is correct
9 Correct 99 ms 23704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10832 KB Output is correct
2 Correct 14 ms 11972 KB Output is correct
3 Correct 97 ms 18628 KB Output is correct
4 Correct 126 ms 20612 KB Output is correct
5 Correct 126 ms 20756 KB Output is correct
6 Correct 134 ms 20640 KB Output is correct
7 Correct 128 ms 20672 KB Output is correct
8 Correct 127 ms 20880 KB Output is correct
9 Correct 147 ms 20672 KB Output is correct
10 Correct 138 ms 20716 KB Output is correct
11 Correct 127 ms 20104 KB Output is correct
12 Correct 134 ms 21372 KB Output is correct
13 Correct 126 ms 20728 KB Output is correct
14 Correct 155 ms 21064 KB Output is correct
15 Correct 122 ms 20784 KB Output is correct
16 Correct 118 ms 20772 KB Output is correct
17 Correct 126 ms 20900 KB Output is correct
18 Correct 117 ms 20612 KB Output is correct
19 Correct 121 ms 21172 KB Output is correct
20 Correct 145 ms 21468 KB Output is correct
21 Correct 106 ms 21548 KB Output is correct
22 Correct 115 ms 21588 KB Output is correct
23 Correct 115 ms 21144 KB Output is correct
24 Correct 122 ms 22084 KB Output is correct
25 Correct 133 ms 23164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 11856 KB Output is correct
2 Correct 16 ms 11992 KB Output is correct
3 Correct 157 ms 20916 KB Output is correct
4 Correct 172 ms 21368 KB Output is correct
5 Correct 198 ms 22204 KB Output is correct
6 Correct 195 ms 22188 KB Output is correct
7 Correct 184 ms 22020 KB Output is correct
8 Correct 203 ms 22028 KB Output is correct
9 Correct 142 ms 18424 KB Output is correct
10 Correct 143 ms 18136 KB Output is correct
11 Correct 151 ms 19496 KB Output is correct
12 Correct 176 ms 20940 KB Output is correct
13 Correct 189 ms 21460 KB Output is correct
14 Correct 179 ms 21948 KB Output is correct
15 Correct 196 ms 21772 KB Output is correct
16 Correct 156 ms 19036 KB Output is correct
17 Correct 142 ms 21420 KB Output is correct
18 Correct 123 ms 21628 KB Output is correct
19 Correct 121 ms 21504 KB Output is correct
20 Correct 116 ms 21668 KB Output is correct
21 Correct 194 ms 22344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 11856 KB Output is correct
2 Correct 17 ms 11856 KB Output is correct
3 Correct 155 ms 21000 KB Output is correct
4 Correct 156 ms 21184 KB Output is correct
5 Partially correct 165 ms 21500 KB Output is partially correct
6 Correct 186 ms 22196 KB Output is correct
7 Correct 157 ms 21348 KB Output is correct
8 Correct 143 ms 21132 KB Output is correct
9 Correct 164 ms 21344 KB Output is correct
10 Correct 193 ms 22028 KB Output is correct
11 Correct 194 ms 22108 KB Output is correct
12 Correct 210 ms 22128 KB Output is correct
13 Correct 151 ms 19228 KB Output is correct
14 Correct 130 ms 18160 KB Output is correct
15 Correct 133 ms 19228 KB Output is correct
16 Correct 132 ms 18276 KB Output is correct
17 Correct 149 ms 19232 KB Output is correct
18 Correct 134 ms 18364 KB Output is correct
19 Correct 201 ms 20900 KB Output is correct
20 Correct 179 ms 21720 KB Output is correct
21 Correct 173 ms 21928 KB Output is correct
22 Correct 196 ms 21900 KB Output is correct
23 Correct 183 ms 21956 KB Output is correct
24 Correct 205 ms 21856 KB Output is correct
25 Correct 192 ms 21776 KB Output is correct
26 Correct 194 ms 21932 KB Output is correct
27 Partially correct 125 ms 21660 KB Output is partially correct
28 Correct 120 ms 21424 KB Output is correct
29 Correct 126 ms 21996 KB Output is correct
30 Correct 123 ms 21684 KB Output is correct
31 Correct 121 ms 21704 KB Output is correct
32 Partially correct 122 ms 21396 KB Output is partially correct
33 Correct 132 ms 21808 KB Output is correct
34 Partially correct 122 ms 21684 KB Output is partially correct
35 Correct 131 ms 21456 KB Output is correct
36 Partially correct 143 ms 21660 KB Output is partially correct
37 Correct 149 ms 21832 KB Output is correct
38 Partially correct 129 ms 21640 KB Output is partially correct
39 Correct 215 ms 22396 KB Output is correct
40 Correct 200 ms 22504 KB Output is correct
41 Partially correct 193 ms 22284 KB Output is partially correct
42 Correct 194 ms 22124 KB Output is correct