zoj 3732 Graph Reconstruction 構造

Let there be a simple graph with N vertices but we just know the degree of each vertex. Is it possible to reconstruct the graph only by these information? 

A simple graph is an undirected graph that has no loops (edges connected at both ends to the same vertex) and no more than one edge between any two different vertices. The degree of a vertex is the number of edges that connect to it.

Input

There are multiple cases. Each case contains two lines. The first line contains one integer N (2 ≤ N ≤ 100), the number of vertices in the graph. The second line conrains N integers in which the ith item is the degree of ith vertex and each degree is between 0 and N-1(inclusive).

Output

If the graph can be uniquely determined by the vertex degree information, output "UNIQUE" in the first line. Then output the graph.

If there are two or more different graphs can induce the same degree for all vertices, output "MULTIPLE" in the first line. Then output two different graphs in the following lines to proof.

If the vertex degree sequence cannot deduced any graph, just output "IMPOSSIBLE".

The output format of graph is as follows:

N E
u1 u2 ... uE
v1 v2 ... vE
Where N is the number of vertices and E is the number of edges, and {ui,vi} is the ith edge the the graph. The order of edges and the order of vertices in the edge representation is not important since we would use special judge to verify your answer. The number of each vertex is labeled from 1 to N. See sample output for more detail.

Sample Input

1
0
6
5 5 5 4 4 3
6
5 4 4 4 4 3
6
3 4 3 1 2 0

Sample Output

UNIQUE
1 0


UNIQUE
6 13
3 3 3 3 3 2 2 2 2 1 1 1 5
2 1 5 4 6 1 5 4 6 5 4 6 4
MULTIPLE
6 12
1 1 1 1 1 5 5 5 6 6 2 2
5 4 3 2 6 4 3 2 4 3 4 3
6 12
1 1 1 1 1 5 5 5 6 6 3 3
5 4 3 2 6 4 3 2 4 2 4 2
IMPOSSIBLE


題意:

給出每個點度數,要你還原圖原來的樣子,如果能還原的話,問是不是唯一解,不是的話要輸出兩個解。


思路:

按度數每次取最高的,連線第2高,第3高.....如果度數大於剩下的點,則不可能,如果小於剩下的點,則多種可能。

用優先隊列來維護點。

多種情況點話,原本是取第2~di+1大的點,改成取2~di,di+2的點即可。


ps:

hdu上和uvalive上都沒特判,改了半天代碼,發現在zoj上就ac了。


具體看代碼:

//
//  main.cpp
//  UVALive - 6617
//
//  Created by zc on 2017/9/5.
//  Copyright © 2017年 zc. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define ll long long 
using namespace std;
const int N=220;
struct node
{
    int i,v;
    friend bool operator < (node a,node b)
    {
        return a.v<b.v;
    }
}a[N];

int n,flag,sum;
priority_queue<node>q1,q2;
vector<pair<int,int> >ans;

void solve(int mul)
{
    while(!q1.empty())  q1.pop();
    while(!q2.empty())  q2.pop();
    ans.clear();
    for(int i=1;i<=n;i++)
        if(a[i].v>0)    q1.push(a[i]);
    flag=0;
    int tt=0;
    while(!q1.empty())
    {
        node t=q1.top();
        q1.pop();
        if(t.v>q1.size())   {flag=-1;break;}
        if(t.v<q1.size())   flag=1,tt=1;
        for(int i=0;i<t.v;i++)
        {
            node c=q1.top();
            q1.pop();
            if(mul&&tt&&i==t.v-1)
            {
                node cc=q1.top();
                q1.pop();
                q1.push(c);
                c=cc;
                tt=0;
            }
            if(--c.v>0) q2.push(c);
            ans.push_back(make_pair(t.i, c.i));
        }
        while(!q2.empty())
        {
            node t=q2.top();
            q2.pop();
            q1.push(t);
        }
    }
}

void out()
{
    printf("%d %d\n",n,sum/2);
    for(int i=0;i<ans.size();i++)
    {
        if(i) printf(" ");
        printf("%d",ans[i].first);
    }
    printf("\n");
    for(int i=0;i<ans.size();i++)
    {
        if(i)   printf(" ");
        printf("%d",ans[i].second);
    }
    printf("\n");
}

int main(int argc, const char * argv[]) {
    while(~scanf("%d",&n))
    {
        sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i].v);
            a[i].i=i;
            sum+=a[i].v;
        }
        if(sum%2==0)    solve(0);
        if(flag==-1||sum%2)
        {
            printf("IMPOSSIBLE\n");
            continue;
        }
        if(flag==0)     printf("UNIQUE\n");
        else    printf("MULTIPLE\n");
        out();
        if(flag==1)
        {
            solve(1);
            out();
        }
    }
}



發佈了323 篇原創文章 · 獲贊 119 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章