zoukankan      html  css  js  c++  java
  • 蓝桥杯 国王的烦恼

    思路:

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

    实现:

     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
    ", cnt);
    47     return 0;
    48 }
  • 相关阅读:
    C# 文件类的操作---删除
    C#实现Zip压缩解压实例
    UVALIVE 2431 Binary Stirling Numbers
    UVA 10570 meeting with aliens
    UVA 306 Cipher
    UVA 10994 Simple Addition
    UVA 696 How Many Knights
    UVA 10205 Stack 'em Up
    UVA 11125 Arrange Some Marbles
    UVA 10912 Simple Minded Hashing
  • 原文地址:https://www.cnblogs.com/wangyiming/p/8659242.html
Copyright © 2011-2022 走看看