• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            poj1679

            The Unique MST

            Time Limit: 1000MS Memory Limit: 10000K
            Total Submissions: 13200 Accepted: 4575

            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!
            
            裸的判定最小生成樹是否唯一
            做法
            1,對圖中每一條邊,如果存在與之相等的其他的邊,則標(biāo)記這條邊
            2,求一次最小生成樹,得到weight1,作為比較用
            3,對于最小生成樹中的每一條邊,檢查這條邊有沒有與之相同的,如果有,則刪掉這條邊,
            再求最小生成樹,如果相等,則說明最小生成樹不唯一
            判斷完所有要判斷的邊后任然不相等,說明最小生成樹唯一
              1#include<algorithm>
              2#include<cstdlib>
              3using namespace std;
              4#define maxn 101
              5#define maxm 15000
              6struct node
              7{
              8    int u,v,w;
              9    int equal,used,del;
             10}
             edge[maxm];
             11int n,m;
             12int parent[maxn];
             13int first;
             14void ufset()
             15{
             16    int i;
             17    for(i=1; i<=n; i++) parent[i]=-1;
             18}

             19int find(int x)
             20{
             21    int s;
             22    for(s=x; parent[s]>=0; s=parent[s]);
             23    while(s!=x)
             24    {
             25        int tmp=parent[x];
             26        parent[x]=s;
             27        x=tmp;
             28    }

             29    return s;
             30}

             31void union1(int R1,int R2)
             32{
             33    int r1=find(R1),r2=find(R2);
             34    int tmp=parent[r1]+parent[r2];
             35    if (parent[r1]>parent[r2])//r2所在樹節(jié)點數(shù)多于r1
             36    {
             37        parent[r1]=r2;
             38        parent[r2]=tmp;
             39    }

             40    else
             41    {
             42        parent[r2]=r1;
             43        parent[r1]=tmp;
             44    }

             45}

             46int cmp(struct node a,struct node b)
             47{
             48    return a.w<b.w;
             49}

             50int kruskal()
             51{
             52    int sumweight=0,num=0;
             53    int u,v;
             54    ufset();
             55    for(int i=0; i<m; i++)
             56    {
             57        if (edge[i].del==1)
             58        {
             59            continue;
             60        }

             61        u=edge[i].u;
             62        v=edge[i].v;
             63        if (find(u)!=find(v))
             64        {
             65            sumweight+=edge[i].w;
             66            num++;
             67            union1(u,v);
             68            if (first)
             69            {
             70                edge[i].used=1;
             71            }

             72        }

             73        if (num>=n-1)
             74        {
             75            break;
             76        }

             77    }

             78    return sumweight;
             79}

             80int main()
             81{
             82    int t,i,j,k;
             83    int u,v,w;
             84    scanf("%d",&t);
             85    for(i=1; i<=t; i++)
             86    {
             87        scanf("%d%d",&n,&m);
             88        memset(edge,0,sizeof(edge));
             89        for(j=0; j<m; j++)
             90        {
             91            scanf("%d%d%d",&u,&v,&w);
             92            edge[j].u=u;
             93            edge[j].v=v;
             94            edge[j].w=w;
             95        }

             96        for(j=0; j<m; j++)
             97            for(k=0; k<m; k++)
             98            {
             99                if (k==j) continue;
            100                if (edge[j].w==edge[k].w) edge[j].equal=1;
            101            }

            102        sort(edge,edge+m,cmp);
            103        first=1;
            104        int weight1=kruskal(),weight2;
            105        first=0;
            106        for(j=0;j<m;j++)
            107        {
            108            if (edge[j].used==1&&edge[j].equal==1)
            109            {
            110                edge[j].del=1;
            111                weight2=kruskal();
            112                if (weight2==weight1)
            113                {
            114                    printf("Not Unique!\n");
            115                    break;
            116                }

            117                edge[j].del=0;
            118            }

            119        }

            120        if (j>=m)
            121        {
            122            printf("%d\n",weight1);
            123        }

            124    }

            125    return 0;
            126}

            127
             

            posted on 2012-04-02 01:49 jh818012 閱讀(260) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當(dāng)于是 取余3的意思 因為 3 的 二進(jìn)制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內(nèi)容較長,點擊標(biāo)題查看
            • --王私江
            99久久久精品免费观看国产| 狠狠综合久久AV一区二区三区| 国产亚洲美女精品久久久2020| 久久久无码精品亚洲日韩京东传媒 | 国产精品久久久久蜜芽| 久久这里都是精品| 久久99精品久久只有精品| 亚洲国产精品婷婷久久| 久久综合视频网站| 久久亚洲日韩精品一区二区三区| 久久亚洲高清观看| 99精品国产99久久久久久97 | 综合久久给合久久狠狠狠97色 | 久久人人爽人人爽人人片AV麻豆 | 中文字幕久久欲求不满| 欧美与黑人午夜性猛交久久久| 午夜精品久久久久久中宇| 色综合久久综精品| 亚洲va久久久噜噜噜久久狠狠| 精品久久久久久无码免费| 伊人久久精品无码av一区| 久久国产精品一区| 久久精品国产一区| 久久丫精品国产亚洲av| 欧美精品丝袜久久久中文字幕| 久久精品人人做人人爽电影| 日产精品久久久久久久性色| 欧美激情精品久久久久久久| 国产精品久久久久久久午夜片 | 精品亚洲综合久久中文字幕| 国内精品久久国产| 手机看片久久高清国产日韩| 99久久精品免费看国产| 国产精品久久久久无码av| 色综合久久久久综合体桃花网| 久久无码中文字幕东京热| 欧美激情精品久久久久久久九九九| 久久精品国产一区二区| 亚洲精品高清久久| 久久精品免费观看| 久久国产精品无码网站|