[原]Graph Reconstruction ZOJ - 3732 Havel-Hakimi定理 + 构造

楚东方 17/12/14 18:49:55

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

Havel-Hakimi定理:

  1. 一个非负整数组成的有限序列如果是某个无向图的度序列,则称该序列是可图的。

  2. 判定过程:
    (1)对当前数列排序,使其呈非递增序列
    (2)从第二个数开始对其后d[1]个数字减1,d[1]代表排序后第1个数的值
    (3)然后删除第一个之后对剩下的数继续排序
    (4)一直循环直到当前序列出现负数(即不是可图的情况)或者当前序列全为0 (可图)时退出。

  3. 举例:
    序列S:7,7,4,3,3,3,2,1
    删除序列S的首项 7 ,对其后的7项每项减1,
    得到:6,3,2,2,2,1,0,
    继续删除序列的首项6,
    对其后的6项每项减1,
    得到:2,1,1,1,0,-1,
    到这一步出现了负数,因此该序列是不可图的

再举例:
序列:4 3 1 5 4 2 1
排序之后:5 4 4 3 2 1 1
删除5对后面5个数减1操作
3 3 2 1 0 1
排序
3 3 2 1 1 0
删除3对后面3个数减1操作
2 1 0 1 0
排序
2 1 1 0 0
删除2 对后面2个数减1操作
0 0 0 0
全为0,可图

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn=100+10;
const int maxm=100000+10;

struct Edge
{
    int u;
    int  v;
}edge1[maxm],edge2[maxm];


struct Node
{
    int ind;//节点号
    int d;  //度数
    bool operator < (const Node &a) const
    {
        return d>a.d;
    }
};

Node node1[maxn],node2[maxn];

int flag,n,m,mm;

void getother(int s,int z)
{
    for(int i=0;i<n;++i)
      node2[i]=node1[i];
    for(int i=0;i<m;++i)
      edge2[i]=edge1[i];

    swap(node2[z].ind,node2[z+1].ind);


    mm=m;
    for(int i=s;i<n;++i)
    {
        sort(node2+i,node2+n);
        if(node2[i].d==0) break;
        int p=i+node2[i].d;
        if(p>=n||node2[p].d<1)
        {
            flag=-1;
            return ;
        }
        for(int j=1;j<=node2[i].d;++j)
        {
            edge2[mm].u=node2[i].ind;
            edge2[mm].v=node2[i+j].ind;
            mm++;
            node2[i+j].d--;
        }
    }
}
void solve()
{
    for(int i=0;i<n;++i)
    {
        sort(node1+i,node1+n);

        if(node1[i].d==0) break;

        int p=i+node1[i].d;

        if(p>=n||node1[p].d<1)//不存在
        {
            flag=-1;
            return ;
        }

        if(flag==0&&p+1<n&&node1[p].d==node1[p+1].d)
        {
            //存在两种的情况
            flag=1;
            getother(i,p);//计算另一种情况
            if(flag==-1) return ;
        }

        for(int j=1;j<=node1[i].d;++j)
        {
            edge1[m].u=node1[i].ind;
            edge1[m].v=node1[i+j].ind;
            m++;
            node1[i+j].d--;
        }
    }
}
int main()
{
//    freopen("data.txt","r",stdin);
    while(~scanf("%d",&n))
    {
        //input
        for(int i=0;i<n;++i)
        {
            scanf("%d",&node1[i].d);
            node1[i].ind=i;
        }

        flag=0;
        m=mm=0;

        solve();


        if(flag==-1) printf("IMPOSSIBLE\n");
        else if(flag==0)
        {
            printf("UNIQUE\n");
            printf("%d %d\n",n,m);
            for(int i=0;i<m;++i)
            {
                if(i) printf(" ");
                printf("%d",edge1[i].u+1);
            }
            printf("\n");
            for(int i=0;i<m;++i)
            {
                if(i) printf(" ");
                printf("%d",edge1[i].v+1);
            }
            printf("\n");
        }
        else
        {
            printf("MULTIPLE\n");
            printf("%d %d\n",n,m);

            for(int i=0;i<m;++i)
            {
                if(i) printf(" ");
                printf("%d",edge1[i].u+1);
            }
            printf("\n");
            for(int i=0;i<m;++i)
            {
                if(i) printf(" ");
                printf("%d",edge1[i].v+1);
            }
            printf("\n");


            printf("%d %d\n",n,m);
            for(int i=0;i<m;++i)
            {
                if(i) printf(" ");
                printf("%d",edge2[i].u+1);
            }
            printf("\n");

            for(int i=0;i<m;++i)
            {
                if(i) printf(" ");
                printf("%d",edge2[i].v+1);
            }
            printf("\n");
        }
    }
    return 0;
}
作者:chudongfang2015 发表于2017/12/14 18:49:55 原文链接
阅读:1 评论:0 查看评论