[Kuangbin take you to fly] minimum spanning six thematic K - The Unique MST (minimum spanning tree to determine whether or not the only)

K - The Unique MST

Topic links: https://vjudge.net/contest/66965#problem/K

topic:

No fixed connection to the map, tell it to the minimum spanning tree is unique.

    Definition 1 (MST): Consider communication undirected graph G = (V, E). G is a spanning tree of the subgraph of G, such as T = (V ', E' ), has the following properties:
    1. V '= V.
    2.T is connected and acyclic.

    Definition 2 (minimum spanning tree): considered edge-weighted, communication, undirected graph G = (V, E). G Minimum spanning tree T = (V, E ') is the minimum cost spanning tree. The total cost of T refers to the weight of the E 'and the weights of all edges.
Input
    The first line contains a single integer t (1 <= t <= 20), i.e., the number of test cases. Each case represents a chart. It contains two integers n and m (1 <= n <= 100), and the number of nodes line side starts. Each row of the m-th row comprising triplet (xi, yi, wi), represented by xi and yi = the weight wi of the edge connector. For any two nodes, at most one edge connecting them.
Yield
    for each input, if the MST is unique, then the total cost of printing it, or print string 'Not Unique! '.
Sample Input

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

Sample Output

    . 3
    Not of Unique!

Ideas: first calculating the minimum spanning tree, and each side with an array of records, and then remove those enumerated whether it can be constructed to look to the same value, and the minimum spanning tree. And after the border erase, it may constitute a tree diagram, to deal with it

 

 

//
// Created by hanyu on 2019/8/2.
//
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include<math.h>
using namespace std;
typedef long long ll;
const int maxn=2e6+7;
int father[maxn];
struct Node{
    int u,v,w;
    bool operator<(const Node &other)const{
        return this->w<other.w;
    }
}node[maxn];
int find(int x)
{
    if(x==father[x])
        return x;
    return father[x]=find(father[x]);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
            scanf("%d%d%d",&node[i].u,&node[i].v,&node[i].w);
        for(int i=0;i<=n;i++)
            father[i]=i;
        sort(node,node+m);
        int write[maxn],cnt=0,ans=0;
        for(int i=0;i<m;i++)
        {
            int uu=find(node[i].u);
            int vv=Find (Node [I] .v);
             IF (Uu == VV)
                 Continue ;
             the else 
            { 
                Father [Uu] = VV; 
                Write [CNT ++] = I; // array record path 
                ANS = + [I] Node .W ; 
            } 
        } // compute the minimum spanning tree 
        int In Flag = . 1 ;
         for ( int K = 0 ; K <cnt; K ++) // for deletion, cnt edges, then minimum spanning tree, and determining the minimum spanning tree above ans are the same 
        {
             for ( int KK = 0; KK <= n-; KK ++ ) 
            { 
                Father [KK] = KK; 
            } 
            int SUM = 0 , HH = 0 ;
             for ( int I = 0 ; I <m; I ++ ) 
            { 
                IF (I == Write [K]) // the same number and the number of records, without treatment 
                    Continue ;
                 int UUU = Find (Node [I] .u); // minimum spanning tree 
                int VVV = Find (Node [I] .v);
                 IF (UUU == VVV)
                     the Continue ;
                 the else
                {
                    father[uuu]=vvv;
                    sum+=node[i].w;
                    hh++;
                }
            }
            if(hh==n-1&&sum==ans)
            {
                flag=0;
                break;
            }
        }
        if(flag)
            printf("%d\n",ans);
        else
            printf("Not Unique!\n");
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Vampire6/p/11288343.html