Day5 - G - The Unique MST POJ - 1679

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!

思路:找次小生成树,如果权值相等则不唯一,用kruskal实现次小生成树
Day5 - G - The Unique MST POJ - 1679
const int maxm = 105;
const int maxn = 10005;

struct edge {
    int u, v, w;
    edge(int _u=-1, int _v=-1, int _w=0):u(_u), v(_v), w(_w){}
    bool operator<(const edge &a) const {
        return w < a.w;
    }
};
vector<edge> Edge;

int fa[maxm], T, N, M, tree[maxn], k;

void init() {
    Edge.clear();
    for(int i = 1; i <= N; ++i)
        fa[i] = i;
    k = 0;
}

int Find(int x) {
    if(fa[x] == x)
        return x;
    return fa[x] = Find(fa[x]);
}

void Union(int x, int y) {
    x = Find(x), y = Find(y);
    if(x != y) fa[x] = y;
}

int main() {
    scanf("%d", &T);
    while(T--) {
        int t1, t2, t3, u, v;
        scanf("%d%d", &N, &M);
        init();
        int sum = 0;
        for(int i = 0; i < M; ++i) {
            scanf("%d%d%d", &t1, &t2, &t3);
            Edge.push_back(edge(t1, t2, t3));
        }
        sort(Edge.begin(), Edge.end());
        bool flag = true;
        for(int i = 0; i < M; ++i) {
            u = Edge[i].u, v = Edge[i].v;
            u = Find(u), v = Find(v);
            if(u != v) {
                sum += Edge[i].w;
                Union(u,v);
                tree[k++] = i;
            }
        }
        for(int i = 0; i < k; ++i) {
            int cnt = 0, edgenum = 0;
            for(int t = 1; t <= N; ++t)
                fa[t] = t;
            for(int j = 0; j < M; ++j) {
                if(j == tree[i]) continue;
                u = Edge[j].u, v = Edge[j].v;
                u = Find(u), v = Find(v);
                if(u != v) {
                    cnt += Edge[j].w;
                    edgenum++;
                    Union(u,v);
                }
            }
            if(cnt == sum && edgenum == N - 1) {
                flag = false;
                break;
            }
        }
        if(flag)
            printf("%d\n", sum);
        else printf("Not Unique!\n");
    }
    return 0;
}
View Code

 


上一篇:Python学习day5


下一篇:Android开发过程遇到的问题小计