106322024-04-07 14:26:58111Varázserdőcpp17Runtime error 7/1002.164s478788 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

#define MOD 1000000007

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,M;
	cin>>N>>M;
	vector<vector<pair<int,int>>>g(N+1);
	vector<tuple<int,int,int>>e(M);
	for(int i=0;i<M;i++){
		int a,b,c;
		cin>>a>>b>>c;
		e[i]={c,a,b};
	}
	sort(e.rbegin(),e.rend());
	for(int i=0;i<M;i++){
		auto[c,a,b]=e[i];
		g[a].emplace_back(b,c);
		g[b].emplace_back(a,c);
	}
	vector<int>v(N+1);
	vector<unordered_map<int,int>>w(N+1);
	auto dfs=[&](auto self,int i)->void{
		for(auto[j,c]:g[i]){
			if(v[j]){
				continue;
			}
			v[j]=v[i]+1;
			self(self,j);
			w[i][c]+=w[j][c+1]+1;
			w[i][c]%=MOD;
		}
	};
	v[1]=1;
	dfs(dfs,1);
	if(count(v.begin(),v.end(),0)!=1)exit(1);
	auto dfs2=[&](auto self,int i)->void{
		for(auto[j,c]:g[i]){
			if(v[j]){
				w[i][c]+=w[j][c+1]+1;
				w[i][c]%=MOD;
				continue;
			}
			v[j]=v[i]+1;
			self(self,j);
		}
	};
	fill(v.begin(),v.end(),0);
	v[1]=1;
	dfs2(dfs2,1);
	int ans=0;
	for(int i=1;i<=N;i++){
		for(auto[x,y]:w[i]){
			ans+=y;
			ans%=MOD;
		}
	}
	ans-=M;
	cout<<ans<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1888 KiB
2Accepted3ms2020 KiB
3Accepted3ms2104 KiB
subtask27/7
4Accepted3ms2408 KiB
5Accepted4ms3400 KiB
6Accepted17ms11652 KiB
7Accepted17ms12092 KiB
8Accepted1.259s470484 KiB
9Accepted1.202s468952 KiB
10Accepted1.111s467592 KiB
11Accepted1.024s469176 KiB
12Accepted972ms474656 KiB
13Accepted968ms477816 KiB
subtask30/9
14Runtime error3ms49224 KiB
15Runtime error4ms50104 KiB
16Runtime error75ms83076 KiB
17Runtime error479ms189600 KiB
18Runtime error1.547s478788 KiB
19Runtime error1.041s401832 KiB
20Runtime error1.027s420396 KiB
subtask40/14
21Wrong answer3ms70356 KiB
22Wrong answer3ms70364 KiB
23Wrong answer3ms70344 KiB
24Runtime error3ms70348 KiB
subtask50/20
25Runtime error4ms71028 KiB
26Wrong answer8ms72848 KiB
27Wrong answer8ms72220 KiB
28Wrong answer8ms73004 KiB
29Runtime error6ms72156 KiB
30Runtime error6ms72140 KiB
31Wrong answer8ms72728 KiB
32Wrong answer8ms72732 KiB
subtask60/50
33Runtime error13ms77444 KiB
34Runtime error78ms104868 KiB
35Wrong answer878ms223436 KiB
36Wrong answer930ms267960 KiB
37Runtime error1.098s436748 KiB
38Runtime error1.103s437572 KiB
39Runtime error1.09s439916 KiB
40Runtime error1.092s443324 KiB
41Wrong answer2.164s322888 KiB