QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#656390#7009. Rikka with Intersections of PathsAshbourneAC ✓2214ms99152kbC++202.3kb2024-10-19 12:45:462024-10-19 12:45:47

Judging History

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

  • [2024-10-19 12:45:47]
  • 评测
  • 测评结果:AC
  • 用时:2214ms
  • 内存:99152kb
  • [2024-10-19 12:45:46]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
const int N = 3e5 + 10;
const int Mod = 1e9 + 7;
using namespace std;
vector<int>G[N];
int rev[N][20], dep[N]; 
void dfs (int u, int fa) {
        dep[u] = dep[fa] + 1;
        rev[u][0] = fa;
        for (int i = 1; i <= 19; i ++ ) {
                rev[u][i] = rev[rev[u][i - 1]][i - 1];
        }
        for(auto v: G[u]){
                if(v == fa) continue;
                dfs(v, u);
        }
}
int LCA (int x, int y) {
        if (dep[x] < dep[y]) swap (x, y);
        for (int i = 19; i >= 0; i -- ) {
                if (dep[rev[x][i]] >= dep[y]){
                        x = rev[x][i];
                }
        }
        if (x == y) return x;
        for (int i = 19; i >= 0; i -- ) {
                if (rev[x][i] != rev[y][i]) {
                        x = rev[x][i];
                        y = rev[y][i];
                }
        }
        return rev[x][0];
}
int n, m, k;
int fac[N], nfac[N];
int fpow(int a, int b){
	int x = 1;
	while(b){
		if(b & 1) x = x * a % Mod;
		a = a * a % Mod;
		b >>= 1;
	}
	return x;
}
void init(int num){
	fac[0] = 1; 
	for(int i = 1; i <= num; ++ i) fac[i] = fac[i - 1] * i % Mod;
	nfac[num] = fpow(fac[num], Mod - 2);
	for(int i = num - 1; i >= 0; -- i) nfac[i] = nfac[i + 1] * (i + 1) % Mod;
}
int C(int n, int m){
	if(m > n) return 0;
	return fac[n] * nfac[m] % Mod * nfac[n - m] % Mod;
}
int sum[N], ans, zdd[N];
void clear(){
	for(int i = 1; i <= n; ++ i) G[i].clear();
	for(int i = 0; i <= n; ++ i) sum[i] = 0, zdd[i] = 0;
	ans = 0;
}
void calc(int x, int fa){
	for(auto y: G[x]){
		if(y == fa) continue;
		calc(y, x);
		sum[x] += sum[y];
	}
	ans += (C(sum[x], k) - C(sum[x] - zdd[x], k) + Mod) % Mod;
	// cout << x << " " << sum[x] << " " << zdd[x] << endl;
	ans %= Mod;
}
void solve(){
	cin >> n >> m >> k;
	clear();
	for(int i = 1; i <= n - 1; ++ i){
		int x, y; cin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	dfs(1, 0);
	for(int i = 1; i <= m; ++ i){
		int x, y;
		cin >> x >> y;
		sum[x] ++; sum[y] ++;
		int ft = LCA(x, y);
		sum[ft] --;	sum[rev[ft][0]]--;
		zdd[ft] ++;
	}
	calc(1, 0);
	cout << ans << endl;
}
signed main(){
	ios::sync_with_stdio(0);
	init(3e5);
	int T; cin >> T;
	while(T--) solve();
}

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

詳細信息

Test #1:

score: 100
Accepted
time: 6ms
memory: 15948kb

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: 2009ms
memory: 85944kb

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: 2214ms
memory: 99152kb

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