106312024-04-07 14:24:23111Varázserdőcpp17Wrong answer 7/1003.368s504188 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);
	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
1Accepted3ms1892 KiB
2Accepted3ms2088 KiB
3Accepted3ms2304 KiB
subtask27/7
4Accepted3ms2432 KiB
5Accepted4ms3516 KiB
6Accepted17ms11492 KiB
7Accepted17ms12248 KiB
8Accepted1.263s470488 KiB
9Accepted994ms469444 KiB
10Accepted1.12s467884 KiB
11Accepted865ms469716 KiB
12Accepted975ms475048 KiB
13Accepted964ms477812 KiB
subtask30/9
14Wrong answer3ms49264 KiB
15Wrong answer4ms49920 KiB
16Wrong answer83ms82632 KiB
17Wrong answer823ms202528 KiB
18Wrong answer3.368s504188 KiB
19Wrong answer1.656s413636 KiB
20Wrong answer1.634s434424 KiB
subtask40/14
21Wrong answer3ms45220 KiB
22Wrong answer3ms45496 KiB
23Wrong answer3ms45364 KiB
24Wrong answer3ms45368 KiB
subtask50/20
25Wrong answer4ms46368 KiB
26Wrong answer8ms48280 KiB
27Wrong answer8ms47748 KiB
28Wrong answer8ms48580 KiB
29Wrong answer8ms48412 KiB
30Wrong answer8ms48264 KiB
31Wrong answer8ms48156 KiB
32Wrong answer8ms48048 KiB
subtask60/50
33Wrong answer19ms54032 KiB
34Wrong answer79ms79996 KiB
35Wrong answer870ms198796 KiB
36Wrong answer879ms243672 KiB
37Wrong answer2.02s475036 KiB
38Wrong answer2.198s485692 KiB
39Wrong answer2.568s501688 KiB
40Wrong answer2.94s495164 KiB
41Wrong answer1.662s284444 KiB