#include "highway.h"
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int MAX_N=90005;
int q;
int remedges;
int n,m;
vector<int>w;
vector<pair<int,int> >g[MAX_N],G[MAX_N];
long long edges,cost;
int root;
int a,b;
int parent[MAX_N];
int paredge[MAX_N];
int depth[MAX_N];
vector<int>ENDS;
vector<pair<int,int>>nodes;
bool used[MAX_N];
bool usededge[130010];
void initbfs()
{
queue<int>q;
q.push(root);
used[root]=1;
while(q.size())
{
int u=q.front();
q.pop();
for(auto [v,edge]:G[u])
{
if(used[v])continue;
used[v]=1;
usededge[edge]=1;
g[u].push_back({v,edge});
g[v].push_back({u,edge});
parent[v]=u;
paredge[v]=edge;
depth[v]=depth[u]+1;
q.push(v);
}
}
}
int onpath()
{
int l=0,r=n-1;
int ans;
while(l<=r)
{
int mid=(l+r)/2;
for(int u=0;u<=mid;u++)
{
for(auto [v,edge]:G[u])
{
w[edge]=1;
}
}
long long ncost=ask(w);q++;
for(int u=0;u<=mid;u++)
{
for(auto [v,edge]:G[u])
{
w[edge]=0;
}
}
if(ncost!=cost)
{
ans=mid;
r=mid-1;
if(remedges!=edges)return ans;
}
else l=mid+1;
}
return ans;
}
void nodesdfs(int u)
{
if(remedges==edges)
{
if(depth[u]<=remedges)nodes.push_back({depth[u],u});
}
else if(depth[u]==remedges)nodes.push_back({depth[u],u});
for(auto [v,edge]:g[u])
{
if(v==parent[u])continue;
nodesdfs(v);
}
}
int rec(int l,int r)
{
if(l==r)
{
return nodes[l].second;
}
int mid=(l+r)/2;
for(int i=l;i<=mid;i++)
{
w[paredge[nodes[i].second]]=1;
}
long long ncost=ask(w);q++;
for(int i=l;i<=mid;i++)
{
w[paredge[nodes[i].second]]=0;
}
if(ncost!=cost)return rec(l,mid);
return rec(mid+1,r);
}
void solve(int branch,bool with)
{
nodes.clear();
for(int i=0;i<g[root].size();i++)
{
if(with)
{
if(i!=branch)continue;
}
else
{
if(i==branch)continue;
}
nodesdfs(g[root][i].first);
}
sort(nodes.rbegin(),nodes.rend());
ENDS.push_back(rec(0,nodes.size()-1));
}
int solvetwo()
{
int l=0,r=g[root].size()-1;
int ans=-1;
while(l<=r)
{
int mid=(l+r)/2;
for(int i=0;i<=mid;i++)
{
w[g[root][i].second]=1;
}
long long ncost=ask(w);q++;
for(int i=0;i<=mid;i++)
{
w[g[root][i].second]=0;
}
if(ncost!=cost)
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
return ans;
}
void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B)
{
n=N;
m=U.size();
w.resize(m);
a=A;
b=B;
for(int i=0;i<m;i++)
{
G[U[i]].push_back({V[i],i});
G[V[i]].push_back({U[i],i});
}
cost=ask(w);q++;
edges=cost/a;
remedges=edges;
root=onpath();
initbfs();
for(int i=0;i<m;i++)
{
if(usededge[i])continue;
w[i]=1;
}
int branch=solvetwo();
solve(branch,1);
if(depth[ENDS[0]]==edges)
{
ENDS.push_back(root);
}
else
{
remedges-=depth[ENDS[0]];
solve(branch,0);
}
//if(q>56)while(1);
answer(ENDS[0],ENDS[1]);
}
Compilation message
highway.cpp: In function 'void initbfs()':
highway.cpp:34:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | for(auto [v,edge]:G[u])
| ^
highway.cpp: In function 'int onpath()':
highway.cpp:59:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
59 | for(auto [v,edge]:G[u])
| ^
highway.cpp:67:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
67 | for(auto [v,edge]:G[u])
| ^
highway.cpp: In function 'void nodesdfs(int)':
highway.cpp:91:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
91 | for(auto [v,edge]:g[u])
| ^
highway.cpp: In function 'void solve(int, bool)':
highway.cpp:123: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]
123 | for(int i=0;i<g[root].size();i++)
| ~^~~~~~~~~~~~~~~
highway.cpp: In function 'int onpath()':
highway.cpp:52:9: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
52 | int ans;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4696 KB |
Output is correct |
2 |
Correct |
2 ms |
4476 KB |
Output is correct |
3 |
Correct |
2 ms |
4696 KB |
Output is correct |
4 |
Correct |
2 ms |
4696 KB |
Output is correct |
5 |
Correct |
2 ms |
4696 KB |
Output is correct |
6 |
Correct |
2 ms |
4696 KB |
Output is correct |
7 |
Correct |
2 ms |
4696 KB |
Output is correct |
8 |
Correct |
2 ms |
4696 KB |
Output is correct |
9 |
Correct |
2 ms |
4696 KB |
Output is correct |
10 |
Correct |
2 ms |
4696 KB |
Output is correct |
11 |
Correct |
2 ms |
4440 KB |
Output is correct |
12 |
Correct |
3 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4696 KB |
Output is correct |
2 |
Correct |
12 ms |
5720 KB |
Output is correct |
3 |
Correct |
99 ms |
14932 KB |
Output is correct |
4 |
Correct |
110 ms |
15388 KB |
Output is correct |
5 |
Correct |
99 ms |
14876 KB |
Output is correct |
6 |
Correct |
97 ms |
15392 KB |
Output is correct |
7 |
Correct |
89 ms |
14652 KB |
Output is correct |
8 |
Correct |
94 ms |
14964 KB |
Output is correct |
9 |
Correct |
89 ms |
14316 KB |
Output is correct |
10 |
Correct |
95 ms |
14876 KB |
Output is correct |
11 |
Correct |
91 ms |
14592 KB |
Output is correct |
12 |
Correct |
93 ms |
15736 KB |
Output is correct |
13 |
Correct |
102 ms |
15328 KB |
Output is correct |
14 |
Correct |
92 ms |
14664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
5976 KB |
Output is correct |
2 |
Correct |
17 ms |
7512 KB |
Output is correct |
3 |
Correct |
25 ms |
7560 KB |
Output is correct |
4 |
Correct |
81 ms |
16924 KB |
Output is correct |
5 |
Correct |
70 ms |
16644 KB |
Output is correct |
6 |
Correct |
73 ms |
18312 KB |
Output is correct |
7 |
Correct |
70 ms |
13580 KB |
Output is correct |
8 |
Correct |
72 ms |
17548 KB |
Output is correct |
9 |
Correct |
87 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4696 KB |
Output is correct |
2 |
Correct |
11 ms |
5720 KB |
Output is correct |
3 |
Correct |
74 ms |
12144 KB |
Output is correct |
4 |
Correct |
91 ms |
14224 KB |
Output is correct |
5 |
Correct |
103 ms |
14196 KB |
Output is correct |
6 |
Correct |
104 ms |
14464 KB |
Output is correct |
7 |
Correct |
86 ms |
14312 KB |
Output is correct |
8 |
Correct |
88 ms |
14160 KB |
Output is correct |
9 |
Correct |
103 ms |
15504 KB |
Output is correct |
10 |
Correct |
97 ms |
14976 KB |
Output is correct |
11 |
Correct |
91 ms |
14340 KB |
Output is correct |
12 |
Correct |
100 ms |
15528 KB |
Output is correct |
13 |
Correct |
86 ms |
14636 KB |
Output is correct |
14 |
Correct |
94 ms |
15372 KB |
Output is correct |
15 |
Correct |
87 ms |
14288 KB |
Output is correct |
16 |
Correct |
93 ms |
14208 KB |
Output is correct |
17 |
Correct |
95 ms |
14812 KB |
Output is correct |
18 |
Correct |
95 ms |
15456 KB |
Output is correct |
19 |
Correct |
90 ms |
14380 KB |
Output is correct |
20 |
Correct |
93 ms |
15348 KB |
Output is correct |
21 |
Correct |
103 ms |
16388 KB |
Output is correct |
22 |
Correct |
112 ms |
16576 KB |
Output is correct |
23 |
Correct |
88 ms |
15604 KB |
Output is correct |
24 |
Correct |
102 ms |
15680 KB |
Output is correct |
25 |
Correct |
97 ms |
18400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
6232 KB |
Output is correct |
2 |
Correct |
13 ms |
5824 KB |
Output is correct |
3 |
Correct |
110 ms |
15740 KB |
Output is correct |
4 |
Correct |
117 ms |
15700 KB |
Output is correct |
5 |
Correct |
146 ms |
17004 KB |
Output is correct |
6 |
Correct |
160 ms |
16428 KB |
Output is correct |
7 |
Correct |
151 ms |
16908 KB |
Output is correct |
8 |
Correct |
141 ms |
16060 KB |
Output is correct |
9 |
Correct |
110 ms |
11824 KB |
Output is correct |
10 |
Correct |
106 ms |
12036 KB |
Output is correct |
11 |
Correct |
115 ms |
13752 KB |
Output is correct |
12 |
Correct |
149 ms |
14896 KB |
Output is correct |
13 |
Correct |
131 ms |
15624 KB |
Output is correct |
14 |
Correct |
148 ms |
16476 KB |
Output is correct |
15 |
Correct |
138 ms |
15988 KB |
Output is correct |
16 |
Correct |
118 ms |
13084 KB |
Output is correct |
17 |
Correct |
96 ms |
16252 KB |
Output is correct |
18 |
Correct |
100 ms |
16336 KB |
Output is correct |
19 |
Correct |
102 ms |
16344 KB |
Output is correct |
20 |
Incorrect |
103 ms |
16576 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
5720 KB |
Output is correct |
2 |
Correct |
14 ms |
5976 KB |
Output is correct |
3 |
Correct |
103 ms |
14980 KB |
Output is correct |
4 |
Correct |
113 ms |
14924 KB |
Output is correct |
5 |
Correct |
133 ms |
15704 KB |
Output is correct |
6 |
Correct |
135 ms |
16244 KB |
Output is correct |
7 |
Correct |
118 ms |
15756 KB |
Output is correct |
8 |
Correct |
104 ms |
15112 KB |
Output is correct |
9 |
Correct |
111 ms |
15312 KB |
Output is correct |
10 |
Correct |
138 ms |
16204 KB |
Output is correct |
11 |
Correct |
143 ms |
16460 KB |
Output is correct |
12 |
Correct |
138 ms |
16164 KB |
Output is correct |
13 |
Correct |
114 ms |
13000 KB |
Output is correct |
14 |
Correct |
108 ms |
12024 KB |
Output is correct |
15 |
Correct |
106 ms |
12956 KB |
Output is correct |
16 |
Correct |
110 ms |
12100 KB |
Output is correct |
17 |
Correct |
114 ms |
13532 KB |
Output is correct |
18 |
Correct |
110 ms |
12352 KB |
Output is correct |
19 |
Correct |
152 ms |
15040 KB |
Output is correct |
20 |
Correct |
134 ms |
15388 KB |
Output is correct |
21 |
Correct |
151 ms |
16040 KB |
Output is correct |
22 |
Correct |
154 ms |
15976 KB |
Output is correct |
23 |
Correct |
156 ms |
15952 KB |
Output is correct |
24 |
Correct |
150 ms |
16008 KB |
Output is correct |
25 |
Correct |
153 ms |
15932 KB |
Output is correct |
26 |
Correct |
145 ms |
16288 KB |
Output is correct |
27 |
Correct |
93 ms |
16424 KB |
Output is correct |
28 |
Correct |
102 ms |
16328 KB |
Output is correct |
29 |
Correct |
102 ms |
16548 KB |
Output is correct |
30 |
Correct |
103 ms |
16432 KB |
Output is correct |
31 |
Correct |
93 ms |
16340 KB |
Output is correct |
32 |
Incorrect |
95 ms |
16312 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |