#include<iostream>
#include<vector>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18;
const int mx=100010;
struct dsu
{
int pa[mx];
inline void init(int n)
{
fill(pa,pa+n+1,0);
return;
}
int par(int x)
{
if(pa[x]==0)
return x;
return pa[x]=par(pa[x]);
}
inline void uni(int x,int y)
{
x=par(x);
y=par(y);
if(x==y)
return;
pa[x]=y;
return;
}
}uf;
struct seg
{
int t[mx*4];
void init(int n,int s,int e)
{
t[n]=e-s+1;
if(s==e)
return;
int m=s+(e-s)/2;
init(n*2,s,m);
init(n*2+1,m+1,e);
return;
}
inline void put(int n)
{
t[n]=0;
return;
}
inline void prop(int n)
{
if(t[n]!=0)
return;
put(n*2),put(n*2+1);
return;
}
void upd(int n,int s,int e,int S,int E)
{
if(s>E||S>e)
return;
if(S<=s&&e<=E)
return put(n);
prop(n);
int m=s+(e-s)/2;
upd(n*2,s,m,S,E);
upd(n*2+1,m+1,e,S,E);
t[n]=t[n*2]+t[n*2+1];
return;
}
int query(int n,int s,int e,int S,int E)
{
if(s>E||S>e)
return 0;
if(S<=s&&e<=E)
return t[n];
prop(n);
int m=s+(e-s)/2;
return query(n*2,s,m,S,E)+query(n*2+1,m+1,e,S,E);
}
}st;
int dep[mx];
int pa[mx];
vector<int>adj[mx];
int sz[mx];
void dfs(int x)
{
sz[x]=1;
for(int&t:adj[x])
dep[t]=dep[pa[t]=x]+1,adj[t].erase(find(all(adj[t]),x)),dfs(t),sz[x]+=sz[t];
sort(all(adj[x]),[&](const int&x,const int&y){return sz[x]>sz[y];});
return;
}
int ord[mx];
int ocnt;
int hc[mx];
void hld(int x)
{
if(hc[x]==0)
hc[x]=x;
ord[x]=++ocnt;
if(!adj[x].empty())
hc[adj[x][0]]=hc[x];
for(int&t:adj[x])
hld(t);
return;
}
int n;
inline void upd(int u,int v)
{
while(hc[u]!=hc[v])
{
if(dep[hc[u]]<dep[hc[v]])
swap(u,v);
st.upd(1,1,n,ord[hc[u]],ord[u]);
u=pa[hc[u]];
}
if(dep[u]<dep[v])
swap(u,v);
if(u!=v)
st.upd(1,1,n,ord[v]+1,ord[u]);
return;
}
inline int query(int u,int v)
{
int sm=0;
while(hc[u]!=hc[v])
{
if(dep[hc[u]]<dep[hc[v]])
swap(u,v);
sm+=st.query(1,1,n,ord[hc[u]],ord[u]);
u=pa[hc[u]];
}
if(dep[u]<dep[v])
swap(u,v);
if(u!=v)
sm+=st.query(1,1,n,ord[v]+1,ord[u]);
return sm;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int m;
cin>>n>>m;
vector<pair<int,pi> >qv;
vector<pi>ted;
for(int i=0;i<m;i++)
{
int t,u,v;
cin>>t>>u>>v;
qv.eb(t,pi(u,v));
if(t==1)
if(uf.par(u)!=uf.par(v))
uf.uni(u,v),ted.eb(u,v);
}
for(pi&t:ted)
adj[t.fi].eb(t.se),adj[t.se].eb(t.fi);
for(int i=0;i++<n;)
if(hc[i]==0)
dfs(i),hld(i);
st.init(1,1,n);
uf.init(n);
for(auto&tq:qv)
{
int t=tq.fi;
int u=tq.se.fi,v=tq.se.se;
if(t==1)
{
if(uf.par(u)!=uf.par(v))
uf.uni(u,v);
else
upd(u,v);
}
else
{
if(uf.par(u)!=uf.par(v))
cout<<"-1\n";
else
cout<<query(u,v)<<'\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2816 KB |
Output is correct |
2 |
Correct |
5 ms |
2816 KB |
Output is correct |
3 |
Correct |
4 ms |
2944 KB |
Output is correct |
4 |
Correct |
4 ms |
2944 KB |
Output is correct |
5 |
Correct |
5 ms |
2944 KB |
Output is correct |
6 |
Correct |
4 ms |
2944 KB |
Output is correct |
7 |
Correct |
5 ms |
2816 KB |
Output is correct |
8 |
Correct |
4 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
300 ms |
17056 KB |
Output is correct |
2 |
Correct |
710 ms |
16700 KB |
Output is correct |
3 |
Correct |
337 ms |
23440 KB |
Output is correct |
4 |
Correct |
307 ms |
23512 KB |
Output is correct |
5 |
Correct |
377 ms |
21804 KB |
Output is correct |
6 |
Correct |
327 ms |
21832 KB |
Output is correct |
7 |
Correct |
549 ms |
17944 KB |
Output is correct |
8 |
Correct |
310 ms |
21468 KB |
Output is correct |
9 |
Correct |
628 ms |
17708 KB |
Output is correct |
10 |
Correct |
304 ms |
20508 KB |
Output is correct |
11 |
Correct |
273 ms |
22680 KB |
Output is correct |
12 |
Correct |
285 ms |
25436 KB |
Output is correct |
13 |
Correct |
245 ms |
25744 KB |
Output is correct |
14 |
Correct |
314 ms |
22416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
271 ms |
17012 KB |
Output is correct |
2 |
Correct |
460 ms |
17908 KB |
Output is correct |
3 |
Correct |
320 ms |
22172 KB |
Output is correct |
4 |
Correct |
283 ms |
24324 KB |
Output is correct |
5 |
Correct |
262 ms |
18004 KB |
Output is correct |
6 |
Correct |
316 ms |
22256 KB |
Output is correct |
7 |
Correct |
216 ms |
18324 KB |
Output is correct |
8 |
Correct |
329 ms |
21012 KB |
Output is correct |
9 |
Correct |
281 ms |
21528 KB |
Output is correct |
10 |
Correct |
293 ms |
23064 KB |
Output is correct |
11 |
Correct |
236 ms |
17964 KB |
Output is correct |
12 |
Correct |
311 ms |
23320 KB |
Output is correct |
13 |
Correct |
305 ms |
23572 KB |
Output is correct |
14 |
Correct |
253 ms |
22560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
177 ms |
15392 KB |
Output is correct |
2 |
Correct |
386 ms |
15128 KB |
Output is correct |
3 |
Correct |
219 ms |
18848 KB |
Output is correct |
4 |
Correct |
203 ms |
20640 KB |
Output is correct |
5 |
Correct |
607 ms |
17456 KB |
Output is correct |
6 |
Correct |
344 ms |
23092 KB |
Output is correct |
7 |
Correct |
79 ms |
12580 KB |
Output is correct |
8 |
Correct |
89 ms |
17316 KB |
Output is correct |
9 |
Correct |
109 ms |
19484 KB |
Output is correct |
10 |
Correct |
139 ms |
15392 KB |
Output is correct |
11 |
Correct |
260 ms |
20740 KB |
Output is correct |
12 |
Correct |
333 ms |
21276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2816 KB |
Output is correct |
2 |
Correct |
5 ms |
2816 KB |
Output is correct |
3 |
Correct |
4 ms |
2944 KB |
Output is correct |
4 |
Correct |
4 ms |
2944 KB |
Output is correct |
5 |
Correct |
5 ms |
2944 KB |
Output is correct |
6 |
Correct |
4 ms |
2944 KB |
Output is correct |
7 |
Correct |
5 ms |
2816 KB |
Output is correct |
8 |
Correct |
4 ms |
2944 KB |
Output is correct |
9 |
Correct |
300 ms |
17056 KB |
Output is correct |
10 |
Correct |
710 ms |
16700 KB |
Output is correct |
11 |
Correct |
337 ms |
23440 KB |
Output is correct |
12 |
Correct |
307 ms |
23512 KB |
Output is correct |
13 |
Correct |
377 ms |
21804 KB |
Output is correct |
14 |
Correct |
327 ms |
21832 KB |
Output is correct |
15 |
Correct |
549 ms |
17944 KB |
Output is correct |
16 |
Correct |
310 ms |
21468 KB |
Output is correct |
17 |
Correct |
628 ms |
17708 KB |
Output is correct |
18 |
Correct |
304 ms |
20508 KB |
Output is correct |
19 |
Correct |
273 ms |
22680 KB |
Output is correct |
20 |
Correct |
285 ms |
25436 KB |
Output is correct |
21 |
Correct |
245 ms |
25744 KB |
Output is correct |
22 |
Correct |
314 ms |
22416 KB |
Output is correct |
23 |
Correct |
271 ms |
17012 KB |
Output is correct |
24 |
Correct |
460 ms |
17908 KB |
Output is correct |
25 |
Correct |
320 ms |
22172 KB |
Output is correct |
26 |
Correct |
283 ms |
24324 KB |
Output is correct |
27 |
Correct |
262 ms |
18004 KB |
Output is correct |
28 |
Correct |
316 ms |
22256 KB |
Output is correct |
29 |
Correct |
216 ms |
18324 KB |
Output is correct |
30 |
Correct |
329 ms |
21012 KB |
Output is correct |
31 |
Correct |
281 ms |
21528 KB |
Output is correct |
32 |
Correct |
293 ms |
23064 KB |
Output is correct |
33 |
Correct |
236 ms |
17964 KB |
Output is correct |
34 |
Correct |
311 ms |
23320 KB |
Output is correct |
35 |
Correct |
305 ms |
23572 KB |
Output is correct |
36 |
Correct |
253 ms |
22560 KB |
Output is correct |
37 |
Correct |
177 ms |
15392 KB |
Output is correct |
38 |
Correct |
386 ms |
15128 KB |
Output is correct |
39 |
Correct |
219 ms |
18848 KB |
Output is correct |
40 |
Correct |
203 ms |
20640 KB |
Output is correct |
41 |
Correct |
607 ms |
17456 KB |
Output is correct |
42 |
Correct |
344 ms |
23092 KB |
Output is correct |
43 |
Correct |
79 ms |
12580 KB |
Output is correct |
44 |
Correct |
89 ms |
17316 KB |
Output is correct |
45 |
Correct |
109 ms |
19484 KB |
Output is correct |
46 |
Correct |
139 ms |
15392 KB |
Output is correct |
47 |
Correct |
260 ms |
20740 KB |
Output is correct |
48 |
Correct |
333 ms |
21276 KB |
Output is correct |
49 |
Correct |
287 ms |
18192 KB |
Output is correct |
50 |
Correct |
505 ms |
18316 KB |
Output is correct |
51 |
Correct |
309 ms |
23148 KB |
Output is correct |
52 |
Correct |
289 ms |
25588 KB |
Output is correct |
53 |
Correct |
238 ms |
17060 KB |
Output is correct |
54 |
Correct |
255 ms |
17312 KB |
Output is correct |
55 |
Correct |
278 ms |
21912 KB |
Output is correct |
56 |
Correct |
242 ms |
23328 KB |
Output is correct |
57 |
Correct |
310 ms |
21236 KB |
Output is correct |
58 |
Correct |
297 ms |
21400 KB |
Output is correct |
59 |
Correct |
275 ms |
21664 KB |
Output is correct |
60 |
Correct |
232 ms |
22688 KB |
Output is correct |
61 |
Correct |
262 ms |
22688 KB |
Output is correct |