Graph Reconstruction


Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

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
u

1

 u

2

 ... u

E

v

1

 v

2

 ... v

EWhere 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 思路:这是一道坑题.......坑在格式的就不说了,可以原谅oj.如果hdu4797交不过,去zoj3732就能过了,(因为不能specail judge纯oj问题).
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=111;
int sum,n;
typedef pair<int ,int > P;
P deg[maxn];
P tmpdeg[maxn];
int ash1[maxn*maxn*2],ash2[maxn*maxn*2],alen; bool cmp(P A,P B)
{
if(A.first==B.first) return A.second<B.second;
return A.first>B.first;
}
void prints(){
printf("%d %d\n",n,sum>>1); if(alen>0)printf("%d",ash1[0]);
for(int i=1;i<alen;i++){
printf(" %d",ash1[i]);
}
puts("");
if(alen>0)printf("%d",ash2[0]);
for(int i=1;i<alen;i++){
printf(" %d",ash2[i]);
}
puts("");
} void cpy(){
for(int i=0;i<n;i++)tmpdeg[i]=deg[i];
sort(tmpdeg,tmpdeg+n,cmp);
} bool single;
bool rebuild(){
cpy();
alen=0;
single=true;
for(;tmpdeg[0].first>0;){
int amount=0;
for(int j=1;j<n;j++){
if(tmpdeg[j].first>0){
amount++;
}
}
if(amount<tmpdeg[0].first)return false;
if(single&&(tmpdeg[tmpdeg[0].first].first)==(tmpdeg[tmpdeg[0].first+1].first)&&tmpdeg[0].first+1<n){
single=false;
}
for(int j=1;j<=tmpdeg[0].first;j++){
tmpdeg[j].first--;
ash1[alen]=tmpdeg[0].second;
ash2[alen++]=tmpdeg[j].second;
} tmpdeg[0].first=0;
sort(tmpdeg,tmpdeg+n,cmp);
}
return true;
} bool rebuild2(){
cpy();
alen=0;
for(;tmpdeg[0].first>0;){
int amount=0;
for(int j=1;j<n;j++){
if(tmpdeg[j].first>0){
amount++;
}
}
if(amount<tmpdeg[0].first)return false;
if((tmpdeg[tmpdeg[0].first].first)==(tmpdeg[tmpdeg[0].first+1].first)&&tmpdeg[0].first+1<n){
swap(tmpdeg[tmpdeg[0].first].second,tmpdeg[tmpdeg[0].first+1].second);
}
for(int j=1;j<=tmpdeg[0].first;j++){
tmpdeg[j].first--;
ash1[alen]=tmpdeg[0].second;
ash2[alen++]=tmpdeg[j].second;
}
tmpdeg[0].first=0;
sort(tmpdeg,tmpdeg+n,cmp);
}
return true;
} int main(){
while(scanf("%d",&n)==1){
bool failed =false;
for(int i=1;i<=n;i++){
scanf("%d",&(deg[i-1].first));
deg[i-1].second=i;
if(deg[i-1].first<0)failed=true;
}
sort(deg,deg+n,cmp);
sum=0;
for(int i=0;i<n;i++){
if(deg[i].first>=n)failed=true;
sum+=deg[i].first;
}
if(sum&1)failed=true;
if(failed||!rebuild()){
puts("IMPOSSIBLE");
}
else if(single){
puts("UNIQUE");
prints();
}
else {
puts("MULTIPLE");
prints();
rebuild2();
prints();
}
}
return 0;
}

  

zoj3732&& hdu4797 Graph Reconstruction的更多相关文章

  1. 2013长沙 G Graph Reconstruction (Havel-Hakimi定理)

    Graph Reconstruction Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Let there ...

  2. Codeforces Round #192 (Div. 1) C. Graph Reconstruction 随机化

    C. Graph Reconstruction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/3 ...

  3. 2013亚洲区域赛长沙站 ZOJ 3732 Graph Reconstruction

    题目链接 Graph Reconstruction 题意 给你无向图每个点的度数, 问是否存在唯一解, 存在输出唯一解, 多解输出两个, 无解输出IMPOSSIBLE 思路 这里用到了 Havel-H ...

  4. ZOJ3732 Graph Reconstruction Havel-Hakimi定理

    分析: 给定一个非负整数序列{dn},若存在一个无向图使得图中各点的度与此序列一一对应,则称此序列可图化. 进一步,若图为简单图,则称此序列可简单图化 (来自百度百科) 可简单图化的判定可以用Have ...

  5. CodeForces-329C(div1):Graph Reconstruction(随机&构造)

    I have an undirected graph consisting of n nodes, numbered 1 through n. Each node has at most two in ...

  6. CF 329C(Graph Reconstruction-随机化求解-random_shuffle(a+1,a+1+n))

    C. Graph Reconstruction time limit per test 3 seconds memory limit per test 256 megabytes input stan ...

  7. 论文解读(SEP)《Structural Entropy Guided Graph Hierarchical Pooling》

    论文信息 论文标题:Structural Entropy Guided Graph Hierarchical Pooling论文作者:Junran Wu, Xueyuan Chen, Ke Xu, S ...

  8. CF-192-diy-2

    题目链接: http://codeforces.com/contest/330 A. Cakeminator 题目意思: 给一个r*c的矩阵方格,有些位置有S,如果某一行和一列都不含标记为S的方格,则 ...

  9. 2018省赛赛第一次训练题解和ac代码

    第一次就去拉了点思维很神奇的CF题目 2018省赛赛第一次训练 # Origin Title     A CodeForces 607A Chain Reaction     B CodeForces ...

随机推荐

  1. Linux内核分析--内核中的数据结构双向链表【转】

    本文转自:http://blog.csdn.net/yusiguyuan/article/details/19840065 一.首先介绍内核中链表 内核中定义的链表是双向链表,在上篇文章--libev ...

  2. C# 给某个方法设定执行超时时间

    ManualResetEvent.WaitOne 方法 https://msdn.microsoft.com/en-us/library/system.threading.manualreseteve ...

  3. Pc端css初始化

    @charset "UTF-8"; /**css初始化**/ body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd ...

  4. 项目梳理6——使用WebApiTestClient为webapi添加测试

    1.使用nuget添加WebApiTestClient的引用 2.xxxxx.WebApi\Areas\HelpPage\Views\Help\Api.cshtml页面末尾添加如下代码: @Html. ...

  5. #网页中动态嵌入PDF文件/在线预览PDF内容#

    摘要:在web开发时我们有时会需要在线预览PDF内容,在线嵌入pdf文件: 问题1:如何网页中嵌入PDF: 在网页中: 常用的几种PDF预览代码片段如下: 代码片段1: 1 <object ty ...

  6. UVa 12563 劲歌金曲(0-1背包)

    https://vjudge.net/problem/UVA-12563 题意: 在一定的时间内连续唱歌,最后一首唱11分钟18秒的劲歌金曲,问最多能长多长时间. 思路: 0-1背包问题,背包容量为t ...

  7. 安装Zookeeper和kafka,安装完毕后,遇到的错误

    按照原文链接 http://www.cnblogs.com/swneng/p/10212460.html 在windows下进行安装 之后运行 D:\00H_Bigdata\kafka_2.11-2. ...

  8. 《WAP团队项目需求分析改进》

    基于原型的团队项目需求调研与分析 本项目是一个家教系统的实现,随着时代的进步,现今已经进入信息技术时代,越来越多的人注意到了教育的重要性.家长对于孩子的学习提高注意力,大家都不想自己的孩子输在起跑线上 ...

  9. 《剑指offer》第二十四题(反转链表)

    // 面试题24:反转链表 // 题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的 // 头结点. #include <iostream> #include &quo ...

  10. English trip V1 - 6.Accidents Happen! 发生意外! Teacher:Corrine Key: 过去进行时 was or were + Ving

    In this lesson you will learn to talk about past occurences. 过去进行时 课上内容(Lesson) C: Hi, Loki! L: Hi, ...