Language: C++

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		var h=sc.La2;
		int n=s[0],m=s[1];
		var a=new int[m][];
		for(int i = 0;i<m;i++) {a[i]=sc.Ia;}
		Array.Sort(a,(u,v)=>u[2]-v[2]);
		Uf uf=new Uf(n+1);
		var d=new List<int>[n-1];
		var g=new int[m];
		var z=new long[n-1];
		for(int i = 0,j=-1;i<m;i++) {
			var p1=uf.Root(a[i][0]);
			var p2=uf.Root(a[i][1]);
			if(uf.Union(p1,p2)){
				j++;
				d[j]=new List<int>(uf.li[uf.Root(p1)]);
				z[j]=h[uf.Root(p1)]=h[p1]+h[p2];
			}
			g[i]=j;
		}
		var b=new bool[n+1];
		int ans=0;
		for(int i = m-1;i>=0;i--) {
			if(b[a[i][0]]){continue;}
			if(a[i][2]<=z[g[i]]){
				for(int j = 0;j<d[g[i]].Count;j++) {b[d[g[i]][j]]=true;}
			}
			else{ans++;}
		}
		Console.WriteLine("{0}",ans);
	}
}
public class Uf{
	private int[] arr,hi;
	public List<int>[] li;
	public int all;
	public Uf(int n){
		arr=new int[n];
		hi=new int[n];
		li=new List<int>[n];
		all=n;
		for(int i=0;i<n;i++){
			arr[i]=-1;
			li[i]=new List<int>();li[i].Add(i);
		}
	}
	public bool Union(int a,int b){
		a=Root(a);
		b=Root(b);
		if(a!=b){
			if(hi[a]<hi[b]){
				int v=b;
				b=a;
				a=v;
			}
			arr[a]+=arr[b];
			hi[a]=Math.Max(hi[a],hi[b]+1);
			arr[b]=a;
			all--;
			li[a].AddRange(li[b]);
			return true;
		}
		return false;
	}
	public bool Same(int a,int b){return Root(a)==Root(b);}
	public int Size(int a){return -arr[Root(a)];}
	public int Root(int a){return arr[a]<0?a:arr[a]=Root(arr[a]);}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),double.Parse);}
	public double[] Da3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}

Compiler output:
a.cc:1:7: error: expected nested-name-specifier before 'System'
    1 | using System;
      |       ^~~~~~
a.cc:2:7: error: expected nested-name-specifier before 'System'
    2 | using System.Collections.Generic;
      |       ^~~~~~
a.cc:3:7: error: expected nested-name-specifier before 'System'
    3 | using System.Collections;
      |       ^~~~~~
a.cc:4:7: error: expected nested-name-specifier before 'System'
    4 | using System.Collections.Specialized;
      |       ^~~~~~
a.cc:5:7: error: expected nested-name-specifier before 'System'
    5 | using System.Linq;
      |       ^~~~~~
a.cc:6:7: error: expected nested-name-specifier before 'System'
    6 | using System.Text;
      |       ^~~~~~
a.cc:7:7: error: expected nested-name-specifier before 'System'
    7 | using System.IO;
      |       ^~~~~~
a.cc:8:7: error: expected nested-name-specifier before 'System'
    8 | using System.Reflection;
      |       ^~~~~~
a.cc:9:7: error: expected nested-name-specifier before 'static'
    9 | using static System.Math;
      |       ^~~~~~
a.cc:10:7: error: expected nested-name-specifier before 'System'
   10 | using System.Numerics;
      |       ^~~~~~
a.cc:46:2: error: expected ';' after class definition
   46 | }
      |  ^
      |  ;
a.cc: In static member function 'static void Program::Main()':
a.cc:14:17: error: 'Sc' was not declared in this scope
   14 |                 Sc sc=new Sc();
      |                 ^~
a.cc:15:17: error: 'var' was not declared in this scope
   15 |                 var s=sc.Ia;
      |                 ^~~
a.cc:16:21: error: expected ';' before 'h'
   16 |                 var h=sc.La2;
      |                     ^
a.cc:17:23: error: 's' was not declared in this scope
   17 |                 int n=s[0],m=s[1];
      |                       ^
a.cc:18:21: error: expected ';' before 'a'
   18 |                 var a=new int[m][];
      |                     ^
a.cc:19:33: error: 'm' was not declared in this scope
   19 |                 for(int i = 0;i<m;i++) {a[i]=sc.Ia;}
      |                                 ^
a.cc:19:41: error: 'a' was not declared in this scope
   19 |                 for(int i = 0;i<m;i++) {a[i]=sc.Ia;}
      |                                         ^
a.cc:19:46: error: 'sc' was not declared in this scope
   19 |                 for(int i = 0;i<m;i++) {a[i]=sc.Ia;}
      |                                              ^~
a.cc:20:17: error: 'Array' was not declared in this scope
   20 |                 Array.Sort(a,(u,v)=>u[2]-v[2]);
      |                 ^~~~~
a.cc:20:28: error: 'a' was not declared in this scope
   20 |                 Array.Sort(a,(u,v)=>u[2]-v[2]);
      |                            ^
a.cc:20:31: error: 'u' was not declared in this scope
   20 |                 Array.Sort(a,(u,v)=>u[2]-v[2]);
      |                               ^
a.cc:20:33: error: 'v' was not declared in this scope
   20 |                 Array.Sort(a,(u,v)=>u[2]-v[2]);
      |                                 ^
a.cc:20:36: error: expected primary-expression before '>' token
   20 |                 Array.Sort(a,(u,v)=>u[2]-v[2]);
      |                                    ^
a.cc:21:17: error: 'Uf' was not declared in this scope
   21 |                 Uf uf=new Uf(n+1);
      |                 ^~
a.cc:22:21: error: expected ';' before 'd'
   22 |                 var d=new List<int>[n-1];
      |                     ^
a.cc:23:21: error: expected ';' before 'g'
   23 |                 var g=new int[m];
      |                     ^
a.cc:24:21: error: expected ';' before 'z'
   24 |                 var z=new long[n-1];
      |                     ^
a.cc:25:38: error: 'm' was not declared in this scope
   25 |                 for(int i = 0,j=-1;i<m;i++) {
      |                                      ^
a.cc:26:29: error: expected ';' before 'p1'
   26 |                         var p1=uf.Root(a[i][0]);
      |                             ^~
a.cc:27:29: error: expected ';' before 'p2'
   27 |                         var p2=uf.Root(a[i][1]);
      |                             ^~
a.cc:28:28: error: 'uf' was not declared in this scope
   28 |                         if(uf.Union(p1,p2)){
      |                            ^~
a.cc:28:37: error: 'p1' was not declared in this scope
   28 |                         if(uf.Union(p1,p2)){
      |                                     ^~
a.cc:28:40: error: 'p2' was not declared in this scope
   28 |                         if(uf.Union(p1,p2)){
      |                                        ^~
a.cc:30:33: error: 'd' was not declared in this scope
   30 |                                 d[j]=new List<int>(uf.li[uf.Root(p1)]);
      |                                 ^
a.cc:30:42: error: 'List' does not name a type
   30 |                                 d[j]=new List<int>(uf.li[uf.Root(p1)]);
      |                                          ^~~~
a.cc:30:47: error: expected primary-expression before 'int'
   30 |                                 d[j]=new List<int>(uf.li[uf.Root(p1)]);
      |                                               ^~~
a.cc:31:33: error: 'z' was not declared in this scope
   31 |                                 z[j]=h[uf.Root(p1)]=h[p1]+h[p2];
      |                                 ^
a.cc:31:38: error: 'h' was not declared in this scope
   31 |                                 z[j]=h[uf.Root(p1)]=h[p1]+h[p2];
      |                                      ^
a.cc:33:25: error: 'g' was not declared in this scope
   33 |                         g[i]=j;
      |                         ^
a.cc:35:21: error: expected ';' before 'b'
   35 |                 var b=new bool[n+1];
      |                     ^
a.cc:37:29: error: 'm' was not declared in this scope
   37 |                 for(int i = m-1;i>=0;i--) {
      |                             ^
a.cc:38:28: error: 'b' was not declared in this scope
   38 |                         if(b[a[i][0]]){continue;}
      |                            ^
a.cc:39:37: error: 'z' was not declared in this scope
   39 |                         if(a[i][2]<=z[g[i]]){
      |                                     ^
a.cc:39:39: error: 'g' was not declared in this scope
   39 |                         if(a[i][2]<=z[g[i]]){
      |                                       ^
a.cc:40:49: error: 'd' was not declared in this scope
   40 |                                 for(int j = 0;j<d[g[i]].Count;j++) {b[d[g[i]][j]]=true;}
      |                                                 ^
a.cc:40:69: error: 'b' was not declared in this scope
   40 |                                 for(int j = 0;j<d[g[i]].Count;j++) {b[d[g[i]][j]]=true;}
      |                                                                     ^
a.cc:44:17: error: 'Console' was not declared in this scope
   44 |                 Console.WriteLine("{0}",ans);
      |                 ^~~~~~~
a.cc: At global scope:
a.cc:11:1: error: a storage class can only be specified for objects and functions
   11 | static class Program{
      | ^~~~~~
a.cc:47:1: error: expected unqualified-id before 'public'
   47 | public class Uf{
      | ^~~~~~
a.cc:83:1: error: expected unqualified-id before 'public'
   83 | public class Sc{
      | ^~~~~~