#include "highway.h"
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int MAX_N=90005;
int q;
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;
}
else l=mid+1;
}
return ans;
}
void nodesdfs(int u)
{
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;
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
{
solve(branch,0);
}
if(q>54)while(1);
answer(ENDS[0],ENDS[1]);
}
Compilation message
highway.cpp: In function 'void initbfs()':
highway.cpp:33:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
33 | for(auto [v,edge]:G[u])
| ^
highway.cpp: In function 'int onpath()':
highway.cpp:58:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | for(auto [v,edge]:G[u])
| ^
highway.cpp:66:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
66 | for(auto [v,edge]:G[u])
| ^
highway.cpp: In function 'void nodesdfs(int)':
highway.cpp:84:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
84 | for(auto [v,edge]:g[u])
| ^
highway.cpp: In function 'void solve(int, bool)':
highway.cpp:116: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]
116 | for(int i=0;i<g[root].size();i++)
| ~^~~~~~~~~~~~~~~
highway.cpp: In function 'int onpath()':
highway.cpp:79:12: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
79 | return ans;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4696 KB |
Output is correct |
2 |
Correct |
2 ms |
4696 KB |
Output is correct |
3 |
Correct |
2 ms |
4696 KB |
Output is correct |
4 |
Correct |
2 ms |
4696 KB |
Output is correct |
5 |
Correct |
3 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 |
4696 KB |
Output is correct |
12 |
Correct |
2 ms |
4696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4696 KB |
Output is correct |
2 |
Correct |
11 ms |
5884 KB |
Output is correct |
3 |
Correct |
96 ms |
15476 KB |
Output is correct |
4 |
Correct |
115 ms |
15356 KB |
Output is correct |
5 |
Correct |
108 ms |
15536 KB |
Output is correct |
6 |
Correct |
99 ms |
15384 KB |
Output is correct |
7 |
Correct |
97 ms |
15384 KB |
Output is correct |
8 |
Correct |
105 ms |
15472 KB |
Output is correct |
9 |
Correct |
92 ms |
15372 KB |
Output is correct |
10 |
Correct |
110 ms |
15372 KB |
Output is correct |
11 |
Correct |
117 ms |
15272 KB |
Output is correct |
12 |
Correct |
102 ms |
15952 KB |
Output is correct |
13 |
Correct |
89 ms |
15744 KB |
Output is correct |
14 |
Correct |
107 ms |
14716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
6232 KB |
Output is correct |
2 |
Correct |
17 ms |
7636 KB |
Output is correct |
3 |
Correct |
24 ms |
7700 KB |
Output is correct |
4 |
Correct |
73 ms |
17276 KB |
Output is correct |
5 |
Correct |
72 ms |
17276 KB |
Output is correct |
6 |
Correct |
72 ms |
18488 KB |
Output is correct |
7 |
Correct |
71 ms |
13484 KB |
Output is correct |
8 |
Correct |
76 ms |
17880 KB |
Output is correct |
9 |
Correct |
74 ms |
15256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4696 KB |
Output is correct |
2 |
Correct |
11 ms |
5876 KB |
Output is correct |
3 |
Correct |
90 ms |
13360 KB |
Output is correct |
4 |
Correct |
115 ms |
15544 KB |
Output is correct |
5 |
Correct |
97 ms |
15472 KB |
Output is correct |
6 |
Correct |
125 ms |
15468 KB |
Output is correct |
7 |
Correct |
96 ms |
15312 KB |
Output is correct |
8 |
Correct |
93 ms |
15308 KB |
Output is correct |
9 |
Correct |
102 ms |
15536 KB |
Output is correct |
10 |
Correct |
99 ms |
15496 KB |
Output is correct |
11 |
Correct |
113 ms |
15208 KB |
Output is correct |
12 |
Correct |
108 ms |
15440 KB |
Output is correct |
13 |
Correct |
106 ms |
15092 KB |
Output is correct |
14 |
Correct |
102 ms |
16084 KB |
Output is correct |
15 |
Correct |
98 ms |
15472 KB |
Output is correct |
16 |
Correct |
114 ms |
15324 KB |
Output is correct |
17 |
Correct |
103 ms |
14852 KB |
Output is correct |
18 |
Correct |
110 ms |
16020 KB |
Output is correct |
19 |
Correct |
90 ms |
15364 KB |
Output is correct |
20 |
Correct |
103 ms |
16524 KB |
Output is correct |
21 |
Correct |
94 ms |
16492 KB |
Output is correct |
22 |
Correct |
104 ms |
16408 KB |
Output is correct |
23 |
Correct |
109 ms |
15600 KB |
Output is correct |
24 |
Correct |
97 ms |
15752 KB |
Output is correct |
25 |
Correct |
102 ms |
18948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5968 KB |
Output is correct |
2 |
Correct |
12 ms |
5976 KB |
Output is correct |
3 |
Correct |
123 ms |
15856 KB |
Output is correct |
4 |
Correct |
145 ms |
15656 KB |
Output is correct |
5 |
Correct |
160 ms |
16916 KB |
Output is correct |
6 |
Correct |
168 ms |
16588 KB |
Output is correct |
7 |
Correct |
172 ms |
16832 KB |
Output is correct |
8 |
Correct |
152 ms |
16900 KB |
Output is correct |
9 |
Correct |
106 ms |
11836 KB |
Output is correct |
10 |
Correct |
108 ms |
12068 KB |
Output is correct |
11 |
Correct |
121 ms |
13720 KB |
Output is correct |
12 |
Correct |
157 ms |
15292 KB |
Output is correct |
13 |
Correct |
150 ms |
16408 KB |
Output is correct |
14 |
Correct |
159 ms |
16224 KB |
Output is correct |
15 |
Correct |
151 ms |
16808 KB |
Output is correct |
16 |
Correct |
126 ms |
13448 KB |
Output is correct |
17 |
Correct |
91 ms |
16212 KB |
Output is correct |
18 |
Correct |
93 ms |
16320 KB |
Output is correct |
19 |
Correct |
100 ms |
16264 KB |
Output is correct |
20 |
Execution timed out |
3012 ms |
16512 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5976 KB |
Output is correct |
2 |
Correct |
14 ms |
5968 KB |
Output is correct |
3 |
Partially correct |
129 ms |
15408 KB |
Output is partially correct |
4 |
Correct |
124 ms |
15980 KB |
Output is correct |
5 |
Partially correct |
134 ms |
15668 KB |
Output is partially correct |
6 |
Partially correct |
153 ms |
16476 KB |
Output is partially correct |
7 |
Correct |
123 ms |
15772 KB |
Output is correct |
8 |
Correct |
105 ms |
15908 KB |
Output is correct |
9 |
Correct |
123 ms |
16024 KB |
Output is correct |
10 |
Partially correct |
150 ms |
16356 KB |
Output is partially correct |
11 |
Correct |
177 ms |
16780 KB |
Output is correct |
12 |
Correct |
139 ms |
16468 KB |
Output is correct |
13 |
Correct |
110 ms |
13248 KB |
Output is correct |
14 |
Correct |
101 ms |
12016 KB |
Output is correct |
15 |
Correct |
137 ms |
13708 KB |
Output is correct |
16 |
Correct |
103 ms |
12012 KB |
Output is correct |
17 |
Correct |
120 ms |
13576 KB |
Output is correct |
18 |
Correct |
116 ms |
12548 KB |
Output is correct |
19 |
Correct |
180 ms |
15152 KB |
Output is correct |
20 |
Correct |
148 ms |
15768 KB |
Output is correct |
21 |
Partially correct |
152 ms |
16212 KB |
Output is partially correct |
22 |
Correct |
160 ms |
16236 KB |
Output is correct |
23 |
Correct |
150 ms |
16672 KB |
Output is correct |
24 |
Correct |
157 ms |
16296 KB |
Output is correct |
25 |
Correct |
157 ms |
16736 KB |
Output is correct |
26 |
Correct |
154 ms |
16836 KB |
Output is correct |
27 |
Correct |
97 ms |
16332 KB |
Output is correct |
28 |
Correct |
111 ms |
16320 KB |
Output is correct |
29 |
Correct |
104 ms |
16584 KB |
Output is correct |
30 |
Correct |
116 ms |
16676 KB |
Output is correct |
31 |
Correct |
104 ms |
16276 KB |
Output is correct |
32 |
Execution timed out |
3093 ms |
16336 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |