Submission #283079

# Submission time Handle Problem Language Result Execution time Memory
283079 2020-08-25T09:38:02 Z Mercenary Highway Tolls (IOI18_highway) C++14
100 / 100
341 ms 11524 KB
#include "highway.h"

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>

#define pb push_back
#define mp make_pair
#define taskname "A"

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

const int maxn = 90000 + 5;
const int logn = log2(maxn) + 1;

int n , m;
vector<ii> adj[maxn];
int par[2][maxn];
int dis[2][maxn];

void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B) {
    n = N;m = U.size();
//    return;
    ll initial = ask(vector<int>(m , 0));
    int l = 0 , h = m - 1;
    while(l < h){
        int mid = l + h >> 1;
        vector<int> val(m , 0);
        for(int i = 0 ; i <= mid ; ++i)val[i] = 1;
        if(ask(val) > initial)h = mid;
        else l = mid + 1;
    }
    for(int i = 0 ; i < m ; ++i){
        adj[U[i]].pb(mp(V[i] , i));
        adj[V[i]].pb(mp(U[i] , i));
    }
    memset(par,-1,sizeof par);
    memset(dis,123,sizeof dis);
    auto bfs = [&](int dis[] , int par[] , int st){
        dis[st] = 0;queue<int> q;q.push(st);
        while(q.size()){
            auto u = q.front();q.pop();
            for(auto & c : adj[u]){
                if(dis[c.first] > dis[u] + 1){
                    dis[c.first] = dis[u] + 1;q.push(c.first);
                    par[c.first] = c.second;
                }
            }
        }
    };
    int u = U[l] , v = V[l];
    vector<int> valU , valV;
    bfs(dis[0] , par[0] , u);
    bfs(dis[1] , par[1] , v);
    for(int i = 0 ; i < n ; ++i){
        if(dis[0][i] < dis[1][i])valU.pb(i);
        else if(dis[0][i] > dis[1][i])valV.pb(i);
    }
//    cout << U[l] << " " << V[l] << endl;
    auto solve = [&](int & res , vector<int> & val , vector<int> & otherval, int must , int dis[] , int par[] , int otherpar[]){
        sort(val.begin(),val.end(),[&](const int u , const int v){return dis[u] < dis[v];});
        int l = 0 , h = (int)val.size() - 1;
        while(l < h){
            int mid = l + h >> 1;
            vector<int> query(m , 1);
            query[must] = 0;
            for(auto & c : otherval)if(otherpar[c] != -1)query[otherpar[c]] = 0;
            for(int i = 0 ; i <= mid ; ++i){
                if(par[val[i]] != -1)query[par[val[i]]] = 0;
            }
            if(ask(query) > initial)l = mid + 1;
            else h = mid;
        }
//        cout << val.size() << " " << val[l] << endl;
        res = val[l];
    };
    solve(u , valU , valV , l , dis[0] , par[0] , par[1]);
    solve(v , valV , valU , l , dis[1] , par[1] , par[0]);
    answer(u,v);
}

#ifdef LOCAL
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <utility>
#include <vector>
#include "highway.h"

namespace {

constexpr int MAX_NUM_CALLS = 100;
constexpr long long INF = 1LL << 61;

int N, M, A, B, S, T;
std::vector<int> U, V;
std::vector<std::vector<std::pair<int, int>>> graph;

bool answered, wrong_pair;
int num_calls;

int read_int() {
  int x;
  if (scanf("%d", &x) != 1) {
    fprintf(stderr, "Error while reading input\n");
    exit(1);
  }
  return x;
}

void wrong_answer(const char *MSG) {
  printf("Wrong Answer: %s\n", MSG);
  exit(0);
}

}  // namespace

long long ask(const std::vector<int> &w) {
  if (++num_calls > MAX_NUM_CALLS) {
    wrong_answer("more than 100 calls to ask");
  }
  if (w.size() != (size_t)M) {
    wrong_answer("w is invalid");
  }
  for (size_t i = 0; i < w.size(); ++i) {
    if (!(w[i] == 0 || w[i] == 1)) {
      wrong_answer("w is invalid");
    }
  }

  std::vector<bool> visited(N, false);
  std::vector<long long> current_dist(N, INF);
  std::queue<int> qa, qb;
  qa.push(S);
  current_dist[S] = 0;
  while (!qa.empty() || !qb.empty()) {
    int v;
    if (qb.empty() ||
        (!qa.empty() && current_dist[qa.front()] <= current_dist[qb.front()])) {
      v = qa.front();
      qa.pop();
    } else {
      v = qb.front();
      qb.pop();
    }
    if (visited[v]) {
      continue;
    }
    visited[v] = true;
    long long d = current_dist[v];
    if (v == T) {
      return d;
    }
    for (auto e : graph[v]) {
      int vv = e.first;
      int ei = e.second;
      if (!visited[vv]) {
        if (w[ei] == 0) {
          if (current_dist[vv] > d + A) {
            current_dist[vv] = d + A;
            qa.push(vv);
          }
        } else {
          if (current_dist[vv] > d + B) {
            current_dist[vv] = d + B;
            qb.push(vv);
          }
        }
      }
    }
  }
  return -1;
}

void answer(int s, int t) {
  if (answered) {
    wrong_answer("answered not exactly once");
  }

  if (!((s == S && t == T) || (s == T && t == S))) {
    wrong_pair = true;
  }

  answered = true;
}

int main() {
    freopen(taskname".INP","r",stdin);
  N = read_int();
  M = read_int();

  A = read_int();
  B = read_int();
  S = read_int();
  T = read_int();
  U.resize(M);
  V.resize(M);
  graph.assign(N, std::vector<std::pair<int, int>>());
  for (int i = 0; i < M; ++i) {
    U[i] = read_int();
    V[i] = read_int();
    graph[U[i]].push_back({V[i], i});
    graph[V[i]].push_back({U[i], i});
  }

  answered = false;
  wrong_pair = false;
  num_calls = 0;
  find_pair(N, U, V, A, B);
  if (!answered) {
    wrong_answer("answered not exactly once");
  }
  if (wrong_pair) {
    wrong_answer("{s, t} is wrong");
  }
  printf("Accepted: %d\n", num_calls);
  return 0;
}

#endif // LOCAL

Compilation message

highway.cpp: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:33:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   33 |         int mid = l + h >> 1;
      |                   ~~^~~
highway.cpp: In lambda function:
highway.cpp:70:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   70 |             int mid = l + h >> 1;
      |                       ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3840 KB Output is correct
2 Correct 3 ms 3840 KB Output is correct
3 Correct 4 ms 3840 KB Output is correct
4 Correct 3 ms 3840 KB Output is correct
5 Correct 4 ms 3840 KB Output is correct
6 Correct 3 ms 3840 KB Output is correct
7 Correct 3 ms 3840 KB Output is correct
8 Correct 3 ms 3840 KB Output is correct
9 Correct 3 ms 3840 KB Output is correct
10 Correct 3 ms 3840 KB Output is correct
11 Correct 3 ms 3840 KB Output is correct
12 Correct 3 ms 3840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3840 KB Output is correct
2 Correct 16 ms 4480 KB Output is correct
3 Correct 170 ms 9604 KB Output is correct
4 Correct 169 ms 9544 KB Output is correct
5 Correct 211 ms 9552 KB Output is correct
6 Correct 157 ms 9692 KB Output is correct
7 Correct 189 ms 9556 KB Output is correct
8 Correct 154 ms 9564 KB Output is correct
9 Correct 207 ms 9552 KB Output is correct
10 Correct 191 ms 9564 KB Output is correct
11 Correct 248 ms 9288 KB Output is correct
12 Correct 241 ms 9040 KB Output is correct
13 Correct 192 ms 9124 KB Output is correct
14 Correct 202 ms 8980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 4352 KB Output is correct
2 Correct 38 ms 4984 KB Output is correct
3 Correct 43 ms 5656 KB Output is correct
4 Correct 181 ms 9180 KB Output is correct
5 Correct 177 ms 9108 KB Output is correct
6 Correct 177 ms 9132 KB Output is correct
7 Correct 138 ms 8992 KB Output is correct
8 Correct 183 ms 9236 KB Output is correct
9 Correct 151 ms 9016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3840 KB Output is correct
2 Correct 24 ms 4472 KB Output is correct
3 Correct 157 ms 8360 KB Output is correct
4 Correct 214 ms 9560 KB Output is correct
5 Correct 162 ms 9556 KB Output is correct
6 Correct 200 ms 9564 KB Output is correct
7 Correct 156 ms 9588 KB Output is correct
8 Correct 150 ms 9548 KB Output is correct
9 Correct 224 ms 9544 KB Output is correct
10 Correct 192 ms 9548 KB Output is correct
11 Correct 202 ms 9004 KB Output is correct
12 Correct 260 ms 8948 KB Output is correct
13 Correct 205 ms 9036 KB Output is correct
14 Correct 185 ms 9164 KB Output is correct
15 Correct 207 ms 9552 KB Output is correct
16 Correct 206 ms 9556 KB Output is correct
17 Correct 216 ms 8980 KB Output is correct
18 Correct 201 ms 9216 KB Output is correct
19 Correct 156 ms 9560 KB Output is correct
20 Correct 196 ms 8984 KB Output is correct
21 Correct 132 ms 9712 KB Output is correct
22 Correct 125 ms 9720 KB Output is correct
23 Correct 145 ms 9796 KB Output is correct
24 Correct 200 ms 9788 KB Output is correct
25 Correct 187 ms 9068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 4480 KB Output is correct
2 Correct 26 ms 4512 KB Output is correct
3 Correct 238 ms 9852 KB Output is correct
4 Correct 224 ms 10324 KB Output is correct
5 Correct 249 ms 11304 KB Output is correct
6 Correct 310 ms 11292 KB Output is correct
7 Correct 260 ms 11316 KB Output is correct
8 Correct 245 ms 11272 KB Output is correct
9 Correct 206 ms 9580 KB Output is correct
10 Correct 178 ms 10268 KB Output is correct
11 Correct 200 ms 10460 KB Output is correct
12 Correct 238 ms 10936 KB Output is correct
13 Correct 308 ms 11236 KB Output is correct
14 Correct 264 ms 11408 KB Output is correct
15 Correct 252 ms 11344 KB Output is correct
16 Correct 206 ms 10464 KB Output is correct
17 Correct 157 ms 9748 KB Output is correct
18 Correct 149 ms 9976 KB Output is correct
19 Correct 198 ms 9844 KB Output is correct
20 Correct 174 ms 9908 KB Output is correct
21 Correct 341 ms 11268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 4564 KB Output is correct
2 Correct 17 ms 4524 KB Output is correct
3 Correct 239 ms 9964 KB Output is correct
4 Correct 245 ms 10100 KB Output is correct
5 Correct 211 ms 10320 KB Output is correct
6 Correct 311 ms 11284 KB Output is correct
7 Correct 168 ms 9900 KB Output is correct
8 Correct 193 ms 10112 KB Output is correct
9 Correct 203 ms 10340 KB Output is correct
10 Correct 233 ms 11352 KB Output is correct
11 Correct 254 ms 11400 KB Output is correct
12 Correct 234 ms 11268 KB Output is correct
13 Correct 178 ms 10572 KB Output is correct
14 Correct 174 ms 10320 KB Output is correct
15 Correct 241 ms 10456 KB Output is correct
16 Correct 171 ms 10304 KB Output is correct
17 Correct 173 ms 10436 KB Output is correct
18 Correct 223 ms 10252 KB Output is correct
19 Correct 228 ms 10944 KB Output is correct
20 Correct 260 ms 11172 KB Output is correct
21 Correct 260 ms 11432 KB Output is correct
22 Correct 306 ms 11400 KB Output is correct
23 Correct 241 ms 11464 KB Output is correct
24 Correct 251 ms 11396 KB Output is correct
25 Correct 245 ms 11320 KB Output is correct
26 Correct 255 ms 11372 KB Output is correct
27 Correct 154 ms 9892 KB Output is correct
28 Correct 155 ms 9808 KB Output is correct
29 Correct 146 ms 10040 KB Output is correct
30 Correct 177 ms 10016 KB Output is correct
31 Correct 155 ms 9952 KB Output is correct
32 Correct 158 ms 9784 KB Output is correct
33 Correct 163 ms 10076 KB Output is correct
34 Correct 195 ms 9948 KB Output is correct
35 Correct 198 ms 9848 KB Output is correct
36 Correct 150 ms 9836 KB Output is correct
37 Correct 158 ms 10044 KB Output is correct
38 Correct 148 ms 9932 KB Output is correct
39 Correct 246 ms 11524 KB Output is correct
40 Correct 270 ms 11344 KB Output is correct
41 Correct 306 ms 11192 KB Output is correct
42 Correct 259 ms 11424 KB Output is correct