POJ 1679 the Unique MST (sub-niche into a tree)

Source: Internet
Author: User

Test instructions: Determines whether the minimum spanning tree is unique.

Idea: Finding the sub-niche into a tree, if it is equal to the minimum spanning tree, is not unique.

PS Blog

#include <iostream> #include <stdio.h> #include <string.h> using namespace std;
/* For minimum spanning tree, use array max[i][j] to represent the maximum weights in MST in the I-J path, immediately enumerate all edges that are not in MST, replace the edge with the maximum weight, update the answer point number starting from 0 */const int maxn=110;
const INT inf=0x3f3f3f3f;//1061109567 bool VIS[MAXN];
int LOWC[MAXN];
int PRE[MAXN];
int Max[maxn][maxn];//max[i][j] represents the maximum edge right of the path from I to J in the smallest spanning tree bool USED[MAXN][MAXN];
int COST[MAXN][MAXN];

int ans;
    int prim (int cost[][maxn],int n) {int ans=0;
    memset (vis,false,sizeof (VIS));
    memset (max,0,sizeof (Max));
    memset (used,false,sizeof (used));
    Vis[0]=true;
    Pre[0]=-1;
        for (int i=1;i<n;i++) {lowc[i]=cost[0][i];
    pre[i]=0;
    } lowc[0]=0;
        for (int i=1;i<n;i++) {int minc=inf;
        int p=-1;
                for (int j=0;j<n;j++) if (!vis[j]&&minc>lowc[j]) {minc=lowc[j];
            P=j;
        } if (Minc==inf) return-1;
        Ans+=minc;
        Vis[p]=true; USED[P][PRE[P]]=USED[PRE[P]][P]=true;
            for (int j=0;j<n;j++) {if (Vis[j]) Max[j][p]=max[p][j]=max (max[j][pre[p]],lowc[p]);
                if (!vis[j]&&lowc[j]>cost[p][j]) {lowc[j]=cost[p][j];
            Pre[j]=p;
}}} return ans;
    } int smst (int cost[][maxn],int n) {int min=inf;
                for (int i=0;i<n;i++) for (int j=i+1;j<n;j++) if (Cost[i][j]!=inf&&!used[i][j]) {
            Min=min (Min,ans+cost[i][j]-max[i][j]);
} if (Min==inf) return-1;//does not exist return Min;
    } int main () {int t,i,j;
    int n,m;
    int u,v,w;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &n,&m);
                for (i=0;i<n;i++) for (j=0;j<n;j++) {if (i==j) cost[i][j]=0;
            else Cost[i][j]=inf;
            } while (m--) {scanf ("%d%d%d", &u,&v,&w);
            u--;v--;
        Cost[u][v]=cost[v][u]=w; } ans=Prim (cost,n);
        if (ans==-1) printf ("Not unique!\n");
        else if (Ans==smst (cost,n)) printf ("Not unique!\n");
    else printf ("%d\n", ans);
} return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.