#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 1987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define COMPRESS(a) sort(ALL(a));a.resize(unique(ALL(a))-a.begin())
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<db, db> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<double> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int,int,int,int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 1000000007;
ll POW(ll a, ll b, ll MMM=MOD) {ll ret=1; for(;b;b>>=1,a=(a*a)%MMM)if(b&1)ret=(ret*a)% MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[]={2,2,-2,-2,1,1,-1,-1},ddy[]={1,-1,1,-1,2,-2,2,-2};
Vi v[100001];
int p[100001];
int find(int x){
if(p[x]==x)return x;
return p[x]=find(p[x]);
}
void merge(int x,int y){
x=find(x),y=find(y);
if(x!=y)p[x]=y;
}
int tree[262144],lazy[262144],hld[100001],cnt[100001],num[100001],idx[100001],parent[100001],depth[100001],cc;
void init(int node,int S, int E){
if(S==E){
tree[node]=1;
return;
}
init(node*2,S,(S+E)/2);
init(node*2+1,(S+E)/2+1,E);
tree[node]=tree[node*2]+tree[node*2+1];
}
void propagation(int node,int S,int E){
if(lazy[node]){
tree[node]=0;
if(S!=E){
lazy[node*2]=1;
lazy[node*2+1]=1;
}
lazy[node]=0;
}
}
void upd(int node, int S, int E, int i, int j)
{
propagation(node, S, E);
if (i > E || j < S) return;
if (j >= E && i <= S)
{
lazy[node] = 1;
propagation(node, S, E);
return;
}
upd(2 * node, S, (S + E) / 2, i, j);
upd(2 * node + 1, (S + E) / 2 + 1, E, i, j);
tree[node] = tree[2 * node] + tree[2 * node + 1];
}
int find(int node, int S, int E, int i, int j)
{
propagation(node, S, E);
if (i > E || j < S)return 0;
if (i <= S && j >= E)return tree[node];
return find(node * 2, S, (S + E) / 2, i, j)+ find(node * 2 + 1, (S + E) / 2 + 1, E, i, j);
}
void dfs(int N,int p,int d)
{
parent[N]=p;
depth[N]=d;
cnt[N]=1;
for(int next:v[N])
{
if(next==p)continue;
dfs(next,N,d+1);
cnt[N]+=cnt[next];
}
}
void HLD(int N,int p)
{
int c=-1;
num[N]=++cc;
idx[num[N]]=N;
for(int next:v[N])
{
if(next==p)continue;
if(c==-1 || cnt[next]>cnt[c])c=next;
}
if(c!=-1)
{
hld[c]=hld[N];
HLD(c,N);
}
for(int next:v[N])
{
if(next==p || next==c)continue;
hld[next]=next;
HLD(next,N);
}
}
int n,q;
void upd(int x,int y){
while(hld[x]!=hld[y])
{
if(depth[hld[x]]>depth[hld[y]])
{
upd(1,1,n,num[hld[x]],num[x]);
x=parent[hld[x]];
}
else
{
upd(1,1,n,num[hld[y]],num[y]);
y=parent[hld[y]];
}
}
if(depth[x]>depth[y])swap(x,y);
upd(1,1,n,num[x]+1,num[y]);
}
int query(int x,int y)
{
int res=0;
while(hld[x]!=hld[y])
{
if(depth[hld[x]]>depth[hld[y]])
{
res+=find(1,1,n,num[hld[x]],num[x]);
x=parent[hld[x]];
}
else
{
res+=find(1,1,n,num[hld[y]],num[y]);
y=parent[hld[y]];
}
}
if(depth[x]>depth[y])swap(x,y);
res+=find(1,1,n,num[x]+1,num[y]);
return res;
}
iii Q[300000];
int main() {
scanf("%d%d",&n,&q);
iota(p,p+n+1,0);
fup(i,0,q-1,1){
int t,x,y;
scanf("%d%d%d",&t,&x,&y);
Q[i]=iii(t,x,y);
if(t==1){
if(find(x)!=find(y)){
v[x].pb(y);
v[y].pb(x);
}
merge(x,y);
}
}
fup(i,1,n,1){
if(!num[i]){
dfs(i,-1,0);
hld[i]=i;
HLD(i,-1);
}
}
init(1,1,n);
iota(p,p+n+1,0);
fup(i,0,q-1,1){
auto [t,x,y]=Q[i];
if(t==1){
if(find(x)==find(y)){
upd(x,y);
}
merge(x,y);
}else{
if(find(x)!=find(y)){
puts("-1");
continue;
}
printf("%d\n",query(x,y));
}
}
}
Compilation message
road_development.cpp: In function 'int main()':
road_development.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
road_development.cpp:171:2: note: in expansion of macro 'fup'
171 | fup(i,0,q-1,1){
| ^~~
road_development.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
road_development.cpp:183:2: note: in expansion of macro 'fup'
183 | fup(i,1,n,1){
| ^~~
road_development.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
road_development.cpp:192:2: note: in expansion of macro 'fup'
192 | fup(i,0,q-1,1){
| ^~~
road_development.cpp:169:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
169 | scanf("%d%d",&n,&q);
| ~~~~~^~~~~~~~~~~~~~
road_development.cpp:173:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
173 | scanf("%d%d%d",&t,&x,&y);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2796 KB |
Output is correct |
2 |
Correct |
5 ms |
2796 KB |
Output is correct |
3 |
Correct |
4 ms |
2924 KB |
Output is correct |
4 |
Correct |
4 ms |
2924 KB |
Output is correct |
5 |
Correct |
5 ms |
2796 KB |
Output is correct |
6 |
Correct |
4 ms |
2924 KB |
Output is correct |
7 |
Correct |
5 ms |
2796 KB |
Output is correct |
8 |
Correct |
4 ms |
2924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
362 ms |
14564 KB |
Output is correct |
2 |
Correct |
863 ms |
14572 KB |
Output is correct |
3 |
Correct |
353 ms |
20076 KB |
Output is correct |
4 |
Correct |
392 ms |
20204 KB |
Output is correct |
5 |
Correct |
405 ms |
18284 KB |
Output is correct |
6 |
Correct |
348 ms |
18540 KB |
Output is correct |
7 |
Correct |
640 ms |
14512 KB |
Output is correct |
8 |
Correct |
356 ms |
18156 KB |
Output is correct |
9 |
Correct |
648 ms |
14444 KB |
Output is correct |
10 |
Correct |
324 ms |
17388 KB |
Output is correct |
11 |
Correct |
302 ms |
19564 KB |
Output is correct |
12 |
Correct |
291 ms |
21868 KB |
Output is correct |
13 |
Correct |
306 ms |
22508 KB |
Output is correct |
14 |
Correct |
325 ms |
19052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
399 ms |
14436 KB |
Output is correct |
2 |
Correct |
589 ms |
18540 KB |
Output is correct |
3 |
Correct |
432 ms |
22668 KB |
Output is correct |
4 |
Correct |
344 ms |
24940 KB |
Output is correct |
5 |
Correct |
365 ms |
18540 KB |
Output is correct |
6 |
Correct |
444 ms |
22764 KB |
Output is correct |
7 |
Correct |
373 ms |
18404 KB |
Output is correct |
8 |
Correct |
408 ms |
21868 KB |
Output is correct |
9 |
Correct |
359 ms |
22124 KB |
Output is correct |
10 |
Correct |
377 ms |
23916 KB |
Output is correct |
11 |
Correct |
344 ms |
18280 KB |
Output is correct |
12 |
Correct |
374 ms |
24044 KB |
Output is correct |
13 |
Correct |
357 ms |
24300 KB |
Output is correct |
14 |
Correct |
280 ms |
23020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
258 ms |
13284 KB |
Output is correct |
2 |
Correct |
470 ms |
13036 KB |
Output is correct |
3 |
Correct |
282 ms |
16852 KB |
Output is correct |
4 |
Correct |
254 ms |
18540 KB |
Output is correct |
5 |
Correct |
795 ms |
14188 KB |
Output is correct |
6 |
Correct |
405 ms |
19820 KB |
Output is correct |
7 |
Correct |
83 ms |
11788 KB |
Output is correct |
8 |
Correct |
91 ms |
17772 KB |
Output is correct |
9 |
Correct |
110 ms |
20204 KB |
Output is correct |
10 |
Correct |
186 ms |
15972 KB |
Output is correct |
11 |
Correct |
351 ms |
21252 KB |
Output is correct |
12 |
Correct |
416 ms |
21996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2796 KB |
Output is correct |
2 |
Correct |
5 ms |
2796 KB |
Output is correct |
3 |
Correct |
4 ms |
2924 KB |
Output is correct |
4 |
Correct |
4 ms |
2924 KB |
Output is correct |
5 |
Correct |
5 ms |
2796 KB |
Output is correct |
6 |
Correct |
4 ms |
2924 KB |
Output is correct |
7 |
Correct |
5 ms |
2796 KB |
Output is correct |
8 |
Correct |
4 ms |
2924 KB |
Output is correct |
9 |
Correct |
362 ms |
14564 KB |
Output is correct |
10 |
Correct |
863 ms |
14572 KB |
Output is correct |
11 |
Correct |
353 ms |
20076 KB |
Output is correct |
12 |
Correct |
392 ms |
20204 KB |
Output is correct |
13 |
Correct |
405 ms |
18284 KB |
Output is correct |
14 |
Correct |
348 ms |
18540 KB |
Output is correct |
15 |
Correct |
640 ms |
14512 KB |
Output is correct |
16 |
Correct |
356 ms |
18156 KB |
Output is correct |
17 |
Correct |
648 ms |
14444 KB |
Output is correct |
18 |
Correct |
324 ms |
17388 KB |
Output is correct |
19 |
Correct |
302 ms |
19564 KB |
Output is correct |
20 |
Correct |
291 ms |
21868 KB |
Output is correct |
21 |
Correct |
306 ms |
22508 KB |
Output is correct |
22 |
Correct |
325 ms |
19052 KB |
Output is correct |
23 |
Correct |
399 ms |
14436 KB |
Output is correct |
24 |
Correct |
589 ms |
18540 KB |
Output is correct |
25 |
Correct |
432 ms |
22668 KB |
Output is correct |
26 |
Correct |
344 ms |
24940 KB |
Output is correct |
27 |
Correct |
365 ms |
18540 KB |
Output is correct |
28 |
Correct |
444 ms |
22764 KB |
Output is correct |
29 |
Correct |
373 ms |
18404 KB |
Output is correct |
30 |
Correct |
408 ms |
21868 KB |
Output is correct |
31 |
Correct |
359 ms |
22124 KB |
Output is correct |
32 |
Correct |
377 ms |
23916 KB |
Output is correct |
33 |
Correct |
344 ms |
18280 KB |
Output is correct |
34 |
Correct |
374 ms |
24044 KB |
Output is correct |
35 |
Correct |
357 ms |
24300 KB |
Output is correct |
36 |
Correct |
280 ms |
23020 KB |
Output is correct |
37 |
Correct |
258 ms |
13284 KB |
Output is correct |
38 |
Correct |
470 ms |
13036 KB |
Output is correct |
39 |
Correct |
282 ms |
16852 KB |
Output is correct |
40 |
Correct |
254 ms |
18540 KB |
Output is correct |
41 |
Correct |
795 ms |
14188 KB |
Output is correct |
42 |
Correct |
405 ms |
19820 KB |
Output is correct |
43 |
Correct |
83 ms |
11788 KB |
Output is correct |
44 |
Correct |
91 ms |
17772 KB |
Output is correct |
45 |
Correct |
110 ms |
20204 KB |
Output is correct |
46 |
Correct |
186 ms |
15972 KB |
Output is correct |
47 |
Correct |
351 ms |
21252 KB |
Output is correct |
48 |
Correct |
416 ms |
21996 KB |
Output is correct |
49 |
Correct |
371 ms |
18696 KB |
Output is correct |
50 |
Correct |
719 ms |
18540 KB |
Output is correct |
51 |
Correct |
406 ms |
23788 KB |
Output is correct |
52 |
Correct |
374 ms |
26092 KB |
Output is correct |
53 |
Correct |
366 ms |
17380 KB |
Output is correct |
54 |
Correct |
334 ms |
17588 KB |
Output is correct |
55 |
Correct |
358 ms |
22408 KB |
Output is correct |
56 |
Correct |
306 ms |
23992 KB |
Output is correct |
57 |
Correct |
367 ms |
22124 KB |
Output is correct |
58 |
Correct |
432 ms |
21868 KB |
Output is correct |
59 |
Correct |
327 ms |
22252 KB |
Output is correct |
60 |
Correct |
295 ms |
23276 KB |
Output is correct |
61 |
Correct |
292 ms |
23316 KB |
Output is correct |