106332024-04-07 14:28:12111Varázserdőcpp17Wrong answer 7/1003.836s504308 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;
		}
	};
	for(int i=1;i<=N;i++){
		if(v[i]){
			continue;
		}
		v[i]=1;
		dfs(dfs,i);
	}
	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);
	for(int i=1;i<=N;i++){
		if(v[i]){
			continue;
		}
		v[i]=1;
		dfs2(dfs2,i);
	}
	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
2Accepted3ms2124 KiB
3Accepted3ms2348 KiB
subtask27/7
4Accepted3ms2724 KiB
5Accepted4ms3684 KiB
6Accepted16ms11792 KiB
7Accepted18ms12604 KiB
8Accepted1.253s470932 KiB
9Accepted981ms469776 KiB
10Accepted1.118s467956 KiB
11Accepted852ms469792 KiB
12Accepted967ms475088 KiB
13Accepted963ms477812 KiB
subtask30/9
14Wrong answer3ms49292 KiB
15Wrong answer4ms50200 KiB
16Wrong answer215ms120684 KiB
17Wrong answer1.062s202624 KiB
18Wrong answer3.836s504308 KiB
19Wrong answer2.032s402584 KiB
20Wrong answer2.118s411844 KiB
subtask40/14
21Wrong answer3ms22760 KiB
22Wrong answer3ms22996 KiB
23Wrong answer3ms23080 KiB
24Wrong answer3ms23068 KiB
subtask50/20
25Wrong answer4ms23924 KiB
26Wrong answer8ms25824 KiB
27Wrong answer8ms24968 KiB
28Wrong answer8ms25676 KiB
29Wrong answer8ms25532 KiB
30Wrong answer8ms25360 KiB
31Wrong answer8ms24796 KiB
32Wrong answer8ms24900 KiB
subtask60/50
33Wrong answer20ms30784 KiB
34Wrong answer321ms101104 KiB
35Wrong answer866ms162564 KiB
36Wrong answer913ms195188 KiB
37Wrong answer2.601s446864 KiB
38Wrong answer2.197s468404 KiB
39Wrong answer3.368s500580 KiB
40Wrong answer3.124s486872 KiB
41Wrong answer1.664s262572 KiB