답안 #838138

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
838138 2023-08-26T09:20:14 Z becaido 통행료 (IOI18_highway) C++17
100 / 100
192 ms 12152 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "highway.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() { cout << "\n"; }
template<typename T, typename...U>
void dout(T t, U...u) { cout << t << (sizeof...(u) ? ", " : ""), dout(u...); }
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for(int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

#ifdef WAIMAI
ll ask(const vector<int> &w);
void answer(int s, int t);
#endif

const int SIZE = 1.3e5 + 5;

ll len;
vector<pair<int, int>> adj[SIZE], all[2];
vector<int> e;
int g[SIZE];
bool is[SIZE];

int cal(int s, int M) {
    int sz = all[s].size();
    int l = 0, r = sz - 1;
    while (l < r) {
        int mid = (l + r) / 2;
        FOR (i, 0, M - 1) e[i] = !is[i];
        FOR (i, mid + 1, sz - 1) e[all[s][i].S] = 1;
        if (ask(e) == len) r = mid;
        else l = mid + 1;
    }
    return all[s][l].F;
}

void find_pair(int N, vector<int> U, vector<int> V, int A, int B) {
    int M = U.size();
    e.resize(M, 0);
    FOR (i, 0, M - 1) {
        adj[U[i]].pb(V[i], i);
        adj[V[i]].pb(U[i], i);
    }

    len = ask(e);
    int l = 0, r = M - 1;
    while (l < r) {
        int mid = (l + r) / 2;
        fill(e.begin(), e.end(), 0);
        fill(e.begin(), e.begin() + mid + 1, 1);
        if (ask(e) == len) l = mid + 1;
        else r = mid;
    }

    queue<int> q;
    is[l] = 1;
    fill(g, g + N, -1);
    q.push(U[l]), g[U[l]] = 0, all[0].pb(U[l], -1);
    q.push(V[l]), g[V[l]] = 1, all[1].pb(V[l], -1);
    while (q.size()) {
        int pos = q.front();
        q.pop();
        for (auto [np, id] : adj[pos]) if (g[np] == -1) {
            g[np] = g[pos];
            is[id] = 1;
            all[g[pos]].pb(np, id);
            q.push(np);
        }
    }

    answer(cal(0, M), cal(1, M));
}

/*
in1
4 4
1 3 1 3
0 1
0 2
0 3
1 2
out1
1 3
*/

#ifdef WAIMAI
namespace {

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

int N, M, A, B, S, T;
vector<int> U, V;
vector<vector<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

ll ask(const 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");
    }
  }

  vector<bool> visited(N, false);
  vector<ll> current_dist(N, INF);
  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;
    ll 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() {
  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, vector<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
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3280 KB Output is correct
2 Correct 2 ms 3280 KB Output is correct
3 Correct 2 ms 3280 KB Output is correct
4 Correct 2 ms 3280 KB Output is correct
5 Correct 2 ms 3280 KB Output is correct
6 Correct 2 ms 3280 KB Output is correct
7 Correct 2 ms 3280 KB Output is correct
8 Correct 2 ms 3280 KB Output is correct
9 Correct 2 ms 3280 KB Output is correct
10 Correct 2 ms 3280 KB Output is correct
11 Correct 2 ms 3280 KB Output is correct
12 Correct 2 ms 3280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3408 KB Output is correct
2 Correct 28 ms 4164 KB Output is correct
3 Correct 80 ms 10068 KB Output is correct
4 Correct 78 ms 10064 KB Output is correct
5 Correct 112 ms 10044 KB Output is correct
6 Correct 89 ms 10104 KB Output is correct
7 Correct 78 ms 10056 KB Output is correct
8 Correct 96 ms 10072 KB Output is correct
9 Correct 104 ms 10032 KB Output is correct
10 Correct 96 ms 10044 KB Output is correct
11 Correct 107 ms 9336 KB Output is correct
12 Correct 77 ms 9772 KB Output is correct
13 Correct 83 ms 9540 KB Output is correct
14 Correct 79 ms 9592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 4048 KB Output is correct
2 Correct 15 ms 4768 KB Output is correct
3 Correct 26 ms 5400 KB Output is correct
4 Correct 122 ms 9304 KB Output is correct
5 Correct 64 ms 9312 KB Output is correct
6 Correct 72 ms 9728 KB Output is correct
7 Correct 98 ms 9464 KB Output is correct
8 Correct 62 ms 9292 KB Output is correct
9 Correct 73 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3408 KB Output is correct
2 Correct 12 ms 4048 KB Output is correct
3 Correct 84 ms 8792 KB Output is correct
4 Correct 99 ms 9992 KB Output is correct
5 Correct 71 ms 10020 KB Output is correct
6 Correct 75 ms 10036 KB Output is correct
7 Correct 124 ms 10040 KB Output is correct
8 Correct 71 ms 10056 KB Output is correct
9 Correct 114 ms 10148 KB Output is correct
10 Correct 114 ms 10068 KB Output is correct
11 Correct 136 ms 9564 KB Output is correct
12 Correct 94 ms 9640 KB Output is correct
13 Correct 81 ms 9712 KB Output is correct
14 Correct 103 ms 9224 KB Output is correct
15 Correct 72 ms 10044 KB Output is correct
16 Correct 80 ms 10076 KB Output is correct
17 Correct 103 ms 9656 KB Output is correct
18 Correct 90 ms 9568 KB Output is correct
19 Correct 102 ms 10096 KB Output is correct
20 Correct 74 ms 9464 KB Output is correct
21 Correct 61 ms 10824 KB Output is correct
22 Correct 62 ms 10816 KB Output is correct
23 Correct 118 ms 10356 KB Output is correct
24 Correct 72 ms 10260 KB Output is correct
25 Correct 86 ms 9688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 4048 KB Output is correct
2 Correct 14 ms 4260 KB Output is correct
3 Correct 90 ms 10420 KB Output is correct
4 Correct 127 ms 10984 KB Output is correct
5 Correct 153 ms 12068 KB Output is correct
6 Correct 164 ms 11596 KB Output is correct
7 Correct 126 ms 11640 KB Output is correct
8 Correct 122 ms 11652 KB Output is correct
9 Correct 117 ms 9456 KB Output is correct
10 Correct 84 ms 9940 KB Output is correct
11 Correct 109 ms 10428 KB Output is correct
12 Correct 157 ms 11476 KB Output is correct
13 Correct 192 ms 11632 KB Output is correct
14 Correct 138 ms 11820 KB Output is correct
15 Correct 121 ms 11960 KB Output is correct
16 Correct 111 ms 10636 KB Output is correct
17 Correct 71 ms 10528 KB Output is correct
18 Correct 88 ms 10580 KB Output is correct
19 Correct 126 ms 10480 KB Output is correct
20 Correct 119 ms 10572 KB Output is correct
21 Correct 178 ms 11976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 4048 KB Output is correct
2 Correct 11 ms 4176 KB Output is correct
3 Correct 92 ms 10348 KB Output is correct
4 Correct 115 ms 10572 KB Output is correct
5 Correct 101 ms 11040 KB Output is correct
6 Correct 145 ms 11568 KB Output is correct
7 Correct 105 ms 10436 KB Output is correct
8 Correct 99 ms 10688 KB Output is correct
9 Correct 100 ms 10708 KB Output is correct
10 Correct 128 ms 11720 KB Output is correct
11 Correct 120 ms 11988 KB Output is correct
12 Correct 188 ms 11592 KB Output is correct
13 Correct 110 ms 10460 KB Output is correct
14 Correct 94 ms 9924 KB Output is correct
15 Correct 120 ms 10460 KB Output is correct
16 Correct 87 ms 9928 KB Output is correct
17 Correct 91 ms 10448 KB Output is correct
18 Correct 94 ms 9984 KB Output is correct
19 Correct 156 ms 11276 KB Output is correct
20 Correct 154 ms 11788 KB Output is correct
21 Correct 190 ms 11824 KB Output is correct
22 Correct 175 ms 11752 KB Output is correct
23 Correct 134 ms 11740 KB Output is correct
24 Correct 122 ms 11612 KB Output is correct
25 Correct 118 ms 12092 KB Output is correct
26 Correct 150 ms 12084 KB Output is correct
27 Correct 123 ms 10528 KB Output is correct
28 Correct 117 ms 10456 KB Output is correct
29 Correct 119 ms 10652 KB Output is correct
30 Correct 72 ms 10632 KB Output is correct
31 Correct 76 ms 10508 KB Output is correct
32 Correct 73 ms 10428 KB Output is correct
33 Correct 89 ms 10708 KB Output is correct
34 Correct 72 ms 10496 KB Output is correct
35 Correct 90 ms 10504 KB Output is correct
36 Correct 73 ms 10404 KB Output is correct
37 Correct 102 ms 10640 KB Output is correct
38 Correct 76 ms 10540 KB Output is correct
39 Correct 113 ms 12152 KB Output is correct
40 Correct 122 ms 12072 KB Output is correct
41 Correct 150 ms 11880 KB Output is correct
42 Correct 117 ms 11632 KB Output is correct