现在的位置: 首页 > 综合 > 正文

uva 10720 – Graph Construction

2013年08月10日 ⁄ 综合 ⁄ 共 2076字 ⁄ 字号 评论关闭

Problem C

Graph Construction

Time Limit

2 Seconds

Graph is a collection of edges E and vertices
V. Graph has a wide variety of applications in computer. There are different ways to represent graph in computer. It can be represented by adjacency matrix or by adjacency list. There are some other ways to represent graph. One of them is to
write the degrees (the numbers of edges that a vertex has) of each vertex. If there are
n vertices then n integers can represent that graph. In this problem we are talking about simple graph which does not have same endpoints for more than one edge, and also does not have edges with the same endpoint.

Any graph can be represented by n number of integers. But the reverse is not always true. If you are given
n integers, you have to find out whether this n numbers can represent the degrees of
n vertices of a graph.

Input
Each line will start with the number n (≤ 10000). The next
n integers will represent the degrees of n vertices of the graph. A 0 input for n will indicate end of input which should not be processed.

Output
If the n integers can represent a graph then print “Possible”. Otherwise print “Not possible”. Output for each test case should be on separate line.

Sample Input

Output for Sample Input

4 3
3
3 3
6 2 4 5
5
2 1
5 3 2 3 2 1
0

Possible
Not possible
Not possible


Problemsetter:
Md.
Bahlul
Haider
Judge Solution: Mohammed Shamsul Alam
Special thanks to Tanveer Ahsan
补充了个知识点,自己只能想到n个节点图的度数和小于等于n*(n-1),每个节点度数小于n,度数和为偶数;
学习了个定理:

Havel定理:
   描述: 我们把序列排成不增序,即d1>=d2>=...>=dn,则d可简单图化当且仅当d'=(d2-1, d3-1, ... d(d1+1)-1, d(d1+2), d(d1+3), ... dn)可简单图化。这个定理写起来麻烦,实际上就是说,我们把d排序以后,找出度最大的点(设度为d1),把它和度次大的d1个点之间连边,然后这个点就可以不管了,一直继续这个过程,直到建出完整的图,或出现负度等明显不合理的情况。

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

  其实仔细想想,就是贪心的构建一个图,一条边对应2个点产生两个度数,每次拿掉一个度数最大的点删掉与之相连的边(有点拓扑排序的意思),与之相连的点度数自然-1;因为要尽可能连成图,当然是让度数大的点度数减少1,否则度数小的点减没了,就不能构成图了;
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
 int n,s,a[10000],f,i;
 while (scanf("%d",&n),n)
 {
  s=0; f=0;
  for (i=1;i<=n;i++)
  {
   scanf("%d",&a[i-1]);
   s+=a[i-1];
   if (a[i-1]>n) f=1;
  }
  if (s>n*(n-1) || s%2 || f) printf("Not possible\n");
  else
  {n--;
   while (n)
   {
    sort(a,a+n);
    if (a[n]) {
               for (i=n-1;i>=n-a[n]&&i>=0;i--)
               {--a[i]; if (a[i]<0) f=1;}
              }
     --n;
    if (f) break;
    }
       if (f) printf("Not possible\n");
         else printf("Possible\n");
  }
 }
 return 0;
}

抱歉!评论已关闭.