#include "highway.h"
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
int n, m;
vector<int>w;
vector<int>gs[maxn];
vector<pair<int,int> > g[maxn];
vector<int>gt[maxn];
int dist[maxn][2], parent[maxn][2];
bool visited[maxn];
ll dista;
map<pair<int,int>, int> ind;
int type[maxn];
void bfs(int u, int v) {
visited[u] = visited[v] = true;
queue<pair<int,int> > q;
type[u] = 0;
type[v] = 1;
q.push(mp(u, 0));
q.push(mp(v, 1));
while(!q.empty()) {
int curr = q.front().first;
int currtype = q.front().second;
q.pop();
for(auto i:gs[curr]) {
if(visited[i]) continue;
visited[i] = true;
type[i] = currtype;
q.push(mp(i, currtype));
}
}
}
vector<pair<int,int> > sta, stb;
void preprocess() {
for(int i=0;i<n;i++) {
if(type[i] == 0) {
for(auto j:gs[i]) {
if(type[j] == type[i]) {
sta.pb(mp(min(i, j), max(i, j)));
}
}
}
else {
for(auto j:gs[i]) {
if(type[j] == type[i]) {
stb.pb(mp(min(i, j), max(i, j)));
}
}
}
}
sort( sta.begin(), sta.end() );
sta.erase( unique( sta.begin(), sta.end() ), sta.end() );
sort( stb.begin(), stb.end() );
stb.erase( unique( stb.begin(), stb.end() ), stb.end() );
}
int solve(int root, int d) {
// build bfs tree
//memset(visited,false,sizeof(visited));
//memset(parent,-1,sizeof(parent));
vector<pair<int,int> > edges;
if(d == 0) edges = sta;
else edges = stb;
for(auto i:edges) {
gt[i.first].pb(i.second);
gt[i.second].pb(i.first);
}
vector<int>v, v2;
queue<int>q;
q.push(root);
visited[root] = true;
parent[root][d] = -1;
while(!q.empty()) {
int curr = q.front();
q.pop();
v.pb(parent[curr][d]);
v2.pb(curr);
for(auto i:gt[curr]) {
if(!visited[i]) {
visited[i] = true;
parent[i][d] = curr;
dist[i][d] = dist[curr][d] + 1;
q.push(i);
}
}
}
if(v.size() == 1) return v2[0];
// perform binary search
for(int i=0;i<v.size();i++) {
if(v[i] == -1) continue; // root
int cind = ind[mp(v[i], v2[i])];
w[cind] = 1;
}
int li = 0, ri = v.size() - 1;
while(li < ri) {
int mid = (li + ri) / 2;
// all before mid put them as low cost and other as high cost
for(int i=0;i<=mid;i++) {
if(v[i] == -1) continue; // root
int cind = ind[mp(v[i], v2[i])];
w[cind] = 0;
}
ll cdist = ask(w);
for(int i=0;i<=mid;i++) {
if(v[i] == -1) continue; // root
int cind = ind[mp(v[i], v2[i])];
w[cind] = 1;
}
if(cdist == dista) {
ri = mid;
}
else {
li = mid + 1;
}
}
for(int i=0;i<v.size();i++) {
if(v[i] == -1) continue; // root
int cind = ind[mp(v[i], v2[i])];
w[cind] = 0;
}
return v2[li];
}
void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B) {
n = N; m = U.size();
int li=0, ri=m-1;
for(int i=0;i<m;i++) {
ind[mp(U[i], V[i])] = ind[mp(V[i], U[i])] = i;
gs[U[i]].pb(V[i]);
gs[V[i]].pb(U[i]);
g[U[i]].pb(mp(V[i], i));
g[V[i]].pb(mp(U[i], i));
w.pb(0);
}
dista = ask(w);
while(li<ri) {
int mid = (li+ri)/2;
for(int i=li;i<=mid;i++) {
w[i] = 1;
}
ll distb = ask(w);
for(int i=li;i<=mid;i++) {
w[i] = 0;
}
if(dista == distb) {
li = mid + 1;
}
else {
ri = mid;
}
}
int x = li;
int u = U[x], v = V[x];
//cout<<u<<" "<<v<<"\n";
bfs(u, v);
preprocess();
memset(visited,false,sizeof(visited));
//cout<<"Ans: "<<xx<<" "<<yy<<"\n";
answer(solve(u, 0), solve(v, 1));
}
Compilation message
highway.cpp: In function 'int solve(int, int)':
highway.cpp:116:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v.size();i++) {
~^~~~~~~~~
highway.cpp:149:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v.size();i++) {
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7464 KB |
Output is correct |
2 |
Correct |
9 ms |
7416 KB |
Output is correct |
3 |
Correct |
9 ms |
7416 KB |
Output is correct |
4 |
Correct |
8 ms |
7400 KB |
Output is correct |
5 |
Correct |
9 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7416 KB |
Output is correct |
7 |
Correct |
8 ms |
7392 KB |
Output is correct |
8 |
Correct |
9 ms |
7416 KB |
Output is correct |
9 |
Correct |
8 ms |
7396 KB |
Output is correct |
10 |
Correct |
8 ms |
7392 KB |
Output is correct |
11 |
Correct |
9 ms |
7416 KB |
Output is correct |
12 |
Correct |
8 ms |
7396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
7728 KB |
Output is correct |
2 |
Correct |
61 ms |
10512 KB |
Output is correct |
3 |
Correct |
1434 ms |
35060 KB |
Output is correct |
4 |
Execution timed out |
1681 ms |
35112 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
10432 KB |
Output is correct |
2 |
Correct |
73 ms |
13228 KB |
Output is correct |
3 |
Correct |
113 ms |
16328 KB |
Output is correct |
4 |
Correct |
442 ms |
33048 KB |
Output is correct |
5 |
Correct |
384 ms |
32988 KB |
Output is correct |
6 |
Correct |
651 ms |
33424 KB |
Output is correct |
7 |
Correct |
339 ms |
34220 KB |
Output is correct |
8 |
Correct |
541 ms |
32816 KB |
Output is correct |
9 |
Correct |
337 ms |
33920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
7672 KB |
Output is correct |
2 |
Correct |
59 ms |
10444 KB |
Output is correct |
3 |
Correct |
500 ms |
29104 KB |
Output is correct |
4 |
Correct |
733 ms |
35072 KB |
Output is correct |
5 |
Correct |
641 ms |
35272 KB |
Output is correct |
6 |
Correct |
691 ms |
34848 KB |
Output is correct |
7 |
Correct |
642 ms |
34948 KB |
Output is correct |
8 |
Correct |
635 ms |
34940 KB |
Output is correct |
9 |
Correct |
1191 ms |
35020 KB |
Output is correct |
10 |
Correct |
1018 ms |
35052 KB |
Output is correct |
11 |
Execution timed out |
1634 ms |
34240 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
10568 KB |
Output is correct |
2 |
Correct |
76 ms |
10860 KB |
Output is correct |
3 |
Execution timed out |
1922 ms |
36620 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
10660 KB |
Output is correct |
2 |
Correct |
96 ms |
10924 KB |
Output is correct |
3 |
Correct |
728 ms |
35896 KB |
Output is correct |
4 |
Incorrect |
1069 ms |
37024 KB |
Output is incorrect: {s, t} is wrong. |
5 |
Halted |
0 ms |
0 KB |
- |