#include "swap.h"
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
const int mxn=3e5+100,lg=30;
int pa[mxn+10],deg[mxn+10],val[mxn+10],up[mxn+10][lg+5],h[mxn+10];
bool yes[mxn+10];
vector<int>adj[mxn+10];
bitset<mxn+10>vis;
int find(int u){return ((pa[u]==u)?u:pa[u]=find(pa[u]));}
int cnt=0;
void merg(int a,int b){
int u=find(a),v=find(b);
cnt++;
adj[cnt].pb(v);
if(u==v){
yes[cnt]=true;
pa[v]=cnt;
return;
}
adj[cnt].pb(u);
yes[cnt]=(yes[v]||yes[u]);
deg[a]++,deg[b]++;
if(deg[a]>=3||deg[b]>=3)yes[cnt]=true;
pa[u]=pa[v]=cnt;
}
void dfs(int cur){
if(vis[cur])return;
vis[cur]=true;
for(auto i:adj[cur])h[i]=h[cur]+1,up[i][0]=cur,dfs(i);
}
void init(int n,int m,vector<int>u,vector<int>v,vector<int>w){
cnt=n-1;
for(int i=0;i<mxn;i++)pa[i]=i;
vector<ppii>e;
for(int i=0;i<m;i++)e.pb({w[i],{u[i],v[i]}});
sort(all(e));
for(auto i:e)merg(i.s.f,i.s.s),val[cnt]=i.f;
for(int i=0;i<=lg;i++)up[cnt][i]=cnt;
dfs(cnt);
for(int j=1;j<=lg;j++)for(int i=0;i<=cnt;i++)up[i][j]=up[up[i][j-1]][j-1];
}
int lca(int a,int b){
if(h[a]<h[b])swap(a,b);
int k=h[a]-h[b];
for(int i=0;i<=lg;i++)if(k&(1<<i))a=up[a][i];
if(a==b)return a;
for(int i=lg;i>=0;i--)if(up[a][i]!=up[b][i])a=up[a][i],b=up[b][i];
return up[a][0];
}
int getMinimumFuelCapacity(int a,int b){
int node=lca(a,b);
for(int i=lg;i>=0;i--)if(!yes[up[node][i]])node=up[node][i];
if(!yes[node])node=up[node][0];
if(yes[node])return val[node];
else return -1;
}/*
int main() {
int N, M;
assert(2 == scanf("%d %d", &N, &M));
std::vector<int> U(M), V(M), W(M);
for (int i = 0; i < M; ++i) {
assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
}
int Q;
assert(1 == scanf("%d", &Q));
std::vector<int> X(Q), Y(Q);
for (int i = 0; i < Q; ++i) {
assert(2 == scanf("%d %d", &X[i], &Y[i]));
}
init(N, M, U, V, W);
std::vector<int> minimum_fuel_capacities(Q);
for (int i = 0; i < Q; ++i) {
minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
}
for (int i = 0; i < Q; ++i) {
printf("%d\n", minimum_fuel_capacities[i]);
}
return 0;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13916 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
3 ms |
13912 KB |
Output is correct |
5 |
Correct |
3 ms |
13916 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
4 ms |
13916 KB |
Output is correct |
8 |
Correct |
4 ms |
13916 KB |
Output is correct |
9 |
Correct |
79 ms |
39776 KB |
Output is correct |
10 |
Correct |
100 ms |
45000 KB |
Output is correct |
11 |
Correct |
93 ms |
44996 KB |
Output is correct |
12 |
Correct |
106 ms |
47160 KB |
Output is correct |
13 |
Correct |
96 ms |
50372 KB |
Output is correct |
14 |
Correct |
92 ms |
39876 KB |
Output is correct |
15 |
Correct |
232 ms |
48584 KB |
Output is correct |
16 |
Correct |
233 ms |
46376 KB |
Output is correct |
17 |
Correct |
268 ms |
48744 KB |
Output is correct |
18 |
Correct |
298 ms |
52084 KB |
Output is correct |
19 |
Correct |
90 ms |
22516 KB |
Output is correct |
20 |
Correct |
238 ms |
48840 KB |
Output is correct |
21 |
Correct |
244 ms |
46996 KB |
Output is correct |
22 |
Correct |
253 ms |
49336 KB |
Output is correct |
23 |
Correct |
308 ms |
52628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13916 KB |
Output is correct |
3 |
Correct |
324 ms |
52784 KB |
Output is correct |
4 |
Correct |
277 ms |
55424 KB |
Output is correct |
5 |
Correct |
352 ms |
53016 KB |
Output is correct |
6 |
Correct |
287 ms |
55196 KB |
Output is correct |
7 |
Correct |
299 ms |
55172 KB |
Output is correct |
8 |
Correct |
287 ms |
52832 KB |
Output is correct |
9 |
Correct |
302 ms |
54980 KB |
Output is correct |
10 |
Correct |
266 ms |
52600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13916 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
3 ms |
13912 KB |
Output is correct |
5 |
Correct |
3 ms |
13916 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
4 ms |
13916 KB |
Output is correct |
8 |
Correct |
4 ms |
13916 KB |
Output is correct |
9 |
Correct |
3 ms |
13912 KB |
Output is correct |
10 |
Correct |
4 ms |
13916 KB |
Output is correct |
11 |
Correct |
3 ms |
13912 KB |
Output is correct |
12 |
Correct |
3 ms |
13916 KB |
Output is correct |
13 |
Correct |
3 ms |
13916 KB |
Output is correct |
14 |
Correct |
3 ms |
13916 KB |
Output is correct |
15 |
Correct |
4 ms |
13916 KB |
Output is correct |
16 |
Correct |
3 ms |
13916 KB |
Output is correct |
17 |
Correct |
3 ms |
14156 KB |
Output is correct |
18 |
Correct |
4 ms |
14168 KB |
Output is correct |
19 |
Correct |
4 ms |
13912 KB |
Output is correct |
20 |
Correct |
3 ms |
13916 KB |
Output is correct |
21 |
Correct |
3 ms |
13916 KB |
Output is correct |
22 |
Correct |
4 ms |
14172 KB |
Output is correct |
23 |
Correct |
3 ms |
13916 KB |
Output is correct |
24 |
Correct |
4 ms |
14172 KB |
Output is correct |
25 |
Correct |
4 ms |
14168 KB |
Output is correct |
26 |
Correct |
4 ms |
14172 KB |
Output is correct |
27 |
Correct |
4 ms |
14172 KB |
Output is correct |
28 |
Correct |
4 ms |
14172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
3 ms |
13912 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13916 KB |
Output is correct |
8 |
Correct |
4 ms |
13916 KB |
Output is correct |
9 |
Correct |
4 ms |
13916 KB |
Output is correct |
10 |
Correct |
79 ms |
39776 KB |
Output is correct |
11 |
Correct |
100 ms |
45000 KB |
Output is correct |
12 |
Correct |
93 ms |
44996 KB |
Output is correct |
13 |
Correct |
106 ms |
47160 KB |
Output is correct |
14 |
Correct |
96 ms |
50372 KB |
Output is correct |
15 |
Correct |
4 ms |
13916 KB |
Output is correct |
16 |
Correct |
3 ms |
13912 KB |
Output is correct |
17 |
Correct |
3 ms |
13916 KB |
Output is correct |
18 |
Correct |
3 ms |
13916 KB |
Output is correct |
19 |
Correct |
3 ms |
13916 KB |
Output is correct |
20 |
Correct |
4 ms |
13916 KB |
Output is correct |
21 |
Correct |
3 ms |
13916 KB |
Output is correct |
22 |
Correct |
3 ms |
14156 KB |
Output is correct |
23 |
Correct |
4 ms |
14168 KB |
Output is correct |
24 |
Correct |
4 ms |
13912 KB |
Output is correct |
25 |
Correct |
3 ms |
13916 KB |
Output is correct |
26 |
Correct |
3 ms |
13916 KB |
Output is correct |
27 |
Correct |
4 ms |
14172 KB |
Output is correct |
28 |
Correct |
3 ms |
13916 KB |
Output is correct |
29 |
Correct |
4 ms |
14172 KB |
Output is correct |
30 |
Correct |
4 ms |
14168 KB |
Output is correct |
31 |
Correct |
4 ms |
14172 KB |
Output is correct |
32 |
Correct |
4 ms |
14172 KB |
Output is correct |
33 |
Correct |
4 ms |
14172 KB |
Output is correct |
34 |
Correct |
12 ms |
16920 KB |
Output is correct |
35 |
Correct |
113 ms |
47188 KB |
Output is correct |
36 |
Correct |
103 ms |
47204 KB |
Output is correct |
37 |
Correct |
103 ms |
47304 KB |
Output is correct |
38 |
Correct |
97 ms |
47196 KB |
Output is correct |
39 |
Correct |
100 ms |
47056 KB |
Output is correct |
40 |
Correct |
94 ms |
44484 KB |
Output is correct |
41 |
Correct |
106 ms |
47300 KB |
Output is correct |
42 |
Correct |
104 ms |
47304 KB |
Output is correct |
43 |
Correct |
99 ms |
51400 KB |
Output is correct |
44 |
Correct |
106 ms |
47300 KB |
Output is correct |
45 |
Correct |
138 ms |
60348 KB |
Output is correct |
46 |
Correct |
107 ms |
47300 KB |
Output is correct |
47 |
Correct |
110 ms |
47556 KB |
Output is correct |
48 |
Correct |
114 ms |
50404 KB |
Output is correct |
49 |
Correct |
103 ms |
54828 KB |
Output is correct |
50 |
Correct |
79 ms |
45508 KB |
Output is correct |
51 |
Correct |
121 ms |
52420 KB |
Output is correct |
52 |
Correct |
149 ms |
63164 KB |
Output is correct |
53 |
Correct |
164 ms |
66300 KB |
Output is correct |
54 |
Correct |
193 ms |
72660 KB |
Output is correct |
55 |
Correct |
97 ms |
52936 KB |
Output is correct |
56 |
Correct |
171 ms |
72260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13916 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
3 ms |
13912 KB |
Output is correct |
5 |
Correct |
3 ms |
13916 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
4 ms |
13916 KB |
Output is correct |
8 |
Correct |
4 ms |
13916 KB |
Output is correct |
9 |
Correct |
79 ms |
39776 KB |
Output is correct |
10 |
Correct |
100 ms |
45000 KB |
Output is correct |
11 |
Correct |
93 ms |
44996 KB |
Output is correct |
12 |
Correct |
106 ms |
47160 KB |
Output is correct |
13 |
Correct |
96 ms |
50372 KB |
Output is correct |
14 |
Correct |
92 ms |
39876 KB |
Output is correct |
15 |
Correct |
232 ms |
48584 KB |
Output is correct |
16 |
Correct |
233 ms |
46376 KB |
Output is correct |
17 |
Correct |
268 ms |
48744 KB |
Output is correct |
18 |
Correct |
298 ms |
52084 KB |
Output is correct |
19 |
Correct |
324 ms |
52784 KB |
Output is correct |
20 |
Correct |
277 ms |
55424 KB |
Output is correct |
21 |
Correct |
352 ms |
53016 KB |
Output is correct |
22 |
Correct |
287 ms |
55196 KB |
Output is correct |
23 |
Correct |
299 ms |
55172 KB |
Output is correct |
24 |
Correct |
287 ms |
52832 KB |
Output is correct |
25 |
Correct |
302 ms |
54980 KB |
Output is correct |
26 |
Correct |
266 ms |
52600 KB |
Output is correct |
27 |
Correct |
4 ms |
13916 KB |
Output is correct |
28 |
Correct |
3 ms |
13912 KB |
Output is correct |
29 |
Correct |
3 ms |
13916 KB |
Output is correct |
30 |
Correct |
3 ms |
13916 KB |
Output is correct |
31 |
Correct |
3 ms |
13916 KB |
Output is correct |
32 |
Correct |
4 ms |
13916 KB |
Output is correct |
33 |
Correct |
3 ms |
13916 KB |
Output is correct |
34 |
Correct |
3 ms |
14156 KB |
Output is correct |
35 |
Correct |
4 ms |
14168 KB |
Output is correct |
36 |
Correct |
12 ms |
16920 KB |
Output is correct |
37 |
Correct |
113 ms |
47188 KB |
Output is correct |
38 |
Correct |
103 ms |
47204 KB |
Output is correct |
39 |
Correct |
103 ms |
47304 KB |
Output is correct |
40 |
Correct |
97 ms |
47196 KB |
Output is correct |
41 |
Correct |
100 ms |
47056 KB |
Output is correct |
42 |
Correct |
94 ms |
44484 KB |
Output is correct |
43 |
Correct |
106 ms |
47300 KB |
Output is correct |
44 |
Correct |
104 ms |
47304 KB |
Output is correct |
45 |
Correct |
99 ms |
51400 KB |
Output is correct |
46 |
Correct |
106 ms |
47300 KB |
Output is correct |
47 |
Correct |
21 ms |
17028 KB |
Output is correct |
48 |
Correct |
237 ms |
48840 KB |
Output is correct |
49 |
Correct |
229 ms |
48780 KB |
Output is correct |
50 |
Correct |
226 ms |
48836 KB |
Output is correct |
51 |
Correct |
225 ms |
48956 KB |
Output is correct |
52 |
Correct |
219 ms |
46476 KB |
Output is correct |
53 |
Correct |
183 ms |
37584 KB |
Output is correct |
54 |
Correct |
228 ms |
49212 KB |
Output is correct |
55 |
Correct |
256 ms |
48760 KB |
Output is correct |
56 |
Correct |
342 ms |
52160 KB |
Output is correct |
57 |
Correct |
260 ms |
49444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
13912 KB |
Output is correct |
3 |
Correct |
3 ms |
13916 KB |
Output is correct |
4 |
Correct |
3 ms |
13916 KB |
Output is correct |
5 |
Correct |
3 ms |
13912 KB |
Output is correct |
6 |
Correct |
3 ms |
13916 KB |
Output is correct |
7 |
Correct |
3 ms |
13916 KB |
Output is correct |
8 |
Correct |
4 ms |
13916 KB |
Output is correct |
9 |
Correct |
4 ms |
13916 KB |
Output is correct |
10 |
Correct |
79 ms |
39776 KB |
Output is correct |
11 |
Correct |
100 ms |
45000 KB |
Output is correct |
12 |
Correct |
93 ms |
44996 KB |
Output is correct |
13 |
Correct |
106 ms |
47160 KB |
Output is correct |
14 |
Correct |
96 ms |
50372 KB |
Output is correct |
15 |
Correct |
92 ms |
39876 KB |
Output is correct |
16 |
Correct |
232 ms |
48584 KB |
Output is correct |
17 |
Correct |
233 ms |
46376 KB |
Output is correct |
18 |
Correct |
268 ms |
48744 KB |
Output is correct |
19 |
Correct |
298 ms |
52084 KB |
Output is correct |
20 |
Correct |
324 ms |
52784 KB |
Output is correct |
21 |
Correct |
277 ms |
55424 KB |
Output is correct |
22 |
Correct |
352 ms |
53016 KB |
Output is correct |
23 |
Correct |
287 ms |
55196 KB |
Output is correct |
24 |
Correct |
299 ms |
55172 KB |
Output is correct |
25 |
Correct |
287 ms |
52832 KB |
Output is correct |
26 |
Correct |
302 ms |
54980 KB |
Output is correct |
27 |
Correct |
266 ms |
52600 KB |
Output is correct |
28 |
Correct |
4 ms |
13916 KB |
Output is correct |
29 |
Correct |
3 ms |
13912 KB |
Output is correct |
30 |
Correct |
3 ms |
13916 KB |
Output is correct |
31 |
Correct |
3 ms |
13916 KB |
Output is correct |
32 |
Correct |
3 ms |
13916 KB |
Output is correct |
33 |
Correct |
4 ms |
13916 KB |
Output is correct |
34 |
Correct |
3 ms |
13916 KB |
Output is correct |
35 |
Correct |
3 ms |
14156 KB |
Output is correct |
36 |
Correct |
4 ms |
14168 KB |
Output is correct |
37 |
Correct |
12 ms |
16920 KB |
Output is correct |
38 |
Correct |
113 ms |
47188 KB |
Output is correct |
39 |
Correct |
103 ms |
47204 KB |
Output is correct |
40 |
Correct |
103 ms |
47304 KB |
Output is correct |
41 |
Correct |
97 ms |
47196 KB |
Output is correct |
42 |
Correct |
100 ms |
47056 KB |
Output is correct |
43 |
Correct |
94 ms |
44484 KB |
Output is correct |
44 |
Correct |
106 ms |
47300 KB |
Output is correct |
45 |
Correct |
104 ms |
47304 KB |
Output is correct |
46 |
Correct |
99 ms |
51400 KB |
Output is correct |
47 |
Correct |
106 ms |
47300 KB |
Output is correct |
48 |
Correct |
21 ms |
17028 KB |
Output is correct |
49 |
Correct |
237 ms |
48840 KB |
Output is correct |
50 |
Correct |
229 ms |
48780 KB |
Output is correct |
51 |
Correct |
226 ms |
48836 KB |
Output is correct |
52 |
Correct |
225 ms |
48956 KB |
Output is correct |
53 |
Correct |
219 ms |
46476 KB |
Output is correct |
54 |
Correct |
183 ms |
37584 KB |
Output is correct |
55 |
Correct |
228 ms |
49212 KB |
Output is correct |
56 |
Correct |
256 ms |
48760 KB |
Output is correct |
57 |
Correct |
342 ms |
52160 KB |
Output is correct |
58 |
Correct |
260 ms |
49444 KB |
Output is correct |
59 |
Correct |
90 ms |
22516 KB |
Output is correct |
60 |
Correct |
238 ms |
48840 KB |
Output is correct |
61 |
Correct |
244 ms |
46996 KB |
Output is correct |
62 |
Correct |
253 ms |
49336 KB |
Output is correct |
63 |
Correct |
308 ms |
52628 KB |
Output is correct |
64 |
Correct |
4 ms |
13912 KB |
Output is correct |
65 |
Correct |
3 ms |
13916 KB |
Output is correct |
66 |
Correct |
3 ms |
13916 KB |
Output is correct |
67 |
Correct |
4 ms |
14172 KB |
Output is correct |
68 |
Correct |
3 ms |
13916 KB |
Output is correct |
69 |
Correct |
4 ms |
14172 KB |
Output is correct |
70 |
Correct |
4 ms |
14168 KB |
Output is correct |
71 |
Correct |
4 ms |
14172 KB |
Output is correct |
72 |
Correct |
4 ms |
14172 KB |
Output is correct |
73 |
Correct |
4 ms |
14172 KB |
Output is correct |
74 |
Correct |
138 ms |
60348 KB |
Output is correct |
75 |
Correct |
107 ms |
47300 KB |
Output is correct |
76 |
Correct |
110 ms |
47556 KB |
Output is correct |
77 |
Correct |
114 ms |
50404 KB |
Output is correct |
78 |
Correct |
103 ms |
54828 KB |
Output is correct |
79 |
Correct |
79 ms |
45508 KB |
Output is correct |
80 |
Correct |
121 ms |
52420 KB |
Output is correct |
81 |
Correct |
149 ms |
63164 KB |
Output is correct |
82 |
Correct |
164 ms |
66300 KB |
Output is correct |
83 |
Correct |
193 ms |
72660 KB |
Output is correct |
84 |
Correct |
97 ms |
52936 KB |
Output is correct |
85 |
Correct |
171 ms |
72260 KB |
Output is correct |
86 |
Correct |
73 ms |
31176 KB |
Output is correct |
87 |
Correct |
232 ms |
52936 KB |
Output is correct |
88 |
Correct |
266 ms |
53064 KB |
Output is correct |
89 |
Correct |
280 ms |
56008 KB |
Output is correct |
90 |
Correct |
213 ms |
65476 KB |
Output is correct |
91 |
Correct |
215 ms |
60356 KB |
Output is correct |
92 |
Correct |
280 ms |
58000 KB |
Output is correct |
93 |
Correct |
307 ms |
70148 KB |
Output is correct |
94 |
Correct |
370 ms |
75088 KB |
Output is correct |
95 |
Correct |
316 ms |
80580 KB |
Output is correct |
96 |
Correct |
354 ms |
56448 KB |
Output is correct |
97 |
Correct |
324 ms |
64444 KB |
Output is correct |