QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#515909#7009. Rikka with Intersections of Pathsqsc114AC ✓889ms52632kbC++111.7kb2024-08-12 11:23:062024-08-12 11:23:06

Judging History

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

  • [2024-08-12 11:23:06]
  • 评测
  • 测评结果:AC
  • 用时:889ms
  • 内存:52632kb
  • [2024-08-12 11:23:06]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int MAXN=3e5+5,MOD=1e9+7;
#define ll long long
int n,m,k,siz[MAXN],son[MAXN],dep[MAXN],fa[MAXN];
int dfn[MAXN],rnk[MAXN],top[MAXN],cnt;
int s[MAXN],lc[MAXN];
ll fac[MAXN],ans;
vector<int> ve[MAXN];
void dfs(int o,int pa)
{
	fa[o]=pa;
	siz[o]=1;dep[o]=dep[fa[o]]+1;
	for(auto u:ve[o])
	{
		if(u==pa)continue;
		dfs(u,o);siz[o]+=siz[u];
		if(siz[u]>siz[son[o]])son[o]=u;
	}
}
void dfs1(int o,int tp)
{
	top[o]=tp;dfn[o]=++cnt;rnk[cnt]=o;
	if(son[o])dfs1(son[o],tp);
	for(auto u:ve[o])
	{
		if(u==son[o]||u==fa[o])continue;
		dfs1(u,u);
	}
}
int lca(int x,int y)
{
	while(top[x]^top[y])
	{
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		x=fa[top[x]];
	}
	if(dep[x]>dep[y])swap(x,y);
	return x;
}
ll qpow(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)ans*=a,ans%=MOD;
		a*=a,a%=MOD;b>>=1;
	}
	return ans;
}
ll cbn(ll x,ll y)
{
	if(y>x)return 0;
	return fac[x]*qpow(fac[y]*fac[x-y]%MOD,MOD-2)%MOD;
}
void calc(int o)
{
	for(auto u:ve[o])
	{
		if(u==fa[o])continue;
		calc(u);s[o]+=s[u];
	}
	ans+=cbn(s[o],k)-cbn(s[o]-lc[o],k);ans%=MOD;
}
int main()
{
	int T;cin>>T;fac[0]=1;
	for(int i=1;i<=3e5;i++)fac[i]=fac[i-1]*i%MOD;
	while(T--)
	{
		cin>>n>>m>>k;
		for(int i=1;i<n;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			ve[u].push_back(v);
			ve[v].push_back(u);
		}
		dfs(1,1);
		dfs1(1,1);fa[1]=0;
		for(int i=1;i<=m;i++)
		{
			int u,v,c;
			scanf("%d%d",&u,&v);c=lca(u,v);
			s[u]++,s[v]++;s[c]--,s[fa[c]]--;
			lc[c]++;
		}
		calc(1);
		cout<<(ans+MOD)%MOD<<endl;
		for(int i=1;i<=n;i++)ve[i].clear(),s[i]=lc[i]=0;
		for(int i=1;i<=n;i++)
			son[i]=0;cnt=ans=0;
	}
	return 0;
}

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

详细

Test #1:

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

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: 860ms
memory: 36216kb

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: 889ms
memory: 52632kb

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