UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213511#573. t2laiyishi05ms5948kbC++11938b2024-11-12 19:20:312024-11-12 23:09:47

answer

#include<bits/stdc++.h>
using namespace std;
int n,q;
struct edge{
	int u,v,w;
}e[100005];
int vis[100005];
vector<int>g[100005];
vector<int>d[100005];
int dfs(int u,int fa){
	vis[u]=1;
	int sum=1;
	for(int v:g[u]){
		if(v==fa)continue;
		sum+=dfs(v,u);
	} 
	return sum;
}
void solve(int i){
	long long ans=0;
	for(int x:d[i]){
		g[e[x].u].push_back(e[x].v);
		g[e[x].v].push_back(e[x].u);
	}
	for(int j=1;j<=n;j++){
		if(!vis[j]){
			int sz=dfs(j,-1);
			ans+=1ll*sz*(sz-1)/2;
		}
	}
	printf("%lld\n",ans);
	for(int x:d[i]){
		g[e[x].u].clear(),g[e[x].v].clear();
		vis[e[x].u]=0,vis[e[x].v]=0;
	}
}
int main(){
	scanf("%d%d",&n,&q);
	for(int i=1;i<n;i++){
		scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
		for(int j=1;j<=e[i].w/j;j++){
			if(e[i].w%j==0){
				d[j].push_back(i);
				if(j!=e[i].w/j)d[e[i].w/j].push_back(i);
			}
		}
	}
	while(q--){
		int d;scanf("%d",&d);
		solve(d);
	}
} 

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 5ms
memory: 5948kb

input:

50 50
48 29 49788
47 48 31142
35 48 28665
10 35 23889
39 35 6411
50 39 66666
43 35 27629
46 10 49173...

output:

2
0
0
0
0
0
0
0
0
0
0
0
0
3
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

wrong answer 2nd words differ - expected: '1', found: '0'

Subtask #2:

score: 0
Time Limit Exceeded

Test #11:

score: 0
Time Limit Exceeded

input:

100000 100000
73595 40695 76
13615 40695 96
65545 13615 84
19391 13615 76
2353 73595 27
26730 40695 ...

output:

12815
352
81
16340
618502
17993
1921
134
1006
398
52
33
132
3008
410
450400
17993
422
34
4999950000
...

result:


Subtask #3:

score: 0
Time Limit Exceeded

Test #21:

score: 0
Time Limit Exceeded

input:

100000 100000
73595 40695 12816
13615 73595 81821
65545 40695 75866
19391 65545 1165
2353 73595 3737...

output:

8
2
38659
1
0
160
436
5642
1
0
0
0
0
0
160
0
0
2
0
1
373
553
482
526
8504
3
0
0
0
0
0
0
0
1
0
0
0
0
...

result: