Graph Reconstruction

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HYNU_zhizuzhe/article/details/50844144

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
题意:已知度序列,判断其是否能构图,不能,输出IMPOSSIBLE,能,输出图的边(能构成一种以上的图,只要输出两个图)

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

struct node{
	int id,v;
	node() {}
	bool operator <(const node &c) const {return v>c.v;}
}h[110];

int n,u[110*110],v[110*110],m,s,t,r;

int Havel_Hakim()
{
	int i,j,flag=0;
	for(i=0;i<n;i++)
	{
		sort(h+i,h+n);
		if(h[i].v==0) return flag;
		if(i+h[i].v>=n) return -1;
		for(j=i+1;j<=i+h[i].v;j++)
		{
			h[j].v--;
			if(h[j].v<0) return -1;
			u[m]=h[i].id; v[m++]=h[j].id;
		}
		if(j<n&&h[j].v==h[j-1].v+1)
		{
			flag=1;
			s=m-1;
			t=h[j].id;
			r=h[j-1].id;
		}
	}
}

void put()
{
	printf("%d %d\n",n,m);
	if(m==0) printf("\n\n");
	else {
		for(int i=0;i<m;i++) printf("%d%c",u[i],i==m-1?'\n':' ');
		for(int i=0;i<m;i++) printf("%d%c",v[i],i==m-1?'\n':' ');
	}
}

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		m=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&h[i].v);
			h[i].id=i+1;
		}
		int H=Havel_Hakim();
		if(H==-1) printf("IMPOSSIBLE\n");
		else if(H==0) {
			printf("UNIQUE\n");
			put();
		}else {
			printf("MULTIPLE\n");
			put();
			v[s]=t;
			for(int i=s+1;i<m;i++)
			{
				if(u[i]==t) u[i]=r;
				else if(u[i]==r) u[i]=t;
				if(v[i]==t) v[i]=r;
				else if(v[i]==r) v[i]=t;
			}
			put();
		}
	}
	return 0;
}


注意事项:一个图有n个顶点,最多有n*(n-1)/2条边


猜你喜欢

转载自blog.csdn.net/HYNU_zhizuzhe/article/details/50844144