# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
965393 | Warinchai | Highway Tolls (IOI18_highway) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "highway.h"
#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int>>adj[100000];
void dfs(int u,vector<int>&x,vector<int>&nodes,int p,int val){
x.push_back(val);
nodes.push_back(u);
for(auto v:adj[u])if(v.first!=p)dfs(v.first,x,nodes,u,v.second);
}
void subtree(int n,vector<int>u,vector<int>v,int a,int b,int cost){
int st=0,en=u.size()-1,ans=0;
while(st<=en){
int m=(st+en)/2;
vector<int>temp;
for(int i=0;i<m;i++)temp.push_back(1);
for(int i=m;i<n;i++)temp.push_back(0);
if(ask(temp)>cost){
en=m-1;
ans=m;
}else{
st=m+1;
}
}
int a=u[ans];
int b=v[ans];
vector<int>temp;
vector<int>side1;
vector<int>side2;
vector<int>node1;
vector<int>node2;
for(int i=0;i<n;i++)temp.push_back(0);
dfs(a,side1,node1,b,ans);
dfs(b,side2,node2,a,ans);
for(auto x:side1)temp[x]=1;
int cost1=ask(temp);
for(auto x:side1)temp[x]=0;
for(auto x:side2)temp[x]=1;
int cost2=ask(temp);
for(int i=0;i<n;i++)temp[i]=0;
int n1,n2;
st=0,en=side1.size();
for(int i=0;i<n;i++)temp[i]=0;
while(st<=en){
int m=(st+en)/2;
for(int i=0;i<m;i++)temp[side1[i]]=1;
if(ask(temp)<=cost1){
st=m+1;
n1=m;
}else{
en=m-1;
}
for(int i=0;i<m;i++)temp[side1[i]]=0;
}
st=0,en=side2.size();
while(st<=en){
int m=(st+en)/2;
for(int i=0;i<m;i++)temp[side2[i]]=1;
if(ask(temp)<=cost2){
st=m+1;
n2=m;
}else{
en=m-1;
}
for(int i=0;i<m;i++)temp[side2[i]]=0;
}
answer(node1[n1],node2[n2]);
}
void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B) {
vector<int>temp;
for(int i=0;i<N;i++)temp.push_back(0);
if(U.size()!=N-1)return answer(rand()%N,rand()%N);
else return subtree(N,U,V,A,B,ask(temp));
}