PKU 1679 The Unique MST(解题报告)

思路是先找出最小生成树,并记录每条遍,要想说明是唯一的最小生成树,那么他的每条边都是必不可少的,然后我们可以枚举所有最小生成树的边 ,依次去掉,看是否还能不能组成一棵相同权值和的mst。就是因为当初的一点点儿错误导致 ,一周了将近,才拿出来,一眼看出bug!

#include<iostream>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int M=103;
int F[M];
int r[M];
int n,m,t,cnt;
int res,ans;

struct edge
{
    int u,v,w;
} per[M*M*M];

bool cmp(edge a,edge b)
{
    return a.w<b.w;
}
void init()
{
    for(int i=1; i<=n; i++)
    {
        F[i]=i;
        r[i]=1;
    }
}

//int Find(int x)
//{
//    if(x!=F[x])
//       F[x]=Find(F[x]);
//       return F[x];
//}
int Find(int x)
{
    int r=x;
    while(r!=F[r])
    {
        r=F[r];
    }
    int k=x;
    while(k!=r)
    {
        int t=F[k];
        F[k]=r;
        k=t;
    }
    return r;
}
bool union_set(int u,int v)
{
    int tx=Find(u);
    int ty=Find(v);
    if(tx==ty)
        return false;
    else if(r[tx]>r[ty])
        F[ty]=tx;
    else if(r[tx]<r[ty])
        F[tx]=ty;
    else
    {
        F[tx]=ty;
        r[ty]++;
    }
    return true;
}

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        queue<int>dict;
        for(int i=0; i<m; i++)
            cin>>per[i].u>>per[i].v>>per[i].w;
        sort(per,per+m,cmp);
        init();
        cnt=0;
        res=0;

        for(int i=0; i<m; i++)
        {
            if(union_set(per[i].u,per[i].v))
            {
                cnt++;
                dict.push(i);
                res+=per[i].w;
                if(cnt==n-1)
                    break;
            }
        }
        int flag=0;
        while(!dict.empty())
        {
            cnt=0;
            ans=0;
            init();
            int k=dict.front();
            dict.pop();
            for(int i=0; i<m; i++)
            {
                if(i==k)
                    continue;
                if(union_set(per[i].u,per[i].v))
                {
                    cnt++;

                    ans+=per[i].w;
                    if(cnt==n-1)
                    {
                        if(ans==res)
                        {
                            flag=1;
                            break;
                        }
                    }
                }
            }
            if(flag)
            {
                cout<<"Not Unique!"<<endl;
                break;
            }
        }
        if(!flag)
            cout<<res<<endl;
    }
    return 0;
}
时间: 2024-11-10 08:21:28

PKU 1679 The Unique MST(解题报告)的相关文章

poj 1679 The Unique MST (判定最小生成树是否唯一)

题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29408   Accepted: 10520 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spannin

POJ - 1679 The Unique MST (次小生成树)

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 followin

poj 1679 The Unique MST (判断最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20679   Accepted: 7255 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1679 The Unique MST(求最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20430   Accepted: 7186 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

poj 1679 The Unique MST (次小生成树)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20293   Accepted: 7124 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

poj 1679 The Unique MST 【次小生成树】【模板】

题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后添加到最小生成树上,然后把原树上添加了之后形成环的最长的边删去,知道一个最小的.就是次小生成树. 这些需要的都可以在求解最小生成树的时候处理出来. AC代码: #include <cstdio> #include <cstring> #include <iostream> #i

hdu 1679 The Unique MST (克鲁斯卡尔)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24152   Accepted: 8587 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

[2016-01-27][POJ][1679][The Unique MST]

C - The Unique MST Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1679 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree):

POJ 1679 The Unique MST【MST是否唯一,Prime算法,最好的代码】

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27389   Accepted: 9816 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire