#include "highway.h"
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define lb lower_bound
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
typedef long long ll;
using namespace std;
const int mxN=300050;
const int mxM=10000;
const int mxK=60;
const ll INF=1e18;
int N, M;
vector <int> adj[mxN];
vector <pii> E;
ll val0;
int S1, S2;
int dist[mxN][2];
int col[mxN];
vector <int> ct[2];
bool trash[mxN];
vector <int> par[mxN];
int ans[2];
void init(int n, vector <int> u, vector <int> v, int a, int b){
N=n;
M=u.size();
for(int i=0;i<M;i++){
int t1=u[i], t2=v[i];
adj[t1].push_back(t2);
adj[t2].push_back(t1);
E.emplace_back(t1, t2);
}
}
void find_edge(){
vector <int> w(M), one(M);
val0=ask(w);
int s=0, e=M-1;
while(s!=e){
int mid=(s+e)/2;
for(int i=0;i<M;i++) w[i]=((s<=i && i<=mid) ? 1 : 0);
for(int i=0;i<M;i++) if(one[i]==1) w[i]=1;
ll valb=ask(w);
if(valb>val0) e=mid;
else{
for(int i=s;i<=mid;i++) one[i]=1;
s=mid+1;
}
}
S1=E[s].fi, S2=E[s].se;
}
void bfs(int st, int idx){
for(int i=0;i<N;i++) dist[i][idx]=-1;
queue <int> que;
que.push(st);
dist[st][idx]=0;
while(que.size()){
int now=que.front();
que.pop();
for(int x : adj[now]) if(dist[x][idx]==-1){
dist[x][idx]=dist[now][idx]+1;
que.push(x);
}
}
}
void classify_vertex(){
for(int i=0;i<N;i++){
if(dist[i][0]==dist[i][1]){
col[i]=-1;
continue;
}
if(dist[i][0]<dist[i][1]) ct[0].push_back(i), col[i]=0;
else ct[1].push_back(i), col[i]=1;
}
for(int i=0;i<M;i++){
if(E[i]==pii(S1, S2) || E[i]==pii(S2, S1)) continue;
int a=E[i].fi, b=E[i].se;
if(col[a]==-1 || col[b]==-1 || col[a]!=col[b]){
trash[i]=true;
continue;
}
int c=col[a];
if(dist[a][c]==dist[b][c]){
trash[i]=true;
continue;
}
if(dist[a][c]<dist[b][c]) swap(a, b);
par[a].push_back(i);
}
for(int i=0;i<M;i++) if(E[i]==pii(S1, S2) || E[i]==pii(S2, S1)) par[S1].push_back(i), par[S2].push_back(i);
}
void find_vertex(int idx){
sort(all(ct[idx]), [&](int a, int b){return dist[a][idx]>dist[b][idx];});
int s=0, e=ct[idx].size()-1;
while(s!=e){
int mid=(s+e)/2;
vector <int> w(M);
for(int i=0;i<=mid;i++) for(int x : par[ct[idx][i]]) w[x]=1;
for(int i=0;i<M;i++) if(trash[i]) w[i]=1;
ll valw=ask(w);
if(valw>val0) e=mid;
else s=mid+1;
}
ans[idx]=ct[idx][s];
}
void find_pair(int n, std::vector<int> U, std::vector<int> V, int A, int B) {
init(n, U, V, A, B);
find_edge();
bfs(S1, 0);
bfs(S2, 1);
classify_vertex();
find_vertex(0);
find_vertex(1);
answer(ans[0], ans[1]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
6 ms |
14424 KB |
Output is correct |
3 |
Correct |
6 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14424 KB |
Output is correct |
5 |
Correct |
6 ms |
14424 KB |
Output is correct |
6 |
Correct |
6 ms |
14424 KB |
Output is correct |
7 |
Correct |
6 ms |
14424 KB |
Output is correct |
8 |
Correct |
6 ms |
14424 KB |
Output is correct |
9 |
Correct |
7 ms |
14556 KB |
Output is correct |
10 |
Correct |
6 ms |
14424 KB |
Output is correct |
11 |
Correct |
6 ms |
14424 KB |
Output is correct |
12 |
Correct |
6 ms |
14424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14420 KB |
Output is correct |
2 |
Correct |
16 ms |
15476 KB |
Output is correct |
3 |
Correct |
123 ms |
24152 KB |
Output is correct |
4 |
Correct |
113 ms |
24164 KB |
Output is correct |
5 |
Correct |
112 ms |
24164 KB |
Output is correct |
6 |
Correct |
101 ms |
24540 KB |
Output is correct |
7 |
Correct |
120 ms |
24172 KB |
Output is correct |
8 |
Correct |
110 ms |
24168 KB |
Output is correct |
9 |
Correct |
110 ms |
24172 KB |
Output is correct |
10 |
Correct |
111 ms |
24168 KB |
Output is correct |
11 |
Correct |
131 ms |
24312 KB |
Output is correct |
12 |
Correct |
121 ms |
24124 KB |
Output is correct |
13 |
Correct |
118 ms |
24072 KB |
Output is correct |
14 |
Correct |
122 ms |
24068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
15448 KB |
Output is correct |
2 |
Correct |
23 ms |
16596 KB |
Output is correct |
3 |
Correct |
33 ms |
17644 KB |
Output is correct |
4 |
Correct |
82 ms |
24184 KB |
Output is correct |
5 |
Correct |
76 ms |
24188 KB |
Output is correct |
6 |
Correct |
85 ms |
24128 KB |
Output is correct |
7 |
Correct |
87 ms |
24088 KB |
Output is correct |
8 |
Correct |
82 ms |
24068 KB |
Output is correct |
9 |
Correct |
100 ms |
24104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14424 KB |
Output is correct |
2 |
Correct |
20 ms |
15440 KB |
Output is correct |
3 |
Correct |
83 ms |
22060 KB |
Output is correct |
4 |
Correct |
104 ms |
24172 KB |
Output is correct |
5 |
Correct |
106 ms |
24172 KB |
Output is correct |
6 |
Correct |
112 ms |
24172 KB |
Output is correct |
7 |
Correct |
119 ms |
24168 KB |
Output is correct |
8 |
Correct |
120 ms |
24168 KB |
Output is correct |
9 |
Correct |
109 ms |
24172 KB |
Output is correct |
10 |
Correct |
121 ms |
24172 KB |
Output is correct |
11 |
Correct |
122 ms |
24068 KB |
Output is correct |
12 |
Correct |
121 ms |
24072 KB |
Output is correct |
13 |
Correct |
122 ms |
24116 KB |
Output is correct |
14 |
Correct |
133 ms |
24092 KB |
Output is correct |
15 |
Correct |
102 ms |
24168 KB |
Output is correct |
16 |
Correct |
109 ms |
24192 KB |
Output is correct |
17 |
Correct |
113 ms |
24064 KB |
Output is correct |
18 |
Correct |
114 ms |
24452 KB |
Output is correct |
19 |
Correct |
109 ms |
24180 KB |
Output is correct |
20 |
Correct |
133 ms |
24072 KB |
Output is correct |
21 |
Correct |
97 ms |
24424 KB |
Output is correct |
22 |
Correct |
96 ms |
24428 KB |
Output is correct |
23 |
Correct |
109 ms |
24440 KB |
Output is correct |
24 |
Correct |
99 ms |
24472 KB |
Output is correct |
25 |
Correct |
127 ms |
24152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
15624 KB |
Output is correct |
2 |
Correct |
17 ms |
15680 KB |
Output is correct |
3 |
Correct |
124 ms |
24684 KB |
Output is correct |
4 |
Correct |
151 ms |
24688 KB |
Output is correct |
5 |
Correct |
174 ms |
25172 KB |
Output is correct |
6 |
Correct |
175 ms |
25052 KB |
Output is correct |
7 |
Correct |
173 ms |
24880 KB |
Output is correct |
8 |
Correct |
169 ms |
24876 KB |
Output is correct |
9 |
Correct |
125 ms |
21372 KB |
Output is correct |
10 |
Correct |
106 ms |
21632 KB |
Output is correct |
11 |
Correct |
119 ms |
22192 KB |
Output is correct |
12 |
Correct |
177 ms |
23620 KB |
Output is correct |
13 |
Correct |
169 ms |
24208 KB |
Output is correct |
14 |
Correct |
172 ms |
24876 KB |
Output is correct |
15 |
Correct |
163 ms |
24968 KB |
Output is correct |
16 |
Correct |
137 ms |
22040 KB |
Output is correct |
17 |
Correct |
115 ms |
24612 KB |
Output is correct |
18 |
Correct |
99 ms |
24680 KB |
Output is correct |
19 |
Correct |
117 ms |
24620 KB |
Output is correct |
20 |
Correct |
95 ms |
25120 KB |
Output is correct |
21 |
Correct |
159 ms |
25476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
15440 KB |
Output is correct |
2 |
Correct |
17 ms |
15440 KB |
Output is correct |
3 |
Correct |
126 ms |
24352 KB |
Output is correct |
4 |
Correct |
136 ms |
24312 KB |
Output is correct |
5 |
Correct |
146 ms |
24468 KB |
Output is correct |
6 |
Correct |
161 ms |
24880 KB |
Output is correct |
7 |
Correct |
122 ms |
24732 KB |
Output is correct |
8 |
Correct |
123 ms |
24644 KB |
Output is correct |
9 |
Correct |
139 ms |
24304 KB |
Output is correct |
10 |
Correct |
163 ms |
24924 KB |
Output is correct |
11 |
Correct |
173 ms |
24976 KB |
Output is correct |
12 |
Correct |
169 ms |
24872 KB |
Output is correct |
13 |
Correct |
127 ms |
22180 KB |
Output is correct |
14 |
Correct |
128 ms |
21564 KB |
Output is correct |
15 |
Correct |
121 ms |
22164 KB |
Output is correct |
16 |
Correct |
116 ms |
21632 KB |
Output is correct |
17 |
Correct |
122 ms |
22196 KB |
Output is correct |
18 |
Correct |
113 ms |
21560 KB |
Output is correct |
19 |
Correct |
161 ms |
23408 KB |
Output is correct |
20 |
Correct |
162 ms |
24312 KB |
Output is correct |
21 |
Correct |
160 ms |
24892 KB |
Output is correct |
22 |
Correct |
165 ms |
24832 KB |
Output is correct |
23 |
Correct |
174 ms |
24992 KB |
Output is correct |
24 |
Correct |
193 ms |
24884 KB |
Output is correct |
25 |
Correct |
169 ms |
24952 KB |
Output is correct |
26 |
Correct |
165 ms |
24912 KB |
Output is correct |
27 |
Correct |
108 ms |
24688 KB |
Output is correct |
28 |
Correct |
112 ms |
24700 KB |
Output is correct |
29 |
Correct |
104 ms |
24768 KB |
Output is correct |
30 |
Correct |
109 ms |
24680 KB |
Output is correct |
31 |
Correct |
115 ms |
24660 KB |
Output is correct |
32 |
Correct |
110 ms |
24812 KB |
Output is correct |
33 |
Correct |
126 ms |
24768 KB |
Output is correct |
34 |
Correct |
98 ms |
24640 KB |
Output is correct |
35 |
Correct |
110 ms |
24640 KB |
Output is correct |
36 |
Correct |
105 ms |
24564 KB |
Output is correct |
37 |
Correct |
105 ms |
24744 KB |
Output is correct |
38 |
Correct |
125 ms |
24756 KB |
Output is correct |
39 |
Correct |
160 ms |
25508 KB |
Output is correct |
40 |
Correct |
169 ms |
25464 KB |
Output is correct |
41 |
Correct |
149 ms |
25532 KB |
Output is correct |
42 |
Correct |
177 ms |
25492 KB |
Output is correct |