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 i th  item is the degree of i th  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 {u i ,v i } is the i th  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();
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值