#include <bits/stdc++.h>
#define mp make_pair
#define eb emplace_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define svec(x) sort(all(x))
#define press(x) x.erase(unique(all(x)), x.end());
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, LL> pil;
typedef pair<LL, int> pli;
typedef pair<LL, LL> pll;
const int INF=1e9;
const LL LLINF=1e18;
struct UNION_FIND{
int par[100010];
void init(){for(int i=1; i<=100000; i++)par[i]=i;}
int findpar(int num){return num==par[num]?num:par[num]=findpar(par[num]);}
void mergepar(int a, int b){par[findpar(a)]=findpar(b);}
}uf;
vector<int> link[100010];
int p[100010], sz[100010], eul[100010], re;
int col[100010], tp[100010], c;
bool ch[100010];
void dfs1(int num, int par){
ch[num]=true;
sz[num]=1;
p[num]=par;
for(auto i:link[num]){
if(i==par)continue;
dfs1(i, num);
sz[num]+=sz[i];
}
}
bool cmp(int a, int b){return sz[a]>sz[b];}
void dfs2(int num, int par, int cl){
ch[num]=true;
col[num]=cl;
eul[num]=++re;
bool flg=false;
sort(all(link[num]), cmp);
for(auto i:link[num]){
if(i==par)continue;
if(flg){
tp[++c]=i;
dfs2(i, num, c);
}
else dfs2(i, num, cl);
flg=true;
}
}
struct LAZY_SEG{
pii tree[400010];
int lzy[400010];
void prop(int point, int s, int e){
if(s==e){
tree[point].F+=lzy[point];
lzy[point]=0;
return;
}
lzy[point*2]+=lzy[point];
lzy[point*2+1]+=lzy[point];
lzy[point]=0;
}
void eat(int point, int s, int e){
pii l=tree[point*2], r=tree[point*2+1];
l.F+=lzy[point*2], r.F+=lzy[point*2+1];
tree[point]=mp(min(l.F, r.F), 0);
if(tree[point].F==l.F)tree[point].S+=l.S;
if(tree[point].F==r.F)tree[point].S+=r.S;
}
void init(int point, int s, int e){
tree[point]=mp(0, e-s+1);
if(s==e)return;
init(point*2, s, (s+e)/2);
init(point*2+1, (s+e)/2+1, e);
}
void alter(int point, int s, int e, int a, int b){
if(e<a||s>b)return;
if(a<=s&&e<=b){
lzy[point]++;
return;
}
prop(point, s, e);
alter(point*2, s, (s+e)/2, a, b);
alter(point*2+1, (s+e)/2+1, e, a, b);
eat(point, s, e);
}
pii query(int point, int s, int e, int a, int b){
if(e<a||s>b)return mp(INF, 0);
if(a<=s&&e<=b)return mp(tree[point].F+lzy[point], tree[point].S);
prop(point, s, e);
pii l=query(point*2, s, (s+e)/2, a, b);
pii r=query(point*2+1, (s+e)/2+1, e, a, b);
eat(point, s, e);
pii ret=mp(min(l.F, r.F), 0);
if(ret.F==l.F)ret.S+=l.S;
if(ret.F==r.F)ret.S+=r.S;
return ret;
}
}seg;
int lca(int a, int b){
if(col[a]==col[b]){
if(eul[a]<eul[b])return a;
return b;
}
if(col[a]>col[b])swap(a, b);
return lca(a, p[tp[col[b]]]);
}
int n, q;
pair<int, pii> qu[300010];
void update(int a, int b){
int l=lca(a, b);
while(1){
if(col[a]==col[l]){
if(l!=a)seg.alter(1, 1, n, eul[l]+1, eul[a]);
break;
}
seg.alter(1, 1, n, eul[tp[col[a]]], eul[a]);
a=p[tp[col[a]]];
}
while(1){
if(col[b]==col[l]){
if(l!=b)seg.alter(1, 1, n, eul[l]+1, eul[b]);
break;
}
seg.alter(1, 1, n, eul[tp[col[b]]], eul[b]);
b=p[tp[col[b]]];
}
}
int query(int a, int b){
int l=lca(a, b), ret=0;
pii tmp;
while(1){
if(col[a]==col[l]){
if(l!=a){
tmp=seg.query(1, 1, n, eul[l]+1, eul[a]);
if(tmp.F==0)ret+=tmp.S;
}
break;
}
tmp=seg.query(1, 1, n, eul[tp[col[a]]], eul[a]);
if(tmp.F==0)ret+=tmp.S;
a=p[tp[col[a]]];
}
while(1){
if(col[b]==col[l]){
if(l!=b){
tmp=seg.query(1, 1, n, eul[l]+1, eul[b]);
if(tmp.F==0)ret+=tmp.S;
}
break;
}
tmp=seg.query(1, 1, n, eul[tp[col[b]]], eul[b]);
if(tmp.F==0)ret+=tmp.S;
b=p[tp[col[b]]];
}
return ret;
}
int main(){
scanf("%d %d", &n, &q);
uf.init();
seg.init(1, 1, n);
for(int i=1; i<=q; i++){
scanf("%d %d %d", &qu[i].F, &qu[i].S.F, &qu[i].S.S);
if(qu[i].F==2)continue;
if(uf.findpar(qu[i].S.F)!=uf.findpar(qu[i].S.S)){
uf.mergepar(qu[i].S.F, qu[i].S.S);
link[qu[i].S.F].eb(qu[i].S.S);
link[qu[i].S.S].eb(qu[i].S.F);
}
}
for(int i=1; i<=n; i++){
if(ch[i])continue;
dfs1(i, 0);
}
memset(ch, 0, sizeof ch);
for(int i=1; i<=n; i++){
if(ch[i])continue;
tp[++c]=i;
dfs2(i, 0, c);
}
uf.init();
for(int i=1; i<=q; i++){
if(qu[i].F==1){
if(uf.findpar(qu[i].S.F)!=uf.findpar(qu[i].S.S))
uf.mergepar(qu[i].S.F, qu[i].S.S);
else update(qu[i].S.F, qu[i].S.S);
}
else{
if(uf.findpar(qu[i].S.F)!=uf.findpar(qu[i].S.S))puts("-1");
else printf("%d\n", query(qu[i].S.F, qu[i].S.S));
}
}
}
Compilation message
road_development.cpp: In function 'int main()':
road_development.cpp:173:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
173 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
road_development.cpp:177:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
177 | scanf("%d %d %d", &qu[i].F, &qu[i].S.F, &qu[i].S.S);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
6400 KB |
Output is correct |
2 |
Correct |
8 ms |
6400 KB |
Output is correct |
3 |
Correct |
7 ms |
6524 KB |
Output is correct |
4 |
Correct |
8 ms |
6528 KB |
Output is correct |
5 |
Correct |
8 ms |
6400 KB |
Output is correct |
6 |
Correct |
7 ms |
6528 KB |
Output is correct |
7 |
Correct |
8 ms |
6400 KB |
Output is correct |
8 |
Correct |
8 ms |
6528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
421 ms |
16488 KB |
Output is correct |
2 |
Correct |
949 ms |
16068 KB |
Output is correct |
3 |
Correct |
474 ms |
25684 KB |
Output is correct |
4 |
Correct |
428 ms |
25592 KB |
Output is correct |
5 |
Correct |
484 ms |
23836 KB |
Output is correct |
6 |
Correct |
477 ms |
23800 KB |
Output is correct |
7 |
Correct |
872 ms |
20296 KB |
Output is correct |
8 |
Correct |
483 ms |
23568 KB |
Output is correct |
9 |
Correct |
838 ms |
19832 KB |
Output is correct |
10 |
Correct |
452 ms |
22520 KB |
Output is correct |
11 |
Correct |
469 ms |
24824 KB |
Output is correct |
12 |
Correct |
367 ms |
27384 KB |
Output is correct |
13 |
Correct |
352 ms |
27768 KB |
Output is correct |
14 |
Correct |
469 ms |
24568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
446 ms |
16512 KB |
Output is correct |
2 |
Correct |
742 ms |
15992 KB |
Output is correct |
3 |
Correct |
495 ms |
24184 KB |
Output is correct |
4 |
Correct |
423 ms |
26208 KB |
Output is correct |
5 |
Correct |
385 ms |
20216 KB |
Output is correct |
6 |
Correct |
519 ms |
24184 KB |
Output is correct |
7 |
Correct |
317 ms |
20592 KB |
Output is correct |
8 |
Correct |
553 ms |
23160 KB |
Output is correct |
9 |
Correct |
371 ms |
23544 KB |
Output is correct |
10 |
Correct |
504 ms |
25208 KB |
Output is correct |
11 |
Correct |
414 ms |
20208 KB |
Output is correct |
12 |
Correct |
552 ms |
25464 KB |
Output is correct |
13 |
Correct |
508 ms |
25720 KB |
Output is correct |
14 |
Correct |
399 ms |
24696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
247 ms |
15344 KB |
Output is correct |
2 |
Correct |
518 ms |
14968 KB |
Output is correct |
3 |
Correct |
294 ms |
19576 KB |
Output is correct |
4 |
Correct |
258 ms |
21240 KB |
Output is correct |
5 |
Correct |
812 ms |
17020 KB |
Output is correct |
6 |
Correct |
417 ms |
22520 KB |
Output is correct |
7 |
Correct |
99 ms |
14840 KB |
Output is correct |
8 |
Correct |
108 ms |
19320 KB |
Output is correct |
9 |
Correct |
128 ms |
21624 KB |
Output is correct |
10 |
Correct |
181 ms |
17844 KB |
Output is correct |
11 |
Correct |
338 ms |
22648 KB |
Output is correct |
12 |
Correct |
449 ms |
23380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
6400 KB |
Output is correct |
2 |
Correct |
8 ms |
6400 KB |
Output is correct |
3 |
Correct |
7 ms |
6524 KB |
Output is correct |
4 |
Correct |
8 ms |
6528 KB |
Output is correct |
5 |
Correct |
8 ms |
6400 KB |
Output is correct |
6 |
Correct |
7 ms |
6528 KB |
Output is correct |
7 |
Correct |
8 ms |
6400 KB |
Output is correct |
8 |
Correct |
8 ms |
6528 KB |
Output is correct |
9 |
Correct |
421 ms |
16488 KB |
Output is correct |
10 |
Correct |
949 ms |
16068 KB |
Output is correct |
11 |
Correct |
474 ms |
25684 KB |
Output is correct |
12 |
Correct |
428 ms |
25592 KB |
Output is correct |
13 |
Correct |
484 ms |
23836 KB |
Output is correct |
14 |
Correct |
477 ms |
23800 KB |
Output is correct |
15 |
Correct |
872 ms |
20296 KB |
Output is correct |
16 |
Correct |
483 ms |
23568 KB |
Output is correct |
17 |
Correct |
838 ms |
19832 KB |
Output is correct |
18 |
Correct |
452 ms |
22520 KB |
Output is correct |
19 |
Correct |
469 ms |
24824 KB |
Output is correct |
20 |
Correct |
367 ms |
27384 KB |
Output is correct |
21 |
Correct |
352 ms |
27768 KB |
Output is correct |
22 |
Correct |
469 ms |
24568 KB |
Output is correct |
23 |
Correct |
446 ms |
16512 KB |
Output is correct |
24 |
Correct |
742 ms |
15992 KB |
Output is correct |
25 |
Correct |
495 ms |
24184 KB |
Output is correct |
26 |
Correct |
423 ms |
26208 KB |
Output is correct |
27 |
Correct |
385 ms |
20216 KB |
Output is correct |
28 |
Correct |
519 ms |
24184 KB |
Output is correct |
29 |
Correct |
317 ms |
20592 KB |
Output is correct |
30 |
Correct |
553 ms |
23160 KB |
Output is correct |
31 |
Correct |
371 ms |
23544 KB |
Output is correct |
32 |
Correct |
504 ms |
25208 KB |
Output is correct |
33 |
Correct |
414 ms |
20208 KB |
Output is correct |
34 |
Correct |
552 ms |
25464 KB |
Output is correct |
35 |
Correct |
508 ms |
25720 KB |
Output is correct |
36 |
Correct |
399 ms |
24696 KB |
Output is correct |
37 |
Correct |
247 ms |
15344 KB |
Output is correct |
38 |
Correct |
518 ms |
14968 KB |
Output is correct |
39 |
Correct |
294 ms |
19576 KB |
Output is correct |
40 |
Correct |
258 ms |
21240 KB |
Output is correct |
41 |
Correct |
812 ms |
17020 KB |
Output is correct |
42 |
Correct |
417 ms |
22520 KB |
Output is correct |
43 |
Correct |
99 ms |
14840 KB |
Output is correct |
44 |
Correct |
108 ms |
19320 KB |
Output is correct |
45 |
Correct |
128 ms |
21624 KB |
Output is correct |
46 |
Correct |
181 ms |
17844 KB |
Output is correct |
47 |
Correct |
338 ms |
22648 KB |
Output is correct |
48 |
Correct |
449 ms |
23380 KB |
Output is correct |
49 |
Correct |
429 ms |
20684 KB |
Output is correct |
50 |
Correct |
729 ms |
20216 KB |
Output is correct |
51 |
Correct |
478 ms |
25336 KB |
Output is correct |
52 |
Correct |
399 ms |
27388 KB |
Output is correct |
53 |
Correct |
336 ms |
19440 KB |
Output is correct |
54 |
Correct |
405 ms |
19448 KB |
Output is correct |
55 |
Correct |
379 ms |
24056 KB |
Output is correct |
56 |
Correct |
379 ms |
25336 KB |
Output is correct |
57 |
Correct |
490 ms |
23416 KB |
Output is correct |
58 |
Correct |
449 ms |
23416 KB |
Output is correct |
59 |
Correct |
416 ms |
23672 KB |
Output is correct |
60 |
Correct |
355 ms |
24824 KB |
Output is correct |
61 |
Correct |
413 ms |
24952 KB |
Output is correct |