fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define ld long double
  6. #define pb push_back
  7. #define pii pair<int, int>
  8. #define pll pair<ll, ll>
  9. #define vvi vector<vector<int>>
  10. #define vt vector
  11. #define arr array
  12. #define ALL(x) begin(x), end(x)
  13. #define rALL(x) rbegin(x), rend(x)
  14. #define SZ(x) x.size()
  15. #define P(x, y) make_pair(x, y)
  16. const int MOD1=998244353;
  17. const ll MOD2=1e9+7;
  18. const ll LINF=3e18;
  19. const int INF=1e9;
  20.  
  21. const int N=1e5;
  22. vt<ll> p(N + 1), sz(N + 1, 1);
  23.  
  24. ll find(ll x){
  25. if (x == p[x]) return x;
  26. return p[x] = find(p[x]);
  27. }
  28.  
  29. ll merge(ll x, ll y){
  30. x = find(x), y = find(y);
  31. if (x == y) return 0;
  32.  
  33. if (sz[x] < sz[y]) swap(x, y);
  34. sz[x] += sz[y];
  35. p[y] = x;
  36. return 1;
  37. }
  38.  
  39. void solve(){
  40. ll n, m;
  41. cin >> n >> m;
  42. vt<arr<ll, 3>> e(m);
  43. for (auto &[u, v, w] : e) cin >> u >> v >> w;
  44. iota(ALL(p), 0);
  45.  
  46. sort(ALL(e), [](arr<ll, 3> &a, arr<ll, 3> &b){
  47. return a[2] < b[2];
  48. });
  49.  
  50. ll ans = 0;
  51. for (int i = 0; i < m; ){
  52. ll k = e[i][2];
  53. vt<ll> idx;
  54. while(i < m && e[i][2] == k){
  55. if (find(e[i][0]) == find(e[i][1])){
  56. i++;
  57. continue;
  58. }
  59. idx.pb(i++);
  60. }
  61. for (auto x : idx){
  62. if (merge(e[x][0], e[x][1])) continue;
  63. ans++;
  64. }
  65. }
  66. cout << ans << '\n';
  67. }
  68.  
  69. int main(){
  70. ios::sync_with_stdio(0);
  71. cin.tie(0); cout.tie(0);
  72.  
  73. solve();
  74.  
  75. return 0;
  76. }
Success #stdin #stdout 0.01s 5292KB
stdin
6 10
1 2 3
1 3 3
2 3 2
2 4 5
2 5 2
3 4 4
3 5 1
3 6 1
4 5 4
5 6 1
stdout
4