QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#689886#4901. Speike & TomAcoipp100 ✓341ms65560kbC++1410.0kb2024-10-30 19:03:312024-10-30 19:03:32

Judging History

你现在查看的是最新测评结果

  • [2024-10-30 19:03:32]
  • 评测
  • 测评结果:100
  • 用时:341ms
  • 内存:65560kb
  • [2024-10-30 19:03:31]
  • 提交

answer

#include<bits/stdc++.h>
#define ll int
#define N 100005
using namespace std;
inline char nc(){
	static char buf[1000000],*p=buf,*q=buf;
	return p==q&&(q=(p=buf)+fread(buf,1,1000000,stdin),p==q)?EOF:*p++;
}
inline ll read(){
	ll res = 0,w = 1;
	char c = nc();
	while(c<'0'||c>'9')w=(c=='-'?-1:w),c=nc();
	while(c<='9'&&c>='0')res=res*10+c-'0',c=nc();
	return res*w;
}
char obuf[1<<21],*p3=obuf; 
inline void pc(char c){ 
	p3-obuf<=(1<<20)?(*p3++=c):(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=c); 
} 
inline void write(long long x){ 
	if(x<0) pc('-'),x=-x; 
	if(x>9) write(x/10); 
	pc(x%10+'0'); 
}
vector<ll> extra[N];
ll NOW,n,m,i,x,y,la[N],ne[N<<1],to[N<<1],et,vis[N],st[N][21],dep[N],key[N],keyboard[N],col[N],tim,si[N],st_cut[N][21],st_extra[N][21],depp[N],next_root,fath[N],dis0[N],dis1[N],type[N],DIS[N],FATH[N],instack[N];
vector<ll> v1,v2;
map<ll,ll> m1[N],m2[N];
long long ans;
struct BIT{
	ll tr[N];
	inline void add(ll k,ll x){while(k<=n) tr[k]+=x,k+=k&(-k);}
	inline ll query(ll k){
		ll num = 0;
		while(k) num+=tr[k],k-=k&(-k);
		return num;
	}
	inline void clear(ll k){while(k<=n) tr[k]=0,k+=k&(-k);}
}tr1,tr2;
inline void merge(ll x,ll y){
	et++,ne[et]=la[x],la[x]=et,to[et]=y;
	et++,ne[et]=la[y],la[y]=et,to[et]=x;
}
inline void dfs(ll x,ll fa){
	FATH[x] = fa;
	for(ll i=1;i<=17;i++) st[x][i]=st[st[x][i-1]][i-1];
	for(ll i=la[x];i;i=ne[i]){
		if(to[i]==fa) continue;
		dep[to[i]] = dep[x]+1,st[to[i]][0] = x;
		dfs(to[i],x);
	}
}
inline void dfs2(ll x,ll fa){
	keyboard[x] = vis[x];
	for(ll i=la[x];i;i=ne[i]){
		if(to[i]==fa) continue;
		dfs2(to[i],x);
		keyboard[x] += keyboard[to[i]];
	}
}
inline ll dis11(ll x,ll y){
	ll ans = dep[x]+dep[y];
	if(dep[x]>dep[y]) swap(x,y);
	for(ll i=17;i>=0;i--) if(dep[st[y][i]]>=dep[x]) y=st[y][i];
	if(x==y) return ans-2*dep[x];
	for(ll i=17;i>=0;i--) if(st[x][i]!=st[y][i]) x=st[x][i],y=st[y][i];
	return ans-2*dep[st[x][0]];
}
inline void find_root(ll x,ll fa,ll &pos,ll &num,ll alls){
	ll now = 0;
	si[x] = 1;
	for(ll i=la[x];i;i=ne[i]){
		if(col[to[i]]!=col[x]||to[i]==fa) continue;
		find_root(to[i],x,pos,num,alls);
		si[x] += si[to[i]],now = max(now,si[to[i]]);
	}
	now = max(now,alls-si[x]);
	if(now<num) num=now,pos=x;
}
inline void init(ll x,ll fa,ll root){
	NOW = max(NOW,depp[x]);
	if(fa==-1) key[x]=keyboard[1];
	else{
		key[x]=keyboard[1];
		if(fa==FATH[x]) key[x]=keyboard[x];
		else key[x]-=keyboard[fa];
	}
	if(col[x]!=col[root]){
		si[x]=0,fath[x]=fa,dis0[x]=dis1[x]=0x3f3f3f3f;
		return ;
	}
	si[x] = (col[x]==col[root]),fath[x] = fa,dis0[x] = 0x3f3f3f3f,dis1[x] = 0x3f3f3f3f;
	for(ll i=la[x];i;i=ne[i]){
		if(to[i]==fa) continue;
		depp[to[i]] = depp[x] + 1; 
		init(to[i],x,root);
		si[x] += si[to[i]];
	}
}
inline void dfs_col(ll x,ll fa){
	for(ll i=la[x];i;i=ne[i]){
		if(col[to[i]]!=col[x]||to[i]==fa) continue;
		dfs_col(to[i],x);
	}
	col[x]=tim;
}
inline void dfs_insert_b(ll x,ll fa,ll delta){
	tr1.add(depp[x],delta);
	for(ll i=la[x];i;i=ne[i]) if(col[to[i]]==col[x]&&to[i]!=fa) dfs_insert_b(to[i],x,delta);
}
inline ll foundd(ll x,ll deppp){
	ll ans = 0;
	for(ll i=NOW;i>=0;i--) if(st_extra[x][i]&&depp[st_extra[x][i]]>deppp) x=st_extra[x][i],ans+=(1ll<<i);
	return ans+(depp[x]>deppp);
}
inline void dfs_find_a(ll x,ll fa,ll root){
	instack[x]=1;
	if(depp[x]==1) st_extra[x][0]=fa;
	else{
		st_extra[x][0]=fa;
		for(ll i=0;i<extra[x].size();i++) if(depp[extra[x][i]]==depp[x]-2) st_extra[x][0]=extra[x][i];
	}
	for(ll i=1;i<=NOW;i++) st_cut[x][i]=st_cut[st_cut[x][i-1]][i-1];
	for(ll i=1;i<=NOW;i++) st_extra[x][i]=st_extra[st_extra[x][i-1]][i-1];
	if(key[x]) ans+=tr1.query(n)+1;
	else{
		vector<ll> v(0);
		ll pos = x,step = -1,delta = -1;
		for(ll i=NOW;i>=0;i--) if(st_cut[pos][i]&&!key[st_cut[pos][i]]) pos=st_cut[pos][i];
		for(ll i=0;i<extra[pos].size();i++) if(depp[extra[pos][i]]==depp[pos]&&key[extra[pos][i]]) v.push_back(extra[pos][i]);
		if(v.size()) step=foundd(x,depp[pos])+1,delta=depp[v[0]];
		else step=foundd(x,depp[st_cut[pos][0]]),delta=depp[st_cut[pos][0]];
		pos=st_cut[pos][0];
		if(step-delta<0) ans++;
		ans+=tr1.query(n)-tr1.query(max(0,step-delta));
		if(pos==root&&v.size()==1&&col[v[0]]==col[x]){
			next_root = v[0];
			v1.push_back(step),v2.push_back(foundd(x,depp[pos]));
		}
	}
	for(ll i=la[x];i;i=ne[i]){
		if(col[to[i]]!=col[x]||to[i]==fa) continue;
		st_cut[to[i]][0] = x;
		dfs_find_a(to[i],x,root);
	}
	for(ll i=0;i<=NOW;i++) st_cut[x][i]=0,st_extra[x][i]=0;
	instack[x]=0;
}
inline void dfss(ll x,ll fa,ll type){
	if(type==0) ans -= tr2.query(depp[x]);
	else ans += tr2.query(depp[x]-1);
	for(ll i=la[x];i;i=ne[i]) if(col[to[i]]==col[x]&&to[i]!=fa) dfss(to[i],x,type);
}
inline void dfs_find_b(ll x,ll fa,ll root){
	instack[x] = 1;
	for(ll i=1;i<=NOW;i++) st_cut[x][i]=st_cut[st_cut[x][i-1]][i-1];
	if(fa!=root) dis0[x] = 0x3f3f3f3f,dis1[x] = 0x3f3f3f3f;
	dis0[x] = min(dis0[x],dis0[fa]+1),dis1[x] = min(dis1[x],dis1[fa]+1);
	for(ll i=0;i<extra[x].size();i++){
		if(depp[extra[x][i]]==depp[x]-2){
			dis0[x] = min(dis0[x],dis0[extra[x][i]]+1);
			dis1[x] = min(dis1[x],dis1[extra[x][i]]+1);
		}
	}
	ll pos = x;
	for(ll i=NOW;i>=0;i--) if(st_cut[pos][i]&&key[root]-key[st_cut[pos][i]]) pos=st_cut[pos][i];
	if(st_cut[pos][0]&&(key[root]-key[pos])) pos=st_cut[pos][0];
	ll cntt = key[root];
	for(ll i=la[pos];i;i=ne[i]) if(instack[to[i]]&&depp[to[i]]>depp[pos]) cntt -= key[to[i]];
	if(cntt){
		ll temp = 0;
		for(ll i=0;i<extra[fath[pos]].size();i++) if(depp[extra[fath[pos]][i]]==depp[pos]+1&&!instack[extra[fath[pos]][i]]&&m1[extra[fath[pos]][i]][pos]) temp=1;
		if(fath[pos]!=root){
			ll step = -1,dis00 = -1,dis11 = -1;
			if(temp==0) step=depp[x]-depp[pos],dis00 = dis0[pos],dis11 = dis1[pos];
			else step=depp[x]-depp[pos]+1,dis00 = dis0[fath[pos]]+1,dis11 = dis1[fath[pos]]+1;
			if(step-1-dis00>=0) ans++;
			ans+=tr1.query(max(step-1-dis00,0));
			ans+=tr2.query(max(step-1-dis11,0));
		}
		else{
			ll step = -1,dis00 = -1,dis11 = -1;
			if(temp==0) step=depp[x]-depp[pos],dis00 = dis0[pos],dis11 = dis1[pos];
			else step=depp[x]-depp[pos]+1,dis00 = 1,dis11 = 1;
			if(step-1-dis00>=0) ans++;
			ans+=tr1.query(max(step-1-dis00,0));
			ans+=tr2.query(max(step-1-dis11,0));
		}
	}	
	for(ll i=la[x];i;i=ne[i]){
		if(to[i]==fa||col[to[i]]!=col[x]) continue;
		st_cut[to[i]][0] = x;
		dfs_find_b(to[i],x,root);
	}
	for(ll i=0;i<=NOW;i++) st_cut[x][i]=0;
	instack[x] = 0;
}
inline void dfs_insert_a(ll x,ll fa){
	if(type[x]==0) tr1.add(DIS[x],1);
	if(type[x]==1) tr2.add(DIS[x],1);
	for(ll i=la[x];i;i=ne[i]){
		if(to[i]==fa||col[to[i]]!=col[x]) continue;
		if(m2[to[i]][fa]) DIS[to[i]]=DIS[fa]+1,type[to[i]]=type[fa];
		else DIS[to[i]]=DIS[x]+1,type[to[i]]=type[x];
		dfs_insert_a(to[i],x);
	}
}
inline void dfs_erase_a(ll x,ll fa){
	if(type[x]==0) tr1.add(DIS[x],-1);
	if(type[x]==1) tr2.add(DIS[x],-1);
	for(ll i=la[x];i;i=ne[i]){
		if(to[i]==fa||col[to[i]]!=col[x]) continue;
		dfs_erase_a(to[i],x);
	}
}
inline void dfs_alls(ll x,ll fa,ll &alls){
	alls++;
	for(ll i=la[x];i;i=ne[i]) if(col[to[i]]==col[x]&&to[i]!=fa) dfs_alls(to[i],x,alls);
}
inline void solve(ll x){
	ll pos = -1,num = INT_MAX,cnt_key_son = 0,alls = 0;
	dfs_alls(x,-1,alls);
	if(alls==1) return ;	
	find_root(x,-1,pos,num,alls);
	NOW = 0;
	depp[pos] = 0,init(pos,-1,pos);
	NOW = log2(NOW);
	for(ll i=la[pos];i;i=ne[i]){
		ll keys = 0;
		if(FATH[pos]==to[i]) keys=keyboard[1]-keyboard[pos];
		else keys=keyboard[to[i]];
		if(keys) cnt_key_son++;
	}
	for(ll i=la[pos];i;i=ne[i]) if(col[to[i]]==col[pos]) dfs_insert_b(to[i],pos,1);
	if(!(cnt_key_son==1&&!vis[pos])){
		//在 A-P 上 
		ans += (alls-1); //Pos 作为 A 点 
		for(ll i=la[pos];i;i=ne[i]){
			if(col[to[i]]==col[pos]){
				next_root = -1;
				st_cut[to[i]][0] = pos;
				dfs_insert_b(to[i],pos,-1);
				dfs_find_a(to[i],pos,pos);
				if(next_root!=-1){
					for(ll j=0;j<v1.size();j++) tr2.add(v1[j],1);
					dfss(next_root,pos,0);
					for(ll j=0;j<v1.size();j++) tr2.clear(v1[j]);
					for(ll j=0;j<v2.size();j++) tr2.add(v2[j],1);
					dfss(next_root,pos,1);
					for(ll j=0;j<v2.size();j++) tr2.clear(v2[j]);
					v1.clear(),v2.clear();
				}
				dfs_insert_b(to[i],pos,1);
			}
		}
		for(ll i=la[pos];i;i=ne[i]) if(col[to[i]]==col[pos]) dfs_insert_b(to[i],pos,-1);
	}
	else{
		for(ll i=la[pos];i;i=ne[i]) if(col[to[i]]==col[pos]&&key[to[i]]) dfs_insert_b(to[i],pos,-1);
		//在 B-P(不包含) 上 
		for(ll i=la[pos];i;i=ne[i]){
			if(col[to[i]]==col[pos]){
				st_cut[to[i]][0] = pos;
				if(!key[to[i]]) dfs_insert_b(to[i],pos,-1);
				dfs_find_a(to[i],pos,pos);
				v1.clear(),v2.clear();
				if(!key[to[i]]) dfs_insert_b(to[i],pos,1);
			}
		}
		ll root = -1;
		for(ll i=la[pos];i;i=ne[i]) if(col[to[i]]==col[pos]&&!key[to[i]]) dfs_insert_b(to[i],pos,-1);
		for(ll i=la[pos];i;i=ne[i]) if(col[to[i]]==col[pos]) if(key[to[i]]) root=to[i];
		ll num = 0;
		for(ll i=la[pos];i;i=ne[i]) if(col[to[i]]==col[pos]) if(!key[to[i]]) ans+=si[to[i]],num+=si[to[i]];
		type[pos]=0,DIS[pos]=0;
		for(ll i=la[pos];i;i=ne[i]){
			if(col[to[i]]==col[pos]){
				if(!key[to[i]]){
					if(m2[to[i]][root]) DIS[to[i]]=1,type[to[i]]=1;
					else DIS[to[i]]=1,type[to[i]]=0;
					dfs_insert_a(to[i],pos);
				}
			} 
		}
		dis0[pos] = 0;
		for(ll i=la[pos];i;i=ne[i]) if(col[to[i]]==col[pos]) if(key[to[i]]) dis0[to[i]]=1,dis1[to[i]]=0,dfs_find_b(to[i],pos,pos);
		for(ll i=la[pos];i;i=ne[i]) if(col[to[i]]==col[pos]) if(!key[to[i]]) dfs_erase_a(to[i],pos);
	}
	for(ll i=la[pos];i;i=ne[i]) if(col[to[i]]==col[pos]) tim++,dfs_col(to[i],pos),solve(to[i]);
}
int main(){
	ll has = 0;
	n=read(),m=read();
	for(i=1;i<n;i++) x=read(),y=read(),merge(x,y),m1[x][y]=m1[y][x]=1;
	dep[1]=1,dfs(1,-1);
	for(i=1;i<=m;i++){
		x=read(),y=read();
		ll temp = dis11(x,y);
		if(temp==1) continue;
		if(temp==2) extra[x].push_back(y),extra[y].push_back(x),m2[x][y]=m2[y][x]=1;
		if(temp>2) vis[x]=1,vis[y]=1,has=1;
	}
	if(!has){
		write(0);
		return fwrite(obuf,p3-obuf,1,stdout),0;
	}
	dfs2(1,-1);
	solve(1);
	write(ans);
	return fwrite(obuf,p3-obuf,1,stdout),0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 2ms
memory: 28336kb

input:

20 3
1 2
1 3
3 4
4 5
1 6
6 7
1 8
5 9
8 10
5 11
7 12
11 13
12 14
11 15
4 16
7 17
2 18
1 19
3 20
8 20
12 4
10 1

output:

307

result:

ok 1 number(s): "307"

Test #2:

score: 10
Accepted
time: 2ms
memory: 27096kb

input:

20 4
1 2
2 3
1 4
3 5
4 6
3 7
4 8
3 9
2 10
8 11
8 12
7 13
8 14
14 15
4 16
1 17
4 18
6 19
19 20
9 13
14 2
14 13
15 6

output:

343

result:

ok 1 number(s): "343"

Test #3:

score: 10
Accepted
time: 0ms
memory: 28484kb

input:

19 4
1 2
1 3
1 4
4 5
4 6
3 7
7 8
6 9
4 10
2 11
6 12
7 13
1 14
13 15
6 16
12 17
3 18
3 19
8 12
4 6
18 5
11 9

output:

314

result:

ok 1 number(s): "314"

Test #4:

score: 10
Accepted
time: 3ms
memory: 29580kb

input:

20 3
1 2
2 3
3 4
4 5
1 6
2 7
1 8
3 9
9 10
5 11
8 12
10 13
12 14
8 15
9 16
11 17
5 18
17 19
19 20
9 19
10 15
18 15

output:

358

result:

ok 1 number(s): "358"

Test #5:

score: 10
Accepted
time: 0ms
memory: 30248kb

input:

20 6
1 2
1 3
3 4
4 5
1 6
6 7
5 8
4 9
7 10
6 11
10 12
6 13
10 14
1 15
15 16
8 17
17 18
15 19
18 20
17 2
16 3
10 1
8 7
13 5
19 7

output:

361

result:

ok 1 number(s): "361"

Test #6:

score: 10
Accepted
time: 0ms
memory: 26408kb

input:

20 8
1 2
2 3
1 4
1 5
1 6
4 7
2 8
4 9
2 10
1 11
11 12
2 13
10 14
1 15
6 16
16 17
11 18
10 19
3 20
2 6
6 7
1 18
18 13
1 20
3 12
8 4
7 1

output:

324

result:

ok 1 number(s): "324"

Subtask #2:

score: 15
Accepted

Dependency #1:

100%
Accepted

Test #7:

score: 15
Accepted
time: 0ms
memory: 27052kb

input:

300 26
1 2
1 3
2 4
4 5
3 6
6 7
5 8
2 9
1 10
10 11
6 12
8 13
6 14
10 15
6 16
4 17
9 18
10 19
5 20
18 21
6 22
10 23
18 24
1 25
19 26
17 27
8 28
10 29
25 30
16 31
27 32
13 33
4 34
5 35
12 36
9 37
15 38
32 39
29 40
11 41
5 42
28 43
1 44
25 45
27 46
3 47
34 48
27 49
9 50
39 51
20 52
48 53
10 54
35 55
23 ...

output:

86687

result:

ok 1 number(s): "86687"

Test #8:

score: 15
Accepted
time: 0ms
memory: 30664kb

input:

300 40
1 2
1 3
3 4
4 5
5 6
3 7
6 8
5 9
2 10
1 11
10 12
11 13
7 14
10 15
6 16
7 17
5 18
12 19
15 20
18 21
15 22
8 23
12 24
23 25
18 26
10 27
4 28
4 29
16 30
20 31
15 32
9 33
6 34
33 35
22 36
27 37
12 38
21 39
37 40
29 41
41 42
32 43
4 44
1 45
12 46
34 47
24 48
20 49
33 50
25 51
47 52
6 53
25 54
5 55
...

output:

88364

result:

ok 1 number(s): "88364"

Test #9:

score: 15
Accepted
time: 4ms
memory: 26276kb

input:

299 100
1 2
1 3
1 4
3 5
3 6
4 7
1 8
6 9
5 10
8 11
1 12
2 13
11 14
3 15
8 16
14 17
11 18
2 19
3 20
10 21
3 22
17 23
23 24
16 25
7 26
5 27
12 28
8 29
7 30
29 31
23 32
1 33
1 34
29 35
7 36
5 37
6 38
19 39
22 40
21 41
5 42
29 43
19 44
38 45
23 46
21 47
29 48
27 49
28 50
47 51
25 52
28 53
9 54
45 55
52 5...

output:

88551

result:

ok 1 number(s): "88551"

Test #10:

score: 15
Accepted
time: 4ms
memory: 26420kb

input:

300 10
1 2
2 3
3 4
1 5
2 6
5 7
5 8
7 9
2 10
7 11
1 12
4 13
2 14
5 15
8 16
7 17
11 18
13 19
14 20
2 21
8 22
18 23
23 24
16 25
22 26
13 27
9 28
22 29
3 30
19 31
28 32
6 33
12 34
9 35
27 36
17 37
36 38
17 39
9 40
37 41
32 42
11 43
37 44
42 45
34 46
26 47
39 48
16 49
42 50
24 51
39 52
38 53
32 54
24 55
...

output:

82854

result:

ok 1 number(s): "82854"

Test #11:

score: 15
Accepted
time: 0ms
memory: 27148kb

input:

300 67
1 2
2 3
3 4
1 5
5 6
4 7
6 8
4 9
5 10
4 11
10 12
2 13
8 14
9 15
3 16
4 17
5 18
12 19
1 20
17 21
6 22
4 23
18 24
18 25
14 26
20 27
7 28
26 29
2 30
14 31
20 32
6 33
30 34
29 35
19 36
33 37
12 38
7 39
35 40
9 41
3 42
42 43
33 44
27 45
25 46
7 47
5 48
24 49
47 50
23 51
19 52
41 53
19 54
1 55
27 56...

output:

88372

result:

ok 1 number(s): "88372"

Test #12:

score: 15
Accepted
time: 0ms
memory: 30400kb

input:

280 280
1 2
2 3
3 4
3 5
4 6
2 7
6 8
5 9
4 10
5 11
5 12
12 13
13 14
5 15
13 16
16 17
7 18
10 19
6 20
10 21
4 22
8 23
10 24
15 25
14 26
20 27
17 28
19 29
28 30
27 31
26 32
19 33
6 34
15 35
14 36
33 37
1 38
23 39
33 40
4 41
8 42
10 43
7 44
8 45
4 46
37 47
25 48
38 49
30 50
49 51
31 52
23 53
9 54
46 55
...

output:

78036

result:

ok 1 number(s): "78036"

Subtask #3:

score: 15
Accepted

Dependency #2:

100%
Accepted

Test #13:

score: 15
Accepted
time: 0ms
memory: 26368kb

input:

300 26
1 2
1 3
2 4
4 5
3 6
6 7
5 8
2 9
1 10
10 11
6 12
8 13
6 14
10 15
6 16
4 17
9 18
10 19
5 20
18 21
6 22
10 23
18 24
1 25
19 26
17 27
8 28
10 29
25 30
16 31
27 32
13 33
4 34
5 35
12 36
9 37
15 38
32 39
29 40
11 41
5 42
28 43
1 44
25 45
27 46
3 47
34 48
27 49
9 50
39 51
20 52
48 53
10 54
35 55
23 ...

output:

86687

result:

ok 1 number(s): "86687"

Test #14:

score: 15
Accepted
time: 0ms
memory: 30300kb

input:

8 2
5 1
6 2
1 3
8 4
3 7
2 1
1 4
2 3
8 5

output:

34

result:

ok 1 number(s): "34"

Test #15:

score: 15
Accepted
time: 3ms
memory: 28956kb

input:

1000 80
1 2
1 3
3 4
1 5
1 6
4 7
3 8
7 9
5 10
9 11
6 12
6 13
3 14
5 15
7 16
4 17
13 18
12 19
13 20
13 21
8 22
10 23
3 24
18 25
7 26
2 27
15 28
14 29
1 30
8 31
22 32
28 33
3 34
34 35
4 36
25 37
3 38
25 39
29 40
29 41
22 42
10 43
27 44
12 45
27 46
27 47
39 48
42 49
13 50
30 51
46 52
34 53
16 54
49 55
1...

output:

988373

result:

ok 1 number(s): "988373"

Test #16:

score: 15
Accepted
time: 5ms
memory: 30484kb

input:

2000 40
1 2
2 3
1 4
4 5
5 6
1 7
7 8
3 9
7 10
3 11
7 12
12 13
3 14
2 15
15 16
16 17
4 18
3 19
18 20
17 21
1 22
17 23
7 24
16 25
6 26
14 27
12 28
13 29
15 30
13 31
30 32
30 33
33 34
28 35
22 36
9 37
7 38
10 39
12 40
5 41
26 42
10 43
27 44
1 45
18 46
34 47
26 48
47 49
24 50
34 51
37 52
28 53
15 54
32 5...

output:

3822427

result:

ok 1 number(s): "3822427"

Test #17:

score: 15
Accepted
time: 3ms
memory: 27456kb

input:

2000 10
1 2
1 3
1 4
3 5
1 6
6 7
4 8
2 9
1 10
6 11
3 12
9 13
13 14
14 15
12 16
14 17
15 18
15 19
16 20
13 21
4 22
9 23
2 24
4 25
1 26
13 27
7 28
20 29
4 30
9 31
14 32
14 33
8 34
5 35
18 36
31 37
11 38
37 39
17 40
20 41
40 42
29 43
24 44
21 45
15 46
38 47
1 48
47 49
35 50
32 51
26 52
9 53
14 54
25 55
...

output:

3501231

result:

ok 1 number(s): "3501231"

Test #18:

score: 15
Accepted
time: 8ms
memory: 28896kb

input:

2000 200
1 2
2 3
1 4
3 5
2 6
4 7
2 8
1 9
4 10
9 11
9 12
8 13
10 14
9 15
6 16
5 17
5 18
4 19
3 20
11 21
14 22
13 23
20 24
15 25
24 26
19 27
16 28
23 29
28 30
30 31
16 32
5 33
20 34
1 35
9 36
5 37
30 38
15 39
6 40
40 41
31 42
25 43
28 44
42 45
2 46
7 47
34 48
2 49
35 50
42 51
28 52
38 53
35 54
51 55
2...

output:

3970625

result:

ok 1 number(s): "3970625"

Test #19:

score: 15
Accepted
time: 3ms
memory: 26628kb

input:

1998 1996
1 2
2 3
3 4
1 5
1 6
6 7
7 8
8 9
2 10
4 11
8 12
12 13
7 14
4 15
13 16
16 17
3 18
14 19
10 20
7 21
9 22
16 23
13 24
21 25
18 26
18 27
11 28
27 29
16 30
12 31
13 32
12 33
29 34
27 35
1 36
29 37
3 38
14 39
26 40
11 41
21 42
27 43
35 44
17 45
20 46
43 47
23 48
39 49
30 50
27 51
46 52
11 53
29 5...

output:

3989549

result:

ok 1 number(s): "3989549"

Test #20:

score: 15
Accepted
time: 0ms
memory: 26728kb

input:

1999 3
1 2
1 3
3 4
4 5
4 6
5 7
4 8
2 9
9 10
9 11
8 12
5 13
9 14
8 15
14 16
12 17
5 18
3 19
15 20
12 21
15 22
17 23
18 24
11 25
21 26
17 27
1 28
10 29
25 30
7 31
21 32
31 33
5 34
7 35
26 36
29 37
33 38
11 39
10 40
25 41
39 42
8 43
6 44
24 45
5 46
45 47
21 48
30 49
6 50
35 51
27 52
23 53
41 54
36 55
1...

output:

2959137

result:

ok 1 number(s): "2959137"

Subtask #4:

score: 20
Accepted

Dependency #3:

100%
Accepted

Test #21:

score: 20
Accepted
time: 4ms
memory: 30300kb

input:

8 2
5 1
6 2
1 3
8 4
3 7
2 1
1 4
2 3
8 5

output:

34

result:

ok 1 number(s): "34"

Test #22:

score: 20
Accepted
time: 89ms
memory: 42060kb

input:

40000 1000
1 2
1 3
3 4
3 5
3 6
1 7
3 8
6 9
9 10
2 11
3 12
7 13
12 14
8 15
13 16
5 17
11 18
15 19
1 20
12 21
11 22
15 23
10 24
12 25
12 26
3 27
3 28
28 29
24 30
29 31
23 32
1 33
21 34
14 35
23 36
3 37
34 38
12 39
24 40
25 41
40 42
27 43
9 44
31 45
35 46
31 47
18 48
21 49
44 50
48 51
2 52
44 53
18 54
...

output:

1595797221

result:

ok 1 number(s): "1595797221"

Test #23:

score: 20
Accepted
time: 80ms
memory: 43140kb

input:

40000 34000
1 2
1 3
2 4
3 5
2 6
5 7
2 8
7 9
9 10
5 11
11 12
4 13
11 14
2 15
10 16
14 17
15 18
16 19
13 20
11 21
19 22
22 23
10 24
3 25
21 26
21 27
24 28
1 29
7 30
22 31
8 32
12 33
24 34
26 35
14 36
5 37
4 38
9 39
16 40
5 41
35 42
27 43
20 44
24 45
26 46
27 47
32 48
8 49
43 50
42 51
27 52
37 53
3 54
...

output:

1599943090

result:

ok 1 number(s): "1599943090"

Test #24:

score: 20
Accepted
time: 96ms
memory: 46348kb

input:

39998 8
1 2
2 3
1 4
3 5
3 6
1 7
7 8
8 9
8 10
4 11
4 12
11 13
8 14
11 15
12 16
6 17
1 18
17 19
6 20
11 21
10 22
22 23
17 24
11 25
3 26
6 27
6 28
12 29
6 30
24 31
12 32
22 33
8 34
31 35
15 36
25 37
20 38
25 39
24 40
9 41
38 42
2 43
32 44
25 45
27 46
21 47
36 48
44 49
7 50
3 51
37 52
35 53
34 54
22 55
...

output:

1329770016

result:

ok 1 number(s): "1329770016"

Test #25:

score: 20
Accepted
time: 90ms
memory: 47920kb

input:

39999 100
1 2
1 3
1 4
2 5
3 6
5 7
3 8
7 9
9 10
9 11
4 12
2 13
5 14
7 15
12 16
7 17
11 18
14 19
5 20
8 21
8 22
14 23
8 24
20 25
19 26
25 27
16 28
25 29
20 30
12 31
19 32
32 33
22 34
8 35
33 36
1 37
25 38
4 39
5 40
24 41
39 42
21 43
17 44
6 45
5 46
25 47
14 48
24 49
1 50
45 51
20 52
27 53
2 54
53 55
1...

output:

1552570727

result:

ok 1 number(s): "1552570727"

Test #26:

score: 20
Accepted
time: 79ms
memory: 48604kb

input:

40000 90
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52...

output:

1599720848

result:

ok 1 number(s): "1599720848"

Test #27:

score: 20
Accepted
time: 27ms
memory: 44508kb

input:

39998 20000
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
8 10
1 11
1 12
1 13
1 14
1 15
10 16
1 17
1 18
1 19
1 20
1 21
10 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
26 30
1 31
29 32
1 33
1 34
12 35
25 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
13 44
1 45
1 46
15 47
1 48
26 49
1 50
18 51
1 52
1 53
1 54
1 55
1 56
43 57
1 58
...

output:

953273251

result:

ok 1 number(s): "953273251"

Test #28:

score: 20
Accepted
time: 13ms
memory: 46428kb

input:

40000 2
2 1
3 2
4 3
5 2
6 5
7 6
8 7
9 1
10 1
11 1
12 1
13 1
14 1
15 1
16 1
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 1
25 1
26 1
27 1
28 1
29 1
30 1
31 1
32 1
33 1
34 1
35 1
36 1
37 1
38 1
39 1
40 1
41 1
42 1
43 1
44 1
45 1
46 1
47 1
48 1
49 1
50 1
51 1
52 1
53 1
54 1
55 1
56 1
57 1
58 1
59 1
60 1
61 1
...

output:

359967

result:

ok 1 number(s): "359967"

Subtask #5:

score: 10
Accepted

Test #29:

score: 10
Accepted
time: 18ms
memory: 40508kb

input:

98765 1
2 1
3 1
4 2
5 2
6 5
7 4
8 6
9 7
10 7
11 6
12 1
13 11
14 13
15 7
16 6
17 14
18 4
19 13
20 14
21 11
22 21
23 1
24 13
25 7
26 16
27 8
28 21
29 20
30 10
31 12
32 10
33 7
34 31
35 29
36 29
37 30
38 34
39 38
40 14
41 40
42 26
43 33
44 1
45 44
46 25
47 14
48 2
49 30
50 26
51 46
52 34
53 32
54 31
55...

output:

0

result:

ok 1 number(s): "0"

Test #30:

score: 10
Accepted
time: 21ms
memory: 40984kb

input:

99824 1
2 1
3 1
4 1
5 1
6 2
7 2
8 1
9 3
10 9
11 4
12 6
13 6
14 2
15 3
16 9
17 13
18 15
19 4
20 13
21 12
22 15
23 5
24 16
25 17
26 9
27 26
28 18
29 8
30 23
31 5
32 31
33 28
34 5
35 11
36 20
37 6
38 36
39 35
40 4
41 11
42 10
43 12
44 28
45 15
46 38
47 9
48 36
49 16
50 45
51 49
52 44
53 6
54 12
55 5
56...

output:

0

result:

ok 1 number(s): "0"

Test #31:

score: 10
Accepted
time: 194ms
memory: 55864kb

input:

67765 1
2 1
3 2
4 1
5 2
6 2
7 3
8 4
9 3
10 5
11 6
12 4
13 7
14 13
15 11
16 11
17 3
18 8
19 2
20 13
21 19
22 7
23 14
24 7
25 6
26 21
27 1
28 1
29 2
30 16
31 9
32 31
33 26
34 1
35 21
36 10
37 32
38 16
39 38
40 10
41 20
42 23
43 8
44 10
45 14
46 42
47 12
48 17
49 45
50 28
51 42
52 49
53 44
54 9
55 8
56...

output:

2254349415

result:

ok 1 number(s): "2254349415"

Test #32:

score: 10
Accepted
time: 341ms
memory: 65560kb

input:

100000 1
2 1
3 1
4 3
5 4
6 2
7 1
8 2
9 5
10 3
11 9
12 10
13 3
14 12
15 13
16 11
17 8
18 14
19 8
20 16
21 7
22 18
23 19
24 21
25 5
26 10
27 22
28 21
29 8
30 17
31 10
32 30
33 7
34 22
35 13
36 30
37 8
38 20
39 23
40 11
41 17
42 35
43 2
44 13
45 17
46 37
47 4
48 37
49 11
50 26
51 45
52 45
53 18
54 44
5...

output:

4706707961

result:

ok 1 number(s): "4706707961"

Subtask #6:

score: 30
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Dependency #5:

100%
Accepted

Test #33:

score: 30
Accepted
time: 40ms
memory: 64508kb

input:

100000 2
2 1
3 2
4 3
5 2
6 5
7 6
8 7
9 1
10 1
11 1
12 1
13 1
14 1
15 1
16 1
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 1
25 1
26 1
27 1
28 1
29 1
30 1
31 1
32 1
33 1
34 1
35 1
36 1
37 1
38 1
39 1
40 1
41 1
42 1
43 1
44 1
45 1
46 1
47 1
48 1
49 1
50 1
51 1
52 1
53 1
54 1
55 1
56 1
57 1
58 1
59 1
60 1
61 1...

output:

899967

result:

ok 1 number(s): "899967"

Test #34:

score: 30
Accepted
time: 2ms
memory: 26868kb

input:

8 2
5 1
6 2
1 3
8 4
3 7
2 1
1 4
2 3
8 5

output:

34

result:

ok 1 number(s): "34"

Test #35:

score: 30
Accepted
time: 2ms
memory: 29640kb

input:

300 26
1 2
1 3
2 4
4 5
3 6
6 7
5 8
2 9
1 10
10 11
6 12
8 13
6 14
10 15
6 16
4 17
9 18
10 19
5 20
18 21
6 22
10 23
18 24
1 25
19 26
17 27
8 28
10 29
25 30
16 31
27 32
13 33
4 34
5 35
12 36
9 37
15 38
32 39
29 40
11 41
5 42
28 43
1 44
25 45
27 46
3 47
34 48
27 49
9 50
39 51
20 52
48 53
10 54
35 55
23 ...

output:

86687

result:

ok 1 number(s): "86687"

Test #36:

score: 30
Accepted
time: 24ms
memory: 40464kb

input:

99999 1
2 1
3 2
4 3
5 2
6 1
7 1
8 4
9 2
10 2
11 7
12 10
13 4
14 9
15 9
16 9
17 15
18 7
19 10
20 13
21 9
22 11
23 3
24 13
25 7
26 8
27 6
28 19
29 21
30 14
31 2
32 12
33 22
34 32
35 14
36 7
37 4
38 1
39 8
40 34
41 10
42 8
43 34
44 39
45 22
46 36
47 34
48 2
49 31
50 27
51 46
52 47
53 49
54 3
55 25
56 3...

output:

0

result:

ok 1 number(s): "0"

Test #37:

score: 30
Accepted
time: 266ms
memory: 63040kb

input:

100000 400
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 4
11 10
12 11
13 12
14 13
15 14
16 15
17 16
18 17
19 11
20 19
21 2
22 21
23 22
24 23
25 24
26 25
27 26
28 27
29 28
30 29
31 30
32 31
33 32
34 23
35 34
36 35
37 36
38 37
39 38
40 39
41 40
42 41
43 19
44 43
45 44
46 45
47 46
48 47
49 48
50 49
51 50
52 51
5...

output:

9933452098

result:

ok 1 number(s): "9933452098"

Test #38:

score: 30
Accepted
time: 43ms
memory: 61624kb

input:

99998 10000
2 1
3 1
4 1
5 1
6 1
7 1
8 5
9 1
10 1
11 1
12 1
13 1
14 1
15 2
16 1
17 1
18 1
19 1
20 1
21 1
22 2
23 1
24 7
25 1
26 1
27 1
28 1
29 1
30 1
31 1
32 1
33 1
34 1
35 1
36 1
37 1
38 1
39 1
40 1
41 1
42 1
43 1
44 1
45 1
46 1
47 1
48 1
49 1
50 1
51 1
52 1
53 1
54 1
55 1
56 1
57 1
58 1
59 44
60 1
...

output:

2147621794

result:

ok 1 number(s): "2147621794"

Test #39:

score: 30
Accepted
time: 330ms
memory: 65520kb

input:

100000 10
2 1
3 1
4 3
5 1
6 5
7 4
8 2
9 2
10 5
11 2
12 11
13 6
14 11
15 1
16 5
17 13
18 14
19 12
20 8
21 16
22 3
23 10
24 2
25 5
26 24
27 8
28 11
29 12
30 6
31 21
32 4
33 19
34 29
35 31
36 15
37 31
38 1
39 17
40 33
41 34
42 15
43 3
44 39
45 10
46 40
47 1
48 18
49 25
50 42
51 13
52 40
53 4
54 3
55 38...

output:

8473241803

result:

ok 1 number(s): "8473241803"

Test #40:

score: 30
Accepted
time: 299ms
memory: 63876kb

input:

100000 300
2 1
3 1
4 1
5 2
6 3
7 1
8 3
9 1
10 8
11 1
12 8
13 7
14 13
15 8
16 10
17 8
18 4
19 18
20 12
21 8
22 18
23 11
24 22
25 14
26 4
27 25
28 11
29 28
30 1
31 2
32 30
33 27
34 5
35 17
36 19
37 13
38 27
39 13
40 36
41 26
42 21
43 14
44 42
45 29
46 45
47 18
48 8
49 34
50 9
51 21
52 34
53 50
54 39
5...

output:

9874396013

result:

ok 1 number(s): "9874396013"

Test #41:

score: 30
Accepted
time: 300ms
memory: 62120kb

input:

100000 3000
2 1
3 2
4 3
5 1
6 1
7 3
8 6
9 7
10 5
11 7
12 10
13 4
14 13
15 12
16 14
17 9
18 5
19 9
20 16
21 16
22 12
23 5
24 5
25 20
26 14
27 10
28 15
29 4
30 16
31 29
32 1
33 8
34 13
35 12
36 19
37 33
38 33
39 24
40 16
41 38
42 27
43 23
44 10
45 9
46 11
47 26
48 21
49 16
50 24
51 11
52 35
53 36
54 1...

output:

9993648157

result:

ok 1 number(s): "9993648157"

Test #42:

score: 30
Accepted
time: 268ms
memory: 60500kb

input:

100000 50000
2 1
3 1
4 2
5 4
6 5
7 1
8 2
9 8
10 5
11 10
12 10
13 9
14 2
15 2
16 15
17 12
18 2
19 2
20 3
21 7
22 11
23 7
24 2
25 11
26 5
27 25
28 20
29 17
30 27
31 11
32 18
33 14
34 8
35 23
36 8
37 33
38 30
39 7
40 39
41 3
42 38
43 4
44 19
45 2
46 13
47 26
48 4
49 20
50 32
51 39
52 6
53 40
54 11
55 3...

output:

9999784987

result:

ok 1 number(s): "9999784987"

Test #43:

score: 30
Accepted
time: 280ms
memory: 60196kb

input:

99998 99999
2 1
3 1
4 3
5 2
6 3
7 3
8 1
9 1
10 7
11 5
12 3
13 8
14 11
15 14
16 10
17 3
18 17
19 13
20 17
21 8
22 12
23 19
24 9
25 14
26 13
27 9
28 5
29 22
30 3
31 17
32 28
33 4
34 14
35 10
36 7
37 3
38 26
39 34
40 29
41 4
42 10
43 41
44 33
45 39
46 25
47 3
48 11
49 18
50 9
51 40
52 33
53 4
54 8
55 2...

output:

9999471735

result:

ok 1 number(s): "9999471735"