제출 #422042

#제출 시각아이디문제언어결과실행 시간메모리
422042SundavarHighway Tolls (IOI18_highway)C++14
100 / 100
322 ms12700 KiB
#include <bits/stdc++.h>
#include "highway.h"
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
struct node{
  vector<pii> to;
  int depth = 0, start = -1, with = -1;
};
vector<node> graph;
vector<int> s;
int getEdge(int l, int r, int M, ll dist){
  if(l+1 == r) return l;
  int m = (l+r)/2-1;
  s.assign(M, 0);
  for(int i = 0; i <= m; i++) s[i] = 1;
  if(ask(s) == dist)
    return getEdge(m+1, r, M, dist);
  else
    return getEdge(l, m+1, M, dist);
}
void BFS(int x, int y, int id){
  deque<int> q;
  q.push_back(x), q.push_back(y);
  graph[x].start = x, graph[y].start = y;
  graph[x].with = graph[y].with = id;
  while(q.size() > 0){
    int curr = q.front();
    q.pop_front();
    for(pii& a : graph[curr].to)
      if(graph[a.first].start == -1){
        q.push_back(a.first);
        graph[a.first].start = graph[curr].start;
        graph[a.first].depth = graph[curr].depth + 1;
        graph[a.first].with = a.second;
      }
  }
}
bool sortFunc(int a, int b){
  return graph[a].depth < graph[b].depth;
}
int find(int u, int N, int M, ll length, ll A, ll B){
  vector<int> S(M, 1);
  vector<int> poss;
  for(int i = 0; i < N; i++)
    if(graph[i].start == u) poss.push_back(i);
  sort(poss.begin(), poss.end(), sortFunc);
  int l = 0, r = poss.size();
  while(r-l > 1){
    int m = (l+r)/2-1;
    S.assign(M, 1);
    for(int i = 0; i < N; i++)
      if(graph[i].start != u) S[graph[i].with] = 0;
    for(int i = 0; i <= m; i++) S[graph[poss[i]].with] = 0;
    if(ask(S) == length*A)
      r = m+1;
    else
      l = m+1;
  }
  return poss[l];
}
void find_pair(int N, vector<int> U, vector<int> V, int A, int B){
  graph.resize(N);
  int M = U.size();
  for(int i = 0; i < M; i++){
    graph[U[i]].to.push_back({V[i],i});
    graph[V[i]].to.push_back({U[i],i});
  }
  vector<int> S(M, 0);
  ll dist = ask(S), length = dist/A;
  s.resize(M, 0);
  int ind = getEdge(0, M, M, dist);
  BFS(V[ind], U[ind], ind);
  answer(find(U[ind], N, M, length, A, B), find(V[ind], N, M, length, A, B));
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...