思路:

倒过来用并查集。注意天数不要重复计算。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN = 10005;
 4 const int MAXM = 100005;
 5 int par[MAXN], n, m;
 6 void init(int n) 
 7 { 
 8     for (int i = 1; i <= n; i++) par[i] = i; 
 9 }
10 int find(int x) 
11 { 
12     if (par[x] == x) return x;
13     return par[x] = find(par[x]);
14 }
15 void uni(int x, int y)
16 {
17     x = find(x); y = find(y);
18     if (x != y) par[x] = y;
19 }
20 bool same(int x, int y)
21 {
22     return find(x) == find(y);
23 }
24 struct node
25 {
26     int x, y, t;
27 };
28 node a[MAXM];
29 bool cmp(node & a, node & b) { return a.t > b.t; }
30 int main()
31 {
32     scanf("%d %d", &n, &m);
33     init(n);
34     for (int i = 0; i < m; i++) scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].t);
35     sort(a, a + m, cmp); 
36     int cnt = 0, last = -1; 
37     for (int i = 0; i < m; i++) 
38     {
39         if (!same(a[i].x, a[i].y) && a[i].t != last)
40         {
41             cnt++;
42             last = a[i].t;
43         }
44         uni(a[i].x, a[i].y);
45     }
46     printf("%d\n", cnt);
47     return 0;
48 }

 

相关文章:

  • 2022-12-23
  • 2021-12-18
  • 2021-07-13
  • 2022-12-23
  • 2022-12-23
  • 2022-01-14
  • 2021-11-13
猜你喜欢
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
  • 2021-03-28
  • 2021-08-26
  • 2022-12-23
相关资源
相似解决方案