zoj3732&& hdu4797 Graph Reconstruction

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;
}

  

时间: 2024-11-08 22:57:01

zoj3732&& hdu4797 Graph Reconstruction的相关文章

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

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 sim

ZOJ 3732 Graph Reconstruction Havel_Hakimi定理

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5078 题意:有N个点组成无向图,每个点的度为ai,问是否能组成图,并且组成的图方式是否唯一. 思路:Havel_Hakimi定理的应用. (以下转载) 1,Havel-Hakimi定理主要用来判定一个给定的序列是否是可图的. 2,首先介绍一下度序列:若把图 G 所有顶点的度数排成一个序列 S,则称 S 为图 G 的度序列. 3,一个非负整数组成的有限序列如果是某个无向图

UVA 12382 Grid of Lamps ZOJ 3732 Graph Reconstruction 可图判定性

Uva 12382 题目链接:点击打开链接 ZOJ 3732题解:点击打开链接 Uva12382 题意: 给定n*m的地板,每个地板上有一盏灯,要么亮要么暗. 下面n个数字表示每行至少亮的灯盏数 下面m个数字表示每列至少亮的灯盏数 求:开始灯都是暗的,最少点亮几盏灯能满足上述条件 思路: 先给行和列大到小排序,然后每次用行减掉列.减完后再排个序即可. 即:实时排序. #include <cstdio> #include <cstring> #include <iostream

CUGBACM_Summer_Tranning3

A.ZOJ3726 Alice's Print Service 题解 here 这道题在HDU上要用I64d 在ZOJ上要用lld C.ZOJ3728 Collision 几何题,分几种情况:和大圆相离.和大圆相交和小圆相离.和大圆相交小圆也相交. 还有一种情况需要考虑,它飞的方向远离圆则永远不相交. 借用土豪的神模板 #include<cstring> #include<string> #include<fstream> #include<iostream>

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

第一次就去拉了点思维很神奇的CF题目 # Origin Title     A CodeForces 607A Chain Reaction     B CodeForces 385C Bear and Prime Numbers     C CodeForces 670D2 Magic Powder - 2     D CodeForces 360B Levko and Array     E CodeForces 68B Energy exchange     F CodeForces 24

Graph Regularized Feature Selection with Data Reconstruction

Abstract • 从图正则数据重构方面处理无监督特征选择: • 模型的思想是所选特征不仅通过图正则保留了原始数据的局部结构,也通过线性组合重构了每个数据点: • 所以重构误差成为判断所选特征质量的自然标准. • 通过最小化重构误差,选择最好保留相似性和判别信息的特征: 1  Introduction  • 目前有两大类无监督特征选择算法:Similarity preserving 和 clustering performance maximization:Similarity preserv

Graph Cut and Its Application in Computer Vision

Graph Cut and Its Application in Computer Vision 原文出处: http://lincccc.blogspot.tw/2011/04/graph-cut-and-its-application-in.html 网络流算法最初用于解决流网络的优化问题,比如水管网络.通信传输和城市的车流等.Graph cut作为其中一类最常见的算法,用于求解流网络的最小割,即寻找一个总容量最小的边集合,去掉这个集合中的所有边将阻断这个网络.图像和视频也能被视作网络(或者

Leetcode: Sequence Reconstruction

Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. The org sequence is a permutation of the integers from 1 to n, with 1 ≤ n ≤ 104. Reconstruction means building a shortest common supersequence of the se

【构造题 贪心】cf1041E. Tree Reconstruction

比赛时候还是太慢了……要是能做快点就能上分了 Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from 11 to nn. For every edge ee of this tree, Monocarp has written two numbers: the ma