zoj 3732 Graph Reconstruction 构造

来源:互联网 发布:挂号网软件 编辑:程序博客网 时间:2024/05/16 19:18

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 Eu1 u2 ... uEv1 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

1065 5 5 4 4 365 4 4 4 4 363 4 3 1 2 0

Sample Output

UNIQUE1 0UNIQUE6 133 3 3 3 3 2 2 2 2 1 1 1 52 1 5 4 6 1 5 4 6 5 4 6 4MULTIPLE6 121 1 1 1 1 5 5 5 6 6 2 25 4 3 2 6 4 3 2 4 3 4 36 121 1 1 1 1 5 5 5 6 6 3 35 4 3 2 6 4 3 2 4 2 4 2IMPOSSIBLE


题意:

给出每个点度数,要你还原图原来的样子,如果能还原的话,问是不是唯一解,不是的话要输出两个解。


思路:

按度数每次取最高的,连线第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();        }    }}



原创粉丝点击