#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
ll readint(){
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,q,tot,T;
int t[300005],a[300005],b[300005],fa[300005],v[200005],nxt[200005],h[100005],dep[100005],p[100005][20],fenw[200005],dfn[100005],dfo[100005];
bool was[100005];
void addedge(int x,int y){
v[++tot]=y; nxt[tot]=h[x]; h[x]=tot;
v[++tot]=x; nxt[tot]=h[y]; h[y]=tot;
}
int gfa(int x){return fa[x]==x?x:fa[x]=gfa(fa[x]);}
bool unite(int x,int y){
x=gfa(x); y=gfa(y);
if(x==y) return false;
fa[y]=x;
return true;
}
void dfs(int x,int f){
dfn[x]=++T;
dep[x]=dep[f]+1;
p[x][0]=f;
for(int i=1;i<20;i++) p[x][i]=p[p[x][i-1]][i-1];
for(int p=h[x];p;p=nxt[p]){
if(v[p]==f) continue;
dfs(v[p],x);
}
dfo[x]=++T;
}
int lca(int x,int y){
if(dep[x]>dep[y]) swap(x,y);
for(int i=19;i>=0;i--) if(dep[p[y][i]]>=dep[x]) y=p[y][i];
for(int i=19;i>=0;i--) if(p[x][i]!=p[y][i]) x=p[x][i],y=p[y][i];
return x==y?x:p[x][0];
}
void change(int p,int x){
for(;p<=200000;p+=p&-p) fenw[p]+=x;
}
int query(int p){
int v=0;
for(;p>=1;p-=p&-p) v+=fenw[p];
return v;
}
int main(){
n=readint(); q=readint();
for(int i=1;i<=q;i++){
t[i]=readint(); a[i]=readint(); b[i]=readint();
}
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=q;i++){
if(t[i]==2) continue;
if(unite(a[i],b[i])){
addedge(a[i],b[i]);
}
}
for(int i=1;i<=n;i++) if(dep[i]==0) dfs(i,i);
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=q;i++){
int l=lca(a[i],b[i]);
if(t[i]==1){
if(!unite(a[i],b[i])){
for(int j=a[i];j!=l;j=p[j][0]){
if(query(dfn[j])-query(dfn[l])==dep[j]-dep[l]){
break;
}
int ver=j;
for(int k=19;k>=0;k--){
if(dep[p[ver][k]]>=dep[l]&&query(dfn[ver])-query(dfn[p[ver][k]])==dep[ver]-dep[p[ver][k]]) ver=p[ver][k];
}
if(ver==l) break;
change(dfn[ver],+1);
change(dfo[ver],-1);
j=ver;
}
for(int j=b[i];j!=l;j=p[j][0]){
if(query(dfn[j])-query(dfn[l])==dep[j]-dep[l]){
break;
}
int ver=j;
for(int k=19;k>=0;k--){
if(dep[p[ver][k]]>=dep[l]&&query(dfn[ver])-query(dfn[p[ver][k]])==dep[ver]-dep[p[ver][k]]) ver=p[ver][k];
}
if(ver==l) break;
change(dfn[ver],+1);
change(dfo[ver],-1);
j=ver;
}
}
continue;
}
if(gfa(a[i])!=gfa(b[i])){
printf("-1\n");
continue;
}
printf("%d\n",dep[a[i]]+dep[b[i]]-2*dep[l]-(query(dfn[a[i]])+query(dfn[b[i]])-2*query(dfn[l])));
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6600 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
155 ms |
20920 KB |
Output is correct |
2 |
Correct |
186 ms |
20908 KB |
Output is correct |
3 |
Correct |
224 ms |
25504 KB |
Output is correct |
4 |
Correct |
268 ms |
25708 KB |
Output is correct |
5 |
Correct |
252 ms |
24128 KB |
Output is correct |
6 |
Correct |
230 ms |
24272 KB |
Output is correct |
7 |
Correct |
161 ms |
20988 KB |
Output is correct |
8 |
Correct |
219 ms |
23892 KB |
Output is correct |
9 |
Correct |
136 ms |
20564 KB |
Output is correct |
10 |
Correct |
267 ms |
23356 KB |
Output is correct |
11 |
Correct |
215 ms |
25048 KB |
Output is correct |
12 |
Correct |
216 ms |
27164 KB |
Output is correct |
13 |
Correct |
198 ms |
27476 KB |
Output is correct |
14 |
Correct |
215 ms |
24692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
137 ms |
21076 KB |
Output is correct |
2 |
Correct |
179 ms |
21080 KB |
Output is correct |
3 |
Correct |
215 ms |
24404 KB |
Output is correct |
4 |
Correct |
239 ms |
26100 KB |
Output is correct |
5 |
Correct |
145 ms |
21076 KB |
Output is correct |
6 |
Correct |
222 ms |
24556 KB |
Output is correct |
7 |
Correct |
145 ms |
20876 KB |
Output is correct |
8 |
Correct |
232 ms |
23564 KB |
Output is correct |
9 |
Correct |
218 ms |
24040 KB |
Output is correct |
10 |
Correct |
200 ms |
25428 KB |
Output is correct |
11 |
Correct |
122 ms |
20568 KB |
Output is correct |
12 |
Correct |
259 ms |
25528 KB |
Output is correct |
13 |
Correct |
190 ms |
25940 KB |
Output is correct |
14 |
Correct |
167 ms |
24908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
18516 KB |
Output is correct |
2 |
Correct |
120 ms |
18748 KB |
Output is correct |
3 |
Correct |
167 ms |
21844 KB |
Output is correct |
4 |
Correct |
202 ms |
23272 KB |
Output is correct |
5 |
Correct |
161 ms |
20636 KB |
Output is correct |
6 |
Correct |
223 ms |
25284 KB |
Output is correct |
7 |
Correct |
73 ms |
16684 KB |
Output is correct |
8 |
Correct |
105 ms |
20408 KB |
Output is correct |
9 |
Correct |
116 ms |
22304 KB |
Output is correct |
10 |
Correct |
93 ms |
18780 KB |
Output is correct |
11 |
Correct |
211 ms |
23556 KB |
Output is correct |
12 |
Correct |
208 ms |
23828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6600 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6596 KB |
Output is correct |
9 |
Correct |
155 ms |
20920 KB |
Output is correct |
10 |
Correct |
186 ms |
20908 KB |
Output is correct |
11 |
Correct |
224 ms |
25504 KB |
Output is correct |
12 |
Correct |
268 ms |
25708 KB |
Output is correct |
13 |
Correct |
252 ms |
24128 KB |
Output is correct |
14 |
Correct |
230 ms |
24272 KB |
Output is correct |
15 |
Correct |
161 ms |
20988 KB |
Output is correct |
16 |
Correct |
219 ms |
23892 KB |
Output is correct |
17 |
Correct |
136 ms |
20564 KB |
Output is correct |
18 |
Correct |
267 ms |
23356 KB |
Output is correct |
19 |
Correct |
215 ms |
25048 KB |
Output is correct |
20 |
Correct |
216 ms |
27164 KB |
Output is correct |
21 |
Correct |
198 ms |
27476 KB |
Output is correct |
22 |
Correct |
215 ms |
24692 KB |
Output is correct |
23 |
Correct |
137 ms |
21076 KB |
Output is correct |
24 |
Correct |
179 ms |
21080 KB |
Output is correct |
25 |
Correct |
215 ms |
24404 KB |
Output is correct |
26 |
Correct |
239 ms |
26100 KB |
Output is correct |
27 |
Correct |
145 ms |
21076 KB |
Output is correct |
28 |
Correct |
222 ms |
24556 KB |
Output is correct |
29 |
Correct |
145 ms |
20876 KB |
Output is correct |
30 |
Correct |
232 ms |
23564 KB |
Output is correct |
31 |
Correct |
218 ms |
24040 KB |
Output is correct |
32 |
Correct |
200 ms |
25428 KB |
Output is correct |
33 |
Correct |
122 ms |
20568 KB |
Output is correct |
34 |
Correct |
259 ms |
25528 KB |
Output is correct |
35 |
Correct |
190 ms |
25940 KB |
Output is correct |
36 |
Correct |
167 ms |
24908 KB |
Output is correct |
37 |
Correct |
93 ms |
18516 KB |
Output is correct |
38 |
Correct |
120 ms |
18748 KB |
Output is correct |
39 |
Correct |
167 ms |
21844 KB |
Output is correct |
40 |
Correct |
202 ms |
23272 KB |
Output is correct |
41 |
Correct |
161 ms |
20636 KB |
Output is correct |
42 |
Correct |
223 ms |
25284 KB |
Output is correct |
43 |
Correct |
73 ms |
16684 KB |
Output is correct |
44 |
Correct |
105 ms |
20408 KB |
Output is correct |
45 |
Correct |
116 ms |
22304 KB |
Output is correct |
46 |
Correct |
93 ms |
18780 KB |
Output is correct |
47 |
Correct |
211 ms |
23556 KB |
Output is correct |
48 |
Correct |
208 ms |
23828 KB |
Output is correct |
49 |
Correct |
139 ms |
21076 KB |
Output is correct |
50 |
Correct |
200 ms |
21112 KB |
Output is correct |
51 |
Correct |
227 ms |
25176 KB |
Output is correct |
52 |
Correct |
270 ms |
27272 KB |
Output is correct |
53 |
Correct |
127 ms |
20076 KB |
Output is correct |
54 |
Correct |
122 ms |
20308 KB |
Output is correct |
55 |
Correct |
211 ms |
24148 KB |
Output is correct |
56 |
Correct |
268 ms |
25700 KB |
Output is correct |
57 |
Correct |
299 ms |
23892 KB |
Output is correct |
58 |
Correct |
233 ms |
24228 KB |
Output is correct |
59 |
Correct |
223 ms |
24148 KB |
Output is correct |
60 |
Correct |
219 ms |
24916 KB |
Output is correct |
61 |
Correct |
205 ms |
24924 KB |
Output is correct |