zoukankan      html  css  js  c++  java
  • 18.12.17 DSA/POJ 1679 The Unique MST(Kruskal)

    描述

    Given a connected undirected graph, tell if its minimum spanning tree is unique.

    Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:
    1. V' = V.
    2. T is connected and acyclic.

    Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'.
    输入

    The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.输出For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.样例输入

    2
    3 3
    1 2 1
    2 3 2
    3 1 3
    4 4
    1 2 2
    2 3 2
    3 4 2
    4 1 2
    

    样例输出

    3
    Not Unique!
    

    来源

    POJ Monthly--2004.06.27 srbga@POJ

     1 #include <iostream>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <stack>
     5 #include <string>
     6 #include <math.h>
     7 #include <queue>
     8 #include <stdio.h>
     9 #include <string.h>
    10 #include <set>
    11 #include <vector>
    12 #include <fstream>
    13 #define maxn 10005
    14 #define inf 999999
    15 #define cha 127
    16 using namespace std;
    17 
    18 struct node {
    19     int x, y, weight;
    20     bool valid;
    21     node() {
    22         valid = true;
    23     }
    24 }all[maxn];
    25 bool operator<(node a, node b) {
    26     return a.weight < b.weight;
    27 }
    28 int p[105], n, m;
    29 
    30 int parent(int x) {
    31     if (p[x] == x)return x;
    32     p[x] = parent(p[x]);
    33     return p[x];
    34 }
    35 
    36 void merge(int x, int y) {
    37     int px = parent(x), py = parent(y);
    38     p[px] = py;
    39 }
    40 
    41 vector<int>q;
    42 
    43 int MST(int ifq) {
    44     int ans=0,num=n;
    45     for (int i = 1; i <= n; i++)
    46         p[i] = i;
    47     for (int i = 1; i <= m; i++) {
    48         int x = all[i].x, y = all[i].y, val = all[i].weight;
    49         if (all[i].valid&&parent(x) != parent(y)) {
    50             merge(x, y);
    51             ans += val;
    52             num--;
    53             if (ifq)
    54                 q.push_back(i);
    55         }
    56         if (num==1)
    57             break;
    58     }
    59     if (num != 1)
    60         return -1;
    61     return ans;
    62 }
    63 
    64 void solve() {
    65     int ans = MST(1), size = q.size();
    66     for (int i = 0; i < size; i++) {
    67         all[q[i]].valid = false;
    68         int tmp=MST(0);
    69         all[q[i]].valid = true;
    70         if (tmp == ans) {
    71             printf("Not Unique!
    ");
    72             return;
    73         }
    74     }
    75     printf("%d
    ", ans);
    76 }
    77 
    78 void init() {
    79     q.clear();
    80     scanf("%d%d", &n, &m);
    81     for (int i = 1; i <= m; i++) 
    82         scanf("%d%d%d", &all[i].x, &all[i].y, &all[i].weight);
    83     sort(all + 1, all + 1 + m);
    84     solve();
    85 }
    86 
    87 int main() {
    88     int kase;
    89     scanf("%d", &kase);
    90     while (kase--)
    91         init();
    92     return 0;
    93 }
    View Code

    wa点:

    1)算法不能写错……

    2)MST的时候要考虑删边后不连通的情况

    3)每次初始化要把上次遗留的数据删干净

    做完模拟题感到了空虚和无奈……

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    filterFilter用法
    angular.copy()克隆数据
    angularjs中是否选择所有和$filter过滤orderBy排序
    qt5.5 qtcreator中文乱码
    shared_ptr
    Thrift-0.9.2编译安装
    一行代码获取通讯录联系框架
    IOS枚举使用
    Static Cell-静态TableView
    NavigationController的使用整理
  • 原文地址:https://www.cnblogs.com/yalphait/p/10133952.html
Copyright © 2011-2022 走看看