QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#817197#7009. Rikka with Intersections of PathsSGColinAC ✓1953ms69120kbC++201.9kb2024-12-16 20:52:302024-12-16 20:52:31

Judging History

This is the latest submission verdict.

  • [2024-12-16 20:52:31]
  • Judged
  • Verdict: AC
  • Time: 1953ms
  • Memory: 69120kb
  • [2024-12-16 20:52:30]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+10;
const int mod=1e9+7;
vector<int>G[maxn];
int n,m,k,cnt1[maxn],cnt2[maxn],p[maxn][20],dep[maxn];
int ans;

int fac[maxn], ifac[maxn];

inline int fpow(int x, int t = mod - 2) {
	int res = 1;
	for (; t; t >>= 1, x = 1ll * x * x % mod)
		if (t & 1) res = 1ll * res * x % mod;
	return res;
}
int C(int n, int m) {
	if (n < m) return 0;
	return 1ll * fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
void dfs(int u,int fa)
{
	p[u][0]=fa;
	dep[u]=dep[fa]+1;
	for(auto v:G[u])
	{
		if(v==fa) continue;
		dfs(v,u);
	}
}
void get_ans(int u,int fa)
{
	for(auto v:G[u])
	{
		if(v==fa) continue;
		get_ans(v,u);
		cnt1[u]+=cnt1[v];
		cnt2[u]+=cnt2[v];
	}
	if(cnt1[u]>=k) ans=(1ll*ans+C(cnt1[u],k))%mod;
	if(cnt2[u]>=k) ans=(1ll*ans-C(cnt2[u],k)+mod)%mod;
}
int lca(int x,int y)
{
	if(dep[x]<dep[y])
		swap(x,y);
	int d=dep[x]-dep[y];
	for(int i=19;i>=0;i--)
		if(d&(1<<i))
			x=p[x][i];
	if(x==y) return x;
	for(int i=19;i>=0;i--)
		if(p[x][i]!=p[y][i])
			x=p[x][i],y=p[y][i];
	return p[x][0];
}
int main()
{
	fac[0] = ifac[0] = 1;
	for (int i = 1; i < maxn; ++i) fac[i] = 1ll * fac[i - 1] * i % mod;
	ifac[maxn - 1] = fpow(fac[maxn - 1]);
	for (int i = maxn - 2; i; --i) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % mod;
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&n,&m,&k);
		for(int i=1;i<=n;i++)
		{
			cnt1[i]=cnt2[i]=0;
			G[i].clear();
		}
		for(int i=1,u,v;i<n;i++)
		{
			scanf("%d%d",&u,&v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		dfs(1,0);
		for(int j=1;j<20;j++)
			for(int i=1;i<=n;i++)
				p[i][j]=p[p[i][j-1]][j-1];
		for(int i=1,x,y;i<=m;i++)
		{
			scanf("%d%d",&x,&y);
			int l=lca(x,y);
			cnt1[l]--;
			cnt1[p[l][0]]--;
			cnt1[x]++;
			cnt1[y]++;
			
			cnt2[l]-=2;
			cnt2[x]++;
			cnt2[y]++;
		}
		ans=0;
		get_ans(1,0);
		printf("%d\n",ans);
	}
	return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 5ms
memory: 11740kb

input:

1
3 6 2
1 2
1 3
1 1
2 2
3 3
1 2
1 3
2 3

output:

10

result:

ok single line: '10'

Test #2:

score: 0
Accepted
time: 1599ms
memory: 52792kb

input:

108
2000 2000 52
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
...

output:

887021006
410694359
325103243
670233279
72813801
947351730
883070706
311794222
998954559
996232156
569968667
505502006
778426774
220584560
246359125
260714580
11417533
351222718
490813635
444958907
207271238
791034394
734465853
472937949
826448869
646757384
776063725
826971663
959125943
459469914
30...

result:

ok 108 lines

Test #3:

score: 0
Accepted
time: 1953ms
memory: 69120kb

input:

6
300000 300000 43
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 5...

output:

690220121
895677607
370155943
510259168
589689421
548994023

result:

ok 6 lines