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<bits/stdc++.h>
#define MAXN 100007
#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<=x;i++)w[i]=1;
for(int i=x+1;i<m;i++)w[i]=0;
return ask(w);
}
long long pref(int x){
for(int i=0;i<=x;i++)w[euler[i]]=1;
for(int i=x+1;i<m;i++)w[euler[i]]=0;
return ask(w);
}
long long pref2(int x){
for(int i=0;i<=x;i++)w[euler2[i]]=1;
for(int i=x+1;i<m;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]=1;
long long path=ask(w);
int l=-1,r=n,tt;
while(l+1<r){
tt=(l+r)/2;
if(check(tt)==path){
r=tt;
}else{
l=tt;
}
}
r++;
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());
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/B-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 (stderr)
highway.cpp: In function 'void dfs(int, int)':
highway.cpp:19: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]
19 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
highway.cpp: In function 'void dfss(int, int)':
highway.cpp:28: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]
28 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
highway.cpp: In function 'void bfs(int)':
highway.cpp:39: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]
39 | for(int i=0;i<g[x].size();i++){
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |