• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

2012 Asia Changsha Regional Contest Problem G

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

                            Author: WANG, Yelei                                                     Contest: The 2013 ACM-ICPC Asia Changsha Regional Contest

 1 #include <cstdio>
 2 #include <vector>
 3 #include <cmath>
 4 #include <queue>
 5 #include <cstring>
 6 #include <iostream>
 7 #include <algorithm>
 8 using namespace std;
 9 #define INF 0x7fffffff
10 #define mod 1000000007
11 #define ll long long
12 #define maxn 100005
13 #define pi acos(-1.0)
14 int n, m, k, t, flag, ans, s ,x ,y,c;
15 bool cmp(pair<int,int> a, pair<int,int> b){ return a.first > b.first; }
16 pair<int, int>a[maxn],b[maxn],pa[maxn],pb[maxn];
17 int main(){
18     while (~scanf("%d\n",&n)){
19         flag = 1; s = 0; k = 0, t = 0; c = 0;
20         for (int i = 1; i <= n; i++){
21             cin >> a[i].first;
22             if (a[i].first > n || a[i].first < 0)flag = 0;
23             s += a[i].first;
24             if (a[i].first == 0)t++;
25             a[i].second = i;
26         }
27         if (t == n){ printf("UNIQUE\n"); printf("%d %d\n\n\n", n, 0); continue; }
28         if ((s & 1)||flag==0){ printf("IMPOSSIBLE\n"); continue; }
29         sort(a+1, a + n+1, cmp);
30         for (int i = 1; i <= n; i++)b[i].first = a[i].first, b[i].second = a[i].second;
31         for (int i = 1, j = 1; i <= n; i++){
32             sort(a + i , a + n + 1, cmp);
33             m = a[i].first;
34             if (m < 0||m+i>n){ flag = 0; break; }
35             for (j = i + 1; j <= i + m; j++){
36                 a[j].first--;
37                 pa[k++] = make_pair(a[i].second, a[j].second);
38             }
39         }
40         for (int i = 1, j = 1; i <= n; i++){
41             sort(b + i , b + n + 1, cmp);
42             m = b[i].first;
43             if (b[i+m].first != 0 && i+m < n&&b[i+m].first == b[i+m+1].first)swap(b[i+m].second, b[i+m+1].second), flag = 2;
44             for (j = i + 1; j <= i + m; j++){
45                 pb[c++] = make_pair(b[i].second, b[j].second);
46                 b[j].first--;
47             }
48         }
49         if (flag == 0||a[n].first!=0){ printf("IMPOSSIBLE\n"); continue; }
50         if (flag == 1)printf("UNIQUE\n"),printf("%d %d\n",n,k);
51         if (flag == 2)printf("MULTIPLE\n"),printf("%d %d\n",n,k);
52         for (int i = 0; i < k; i++)printf(i == k - 1 ? "%d\n" : "%d ", pa[i].first);
53         for (int i = 0; i < k; i++)printf(i == k - 1 ? "%d\n" : "%d ", pa[i].second);
54         if (flag == 2){
55             printf("%d %d\n", n, k);
56             for (int i = 0; i < k; i++)printf(i == k - 1 ? "%d\n" : "%d ", pb[i].first);
57             for (int i = 0; i < k; i++)printf(i == k - 1 ? "%d\n" : "%d ", pb[i].second);
58         }
59     }
60     return 0;
61 }
View Code
posted @ 2013-12-17 05:57  HaibaraAi  阅读(109)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3