zoukankan      html  css  js  c++  java
  • POJ 1679 The Unique MST

    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 15153   Accepted: 5241

    Description

    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'.

    Input

    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.

    Output

    For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

    Sample Input

    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
    

    Sample Output

    3
    Not Unique!
    

    Source

    //这么久没刷题了、回到学校了、第一次写次小生成树,开心的是1A,不过写代码速度下降了
    //I am back
    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #define N 102 using namespace std; struct node { int a,b,w; bool visit; bool operator <(const node &t)const { return w<t.w; } }; struct Link { int to; int next; }; Link link[N]; int len[N][N]; int fa[N]; node Ed[5055]; int n,m,Min; int Find(int x) { if(x!=fa[x]) { return fa[x]=Find(fa[x]); } return x; } bool merge(int x,int y,int i) { x=Find(x); y=Find(y); if(x!=y) { int u,v; for(u=link[x].to;;u=link[u].next) { for(v=link[y].to;;v=link[v].next) { len[u][v]=len[v][u]=Ed[i].w; if(link[v].next==-1) break; } if(link[u].next==-1) break; } Ed[i].visit=1; link[u].next=y; fa[y]=x; return true; } return false; } void Kruskal() { int i; for(Min=i=0;i<m;i++) if(merge(Ed[i].a,Ed[i].b,i)) Min+=Ed[i].w; } int main() { int t,i; scanf("%d",&t); while(t--) { scanf("%d %d",&n,&m); for(i=1;i<=n;i++) fa[i]=i,link[i].to=i,link[i].next=-1; for(i=0;i<m;i++) { scanf("%d %d %d",&Ed[i].a,&Ed[i].b,&Ed[i].w); Ed[i].visit=0; } sort(Ed,Ed+m); Kruskal(); int Mi=-1,tp; for(i=0;i<m;i++) if(!Ed[i].visit) { tp=Min+Ed[i].w-len[Ed[i].a][Ed[i].b]; if(tp>Min) Mi=tp; else { Mi=tp;break; } } if(Min==Mi) printf("Not Unique!\n"); else printf("%d\n",Min); } return 0; }
  • 相关阅读:
    docker 目录移动到其他磁盘的操作
    linux -- 查看磁盘空间的大小 查看文件夹占用磁盘空间大小
    python flask 反向代理
    shell 脚本换行符的问题
    uniapp使用web-view跳转vue单页面通信,互发消息
    java基础(多线程---lambda)
    SpringBoot开发详解(五)--Controller接收参数以及参数校验
    使用 TypeScript 来开发 React 的注意事项
    video标签通过js实现增加倍速播放功能
    zblog调取置顶文章
  • 原文地址:https://www.cnblogs.com/372465774y/p/2658098.html
Copyright © 2011-2022 走看看