题意:根据图的度数列构造图

分析:该题可根据Havel定理来构造图。Havel定理对可图化的判定:

  把序列排成不增序,即d1>=d2>=……>=dn,则d可简单图化当且仅当d’={d2-1,d3-1,……d(d1+1)-1, d(d1+2),d(d1+3),……dn}可简单图化。简单的说,把d排序后,找出度最大的点(设度为d1),把它与度次大的d1个点之间连边,然后这个点就可以不管了,一直继续这个过程,直到建出完整的图,或出现负度等明显不合理的情况。

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn =;
int G[maxn][maxn];
struct Node{
int d,id;
bool operator <(const Node &rhs)const{return d>rhs.d;}
}p[maxn]; int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,M,u,v,tmp;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
memset(G,,sizeof(G));
for(int i=;i<=N;++i){
scanf("%d",&p[i].d);
p[i].id = i;
}
bool flag = true;
while(true){
sort(p+,p+N+);
if(!p[].d) break;
int u = p[].id;
for(int j=;p[].d &&j<=N;++j){
int v =p[j].id;
if(!p[j].d) continue;
G[u][v] = G[v][u] = ;
p[].d--;
p[j].d--;
}
if(p[].d>){
flag = false;
break;
}
}
if(!flag) printf("NO\n");
else{
printf("YES\n");
for(int i=;i<=N;++i){
for(int j=;j<=N;++j){
printf("%d ",G[i][j]);
}
printf("\n");
}
}
printf("\n");
}
return ;
}

POJ 1659 Frogs' Neighborhood (Havel定理构造图)的更多相关文章

  1. POJ 1659 Frogs' Neighborhood(Havel-Hakimi定理)

    题目链接: 传送门 Frogs' Neighborhood Time Limit: 5000MS     Memory Limit: 10000K Description 未名湖附近共有N个大小湖泊L ...

  2. poj 1659 Frogs' Neighborhood Havel-Hakimi定理 可简单图定理

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098136.html 给定一个非负整数序列$D=\{d_1,d_2,...d_n\}$,若存 ...

  3. poj 1659 Frogs' Neighborhood (DFS)

    http://poj.org/problem?id=1659 Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total S ...

  4. POJ 1659 Frogs' Neighborhood(可图性判定—Havel-Hakimi定理)【超详解】

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 9897   Accepted: 41 ...

  5. POJ 1659 Frogs' Neighborhood (Havel--Hakimi定理)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 10545   Accepted: 4 ...

  6. poj 1659 Frogs' Neighborhood (贪心 + 判断度数序列是否可图)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6076   Accepted: 26 ...

  7. poj 1659 Frogs' Neighborhood( 青蛙的邻居)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 9639   Accepted: 40 ...

  8. poj 1659 Frogs' Neighborhood(出入度、可图定理)

    题意:我们常根据无向边来计算每个节点的度,现在反过来了,已知每个节点的度,问是否可图,若可图,输出一种情况. 分析:这是一道定理题,只要知道可图定理,就是so easy了  可图定理:对每个节点的度从 ...

  9. Poj 1659.Frogs' Neighborhood 题解

    Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和 ...

随机推荐

  1. VUE:使用vue-cli脚手架无法安装npm install axios 的巨坑

    使用命令 npm install axios 安装axios可能会报错,无法引用, 这个时候使用淘宝的镜像cnpm安装就可以了 cnpm install axios 如果没有安装cnpm,执行以下命令 ...

  2. win7(64位)+vs2008配置Directshow

    参考链接:http://zhuyanfeng.com/archives/1663 PC环境:win7 64bit + vs2008 1.下载64位的win7 SDK2.安装过程中遇到错误(必须要卸载v ...

  3. IOS 分节表视图

    分节表视图 简单的表视图仅仅有一个节,它实际上是分节表视图的一个特例.一个表能够有多个节,节也有头有脚,分节是加入索引和分组的前提. 索引的正确使用原则如所看到的: 1.索引标题不能与显示的标题全然一 ...

  4. cout顺序,i++和++i

    先看下以下代码 #include<iostream> using namespace std; ; int f1() { x = ; return x; } int f2() { x = ...

  5. springside4

    https://github.com/springside/springside4/wiki/Design Design 1. Web MVC Framwork: SpringMVC3.0 Restf ...

  6. Asynchronous calls and remote callbacks using Lingo Spring Remoting

    http://www.jroller.com/sjivan/entry/asynchronous_calls_and_callbacks_using Asynchronous calls and re ...

  7. mysql 暴力破解 root账号密码

    测试数据库的root账号密码大家都忘记了,好吧,那我们就暴力破解吧 1.找到my.cnf vi /etc/my.cnf在[mysqld]的段中加上一句:skip-grant-tables例如:[mys ...

  8. Integer自动装拆箱

    public static void main(String[] args) { Integer a1 = 1; Integer a2 = 1; Integer b1 = 127; Integer b ...

  9. hdu3625(第一类斯特林数)

    与第二类有些区别! #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...

  10. IOS 计算本周的起至日期

    unsigned units=NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit|NSWeekdayCalendarUnit; NSCal ...