#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,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++;
if(u==v){
yes[cnt]=true;
adj[cnt].pb(v);
pa[v]=cnt;
return;
}
adj[cnt].pb(v);
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<=(n+m+1);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 getMinimumFuelCapacity(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&&yes[a])return val[a];
for(int i=lg;i>=0;i--)if((up[a][i]!=up[b][i])||(up[a][i]==up[b][i]&&!yes[up[a][i]]))a=up[a][i],b=up[b][i];
if((up[a][0]==up[b][0])&&(yes[up[a][0]]))return val[up[a][0]];
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;
}*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12916 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
89 ms |
39204 KB |
Output is correct |
10 |
Correct |
116 ms |
44560 KB |
Output is correct |
11 |
Correct |
97 ms |
44484 KB |
Output is correct |
12 |
Correct |
106 ms |
46796 KB |
Output is correct |
13 |
Correct |
104 ms |
50028 KB |
Output is correct |
14 |
Correct |
102 ms |
39276 KB |
Output is correct |
15 |
Correct |
200 ms |
46224 KB |
Output is correct |
16 |
Correct |
187 ms |
45896 KB |
Output is correct |
17 |
Correct |
205 ms |
48508 KB |
Output is correct |
18 |
Correct |
244 ms |
51596 KB |
Output is correct |
19 |
Correct |
68 ms |
21248 KB |
Output is correct |
20 |
Correct |
188 ms |
48644 KB |
Output is correct |
21 |
Correct |
192 ms |
46468 KB |
Output is correct |
22 |
Correct |
212 ms |
49160 KB |
Output is correct |
23 |
Correct |
280 ms |
51948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
269 ms |
52332 KB |
Output is correct |
4 |
Correct |
241 ms |
54832 KB |
Output is correct |
5 |
Correct |
289 ms |
52684 KB |
Output is correct |
6 |
Correct |
231 ms |
54676 KB |
Output is correct |
7 |
Correct |
256 ms |
54776 KB |
Output is correct |
8 |
Correct |
230 ms |
52252 KB |
Output is correct |
9 |
Correct |
277 ms |
54724 KB |
Output is correct |
10 |
Correct |
231 ms |
52196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12916 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12892 KB |
Output is correct |
12 |
Correct |
5 ms |
12892 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12892 KB |
Output is correct |
16 |
Correct |
4 ms |
12892 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
13044 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12888 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
2 ms |
12892 KB |
Output is correct |
24 |
Correct |
4 ms |
12892 KB |
Output is correct |
25 |
Correct |
3 ms |
12892 KB |
Output is correct |
26 |
Correct |
3 ms |
12888 KB |
Output is correct |
27 |
Correct |
3 ms |
12892 KB |
Output is correct |
28 |
Correct |
3 ms |
12892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12888 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12916 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
89 ms |
39204 KB |
Output is correct |
11 |
Correct |
116 ms |
44560 KB |
Output is correct |
12 |
Correct |
97 ms |
44484 KB |
Output is correct |
13 |
Correct |
106 ms |
46796 KB |
Output is correct |
14 |
Correct |
104 ms |
50028 KB |
Output is correct |
15 |
Correct |
3 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
5 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12892 KB |
Output is correct |
19 |
Correct |
2 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
4 ms |
12892 KB |
Output is correct |
22 |
Correct |
2 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
13044 KB |
Output is correct |
24 |
Correct |
3 ms |
12892 KB |
Output is correct |
25 |
Correct |
3 ms |
12892 KB |
Output is correct |
26 |
Correct |
3 ms |
12888 KB |
Output is correct |
27 |
Correct |
3 ms |
12892 KB |
Output is correct |
28 |
Correct |
2 ms |
12892 KB |
Output is correct |
29 |
Correct |
4 ms |
12892 KB |
Output is correct |
30 |
Correct |
3 ms |
12892 KB |
Output is correct |
31 |
Correct |
3 ms |
12888 KB |
Output is correct |
32 |
Correct |
3 ms |
12892 KB |
Output is correct |
33 |
Correct |
3 ms |
12892 KB |
Output is correct |
34 |
Correct |
13 ms |
15900 KB |
Output is correct |
35 |
Correct |
109 ms |
46964 KB |
Output is correct |
36 |
Correct |
114 ms |
46788 KB |
Output is correct |
37 |
Correct |
105 ms |
46792 KB |
Output is correct |
38 |
Correct |
102 ms |
46944 KB |
Output is correct |
39 |
Correct |
117 ms |
47044 KB |
Output is correct |
40 |
Correct |
87 ms |
44036 KB |
Output is correct |
41 |
Correct |
118 ms |
47024 KB |
Output is correct |
42 |
Correct |
99 ms |
46788 KB |
Output is correct |
43 |
Correct |
98 ms |
51144 KB |
Output is correct |
44 |
Correct |
125 ms |
47044 KB |
Output is correct |
45 |
Correct |
149 ms |
60036 KB |
Output is correct |
46 |
Correct |
107 ms |
47040 KB |
Output is correct |
47 |
Correct |
110 ms |
47044 KB |
Output is correct |
48 |
Correct |
112 ms |
50116 KB |
Output is correct |
49 |
Correct |
103 ms |
54272 KB |
Output is correct |
50 |
Correct |
90 ms |
44996 KB |
Output is correct |
51 |
Correct |
118 ms |
51916 KB |
Output is correct |
52 |
Correct |
158 ms |
63680 KB |
Output is correct |
53 |
Correct |
191 ms |
66160 KB |
Output is correct |
54 |
Correct |
181 ms |
72840 KB |
Output is correct |
55 |
Correct |
107 ms |
50776 KB |
Output is correct |
56 |
Correct |
177 ms |
69060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12916 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
89 ms |
39204 KB |
Output is correct |
10 |
Correct |
116 ms |
44560 KB |
Output is correct |
11 |
Correct |
97 ms |
44484 KB |
Output is correct |
12 |
Correct |
106 ms |
46796 KB |
Output is correct |
13 |
Correct |
104 ms |
50028 KB |
Output is correct |
14 |
Correct |
102 ms |
39276 KB |
Output is correct |
15 |
Correct |
200 ms |
46224 KB |
Output is correct |
16 |
Correct |
187 ms |
45896 KB |
Output is correct |
17 |
Correct |
205 ms |
48508 KB |
Output is correct |
18 |
Correct |
244 ms |
51596 KB |
Output is correct |
19 |
Correct |
269 ms |
52332 KB |
Output is correct |
20 |
Correct |
241 ms |
54832 KB |
Output is correct |
21 |
Correct |
289 ms |
52684 KB |
Output is correct |
22 |
Correct |
231 ms |
54676 KB |
Output is correct |
23 |
Correct |
256 ms |
54776 KB |
Output is correct |
24 |
Correct |
230 ms |
52252 KB |
Output is correct |
25 |
Correct |
277 ms |
54724 KB |
Output is correct |
26 |
Correct |
231 ms |
52196 KB |
Output is correct |
27 |
Correct |
3 ms |
12892 KB |
Output is correct |
28 |
Correct |
3 ms |
12892 KB |
Output is correct |
29 |
Correct |
5 ms |
12892 KB |
Output is correct |
30 |
Correct |
3 ms |
12892 KB |
Output is correct |
31 |
Correct |
2 ms |
12892 KB |
Output is correct |
32 |
Correct |
3 ms |
12892 KB |
Output is correct |
33 |
Correct |
4 ms |
12892 KB |
Output is correct |
34 |
Correct |
2 ms |
12892 KB |
Output is correct |
35 |
Correct |
3 ms |
13044 KB |
Output is correct |
36 |
Correct |
13 ms |
15900 KB |
Output is correct |
37 |
Correct |
109 ms |
46964 KB |
Output is correct |
38 |
Correct |
114 ms |
46788 KB |
Output is correct |
39 |
Correct |
105 ms |
46792 KB |
Output is correct |
40 |
Correct |
102 ms |
46944 KB |
Output is correct |
41 |
Correct |
117 ms |
47044 KB |
Output is correct |
42 |
Correct |
87 ms |
44036 KB |
Output is correct |
43 |
Correct |
118 ms |
47024 KB |
Output is correct |
44 |
Correct |
99 ms |
46788 KB |
Output is correct |
45 |
Correct |
98 ms |
51144 KB |
Output is correct |
46 |
Correct |
125 ms |
47044 KB |
Output is correct |
47 |
Correct |
17 ms |
16036 KB |
Output is correct |
48 |
Correct |
198 ms |
48384 KB |
Output is correct |
49 |
Correct |
215 ms |
48588 KB |
Output is correct |
50 |
Correct |
237 ms |
48512 KB |
Output is correct |
51 |
Correct |
202 ms |
48496 KB |
Output is correct |
52 |
Correct |
194 ms |
46020 KB |
Output is correct |
53 |
Correct |
152 ms |
36868 KB |
Output is correct |
54 |
Correct |
219 ms |
48828 KB |
Output is correct |
55 |
Correct |
248 ms |
48584 KB |
Output is correct |
56 |
Correct |
308 ms |
51820 KB |
Output is correct |
57 |
Correct |
245 ms |
48900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12888 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12916 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
89 ms |
39204 KB |
Output is correct |
11 |
Correct |
116 ms |
44560 KB |
Output is correct |
12 |
Correct |
97 ms |
44484 KB |
Output is correct |
13 |
Correct |
106 ms |
46796 KB |
Output is correct |
14 |
Correct |
104 ms |
50028 KB |
Output is correct |
15 |
Correct |
102 ms |
39276 KB |
Output is correct |
16 |
Correct |
200 ms |
46224 KB |
Output is correct |
17 |
Correct |
187 ms |
45896 KB |
Output is correct |
18 |
Correct |
205 ms |
48508 KB |
Output is correct |
19 |
Correct |
244 ms |
51596 KB |
Output is correct |
20 |
Correct |
269 ms |
52332 KB |
Output is correct |
21 |
Correct |
241 ms |
54832 KB |
Output is correct |
22 |
Correct |
289 ms |
52684 KB |
Output is correct |
23 |
Correct |
231 ms |
54676 KB |
Output is correct |
24 |
Correct |
256 ms |
54776 KB |
Output is correct |
25 |
Correct |
230 ms |
52252 KB |
Output is correct |
26 |
Correct |
277 ms |
54724 KB |
Output is correct |
27 |
Correct |
231 ms |
52196 KB |
Output is correct |
28 |
Correct |
3 ms |
12892 KB |
Output is correct |
29 |
Correct |
3 ms |
12892 KB |
Output is correct |
30 |
Correct |
5 ms |
12892 KB |
Output is correct |
31 |
Correct |
3 ms |
12892 KB |
Output is correct |
32 |
Correct |
2 ms |
12892 KB |
Output is correct |
33 |
Correct |
3 ms |
12892 KB |
Output is correct |
34 |
Correct |
4 ms |
12892 KB |
Output is correct |
35 |
Correct |
2 ms |
12892 KB |
Output is correct |
36 |
Correct |
3 ms |
13044 KB |
Output is correct |
37 |
Correct |
13 ms |
15900 KB |
Output is correct |
38 |
Correct |
109 ms |
46964 KB |
Output is correct |
39 |
Correct |
114 ms |
46788 KB |
Output is correct |
40 |
Correct |
105 ms |
46792 KB |
Output is correct |
41 |
Correct |
102 ms |
46944 KB |
Output is correct |
42 |
Correct |
117 ms |
47044 KB |
Output is correct |
43 |
Correct |
87 ms |
44036 KB |
Output is correct |
44 |
Correct |
118 ms |
47024 KB |
Output is correct |
45 |
Correct |
99 ms |
46788 KB |
Output is correct |
46 |
Correct |
98 ms |
51144 KB |
Output is correct |
47 |
Correct |
125 ms |
47044 KB |
Output is correct |
48 |
Correct |
17 ms |
16036 KB |
Output is correct |
49 |
Correct |
198 ms |
48384 KB |
Output is correct |
50 |
Correct |
215 ms |
48588 KB |
Output is correct |
51 |
Correct |
237 ms |
48512 KB |
Output is correct |
52 |
Correct |
202 ms |
48496 KB |
Output is correct |
53 |
Correct |
194 ms |
46020 KB |
Output is correct |
54 |
Correct |
152 ms |
36868 KB |
Output is correct |
55 |
Correct |
219 ms |
48828 KB |
Output is correct |
56 |
Correct |
248 ms |
48584 KB |
Output is correct |
57 |
Correct |
308 ms |
51820 KB |
Output is correct |
58 |
Correct |
245 ms |
48900 KB |
Output is correct |
59 |
Correct |
68 ms |
21248 KB |
Output is correct |
60 |
Correct |
188 ms |
48644 KB |
Output is correct |
61 |
Correct |
192 ms |
46468 KB |
Output is correct |
62 |
Correct |
212 ms |
49160 KB |
Output is correct |
63 |
Correct |
280 ms |
51948 KB |
Output is correct |
64 |
Correct |
3 ms |
12892 KB |
Output is correct |
65 |
Correct |
3 ms |
12892 KB |
Output is correct |
66 |
Correct |
3 ms |
12888 KB |
Output is correct |
67 |
Correct |
3 ms |
12892 KB |
Output is correct |
68 |
Correct |
2 ms |
12892 KB |
Output is correct |
69 |
Correct |
4 ms |
12892 KB |
Output is correct |
70 |
Correct |
3 ms |
12892 KB |
Output is correct |
71 |
Correct |
3 ms |
12888 KB |
Output is correct |
72 |
Correct |
3 ms |
12892 KB |
Output is correct |
73 |
Correct |
3 ms |
12892 KB |
Output is correct |
74 |
Correct |
149 ms |
60036 KB |
Output is correct |
75 |
Correct |
107 ms |
47040 KB |
Output is correct |
76 |
Correct |
110 ms |
47044 KB |
Output is correct |
77 |
Correct |
112 ms |
50116 KB |
Output is correct |
78 |
Correct |
103 ms |
54272 KB |
Output is correct |
79 |
Correct |
90 ms |
44996 KB |
Output is correct |
80 |
Correct |
118 ms |
51916 KB |
Output is correct |
81 |
Correct |
158 ms |
63680 KB |
Output is correct |
82 |
Correct |
191 ms |
66160 KB |
Output is correct |
83 |
Correct |
181 ms |
72840 KB |
Output is correct |
84 |
Correct |
107 ms |
50776 KB |
Output is correct |
85 |
Correct |
177 ms |
69060 KB |
Output is correct |
86 |
Correct |
58 ms |
28884 KB |
Output is correct |
87 |
Correct |
228 ms |
48488 KB |
Output is correct |
88 |
Correct |
197 ms |
48488 KB |
Output is correct |
89 |
Correct |
290 ms |
51396 KB |
Output is correct |
90 |
Correct |
189 ms |
59480 KB |
Output is correct |
91 |
Correct |
179 ms |
55488 KB |
Output is correct |
92 |
Correct |
220 ms |
52680 KB |
Output is correct |
93 |
Correct |
258 ms |
65024 KB |
Output is correct |
94 |
Correct |
297 ms |
68036 KB |
Output is correct |
95 |
Correct |
290 ms |
74884 KB |
Output is correct |
96 |
Correct |
276 ms |
51832 KB |
Output is correct |
97 |
Correct |
285 ms |
60036 KB |
Output is correct |