题目链接:http://poj.org/problem?id=1659

思路:  havel算法的应用:

(1)对序列从大到小进行排序。

(2)设最大的度数为 t ,把最大的度数置0,然后把最大度数后(不包括自己)的 t 个度数分别减1(意思就是把度数最大的点与后几个点进行连接)

(3)如果序列中出现了负数,证明无法构成。如果序列全部变为0,证明能构成,跳出循环。前两点不出现,就跳回第一步!

简单例子:

4 4 3 3 2 2

第二步后0 3 2 2 1 2

排完续后3 2 2 2 1 0

第二步后0 1 1 1 1 0

排完续后1 1 1 1 0 0

第二步后0 0 1 1 0 0

排完续后1 1 0 0 0 0

第二步后0 0 0 0 0 0

全为0,能构成图,跳出!

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 14 struct Node{
int num,id;
}pp[MAXN]; int n;
int map[MAXN][MAXN]; int cmp(const Node &p,const Node &q)
{
return p.num>q.num;
} int main()
{
int _case;
scanf("%d",&_case);
while(_case--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&pp[i].num);
pp[i].id=i;
}
memset(map,,sizeof(map));
bool flag=true;
while(true){
sort(pp+,pp+n+,cmp);
if(pp[].num==)break;
for(int i=;i<=pp[].num;i++){
pp[+i].num--;
if(pp[+i].num<)flag=false;
map[pp[].id][pp[+i].id]=map[pp[+i].id][pp[].id]=;
}
pp[].num=;
if(!flag)break;
}
if(flag){
puts("YES");
for(int i=;i<=n;i++){
printf("%d",map[i][]);
for(int j=;j<=n;j++){
printf(" %d",map[i][j]);
}
printf("\n");
}
}else
puts("NO");
if(_case)puts("");
}
return ;
}

poj 1659(havel算法)的更多相关文章

  1. 转(havel 算法)

    http://www.cnblogs.com/wally/p/3281361.html poj 1659(havel算法) 题目链接:http://poj.org/problem?id=1659 思路 ...

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

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

  3. poj 1659 Frogs&#39; Neighborhood 度序列可图化 贪心

    题意: 对一个无向图给出一个度序列,问他是否可简单图化. 分析: 依据Havel定理,直接贪心就可以. 代码: //poj 1659 //sep9 #include <iostream> ...

  4. poj 1659 Frog's Neighborhood

    未名湖附近共有N个大小湖泊L1, L2, -, Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只 ...

  5. Frogs‘ Neighborhood(POJ 1659 C/C++)

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

  6. POJ 1659 Frogs' Neighborhood (Havel定理构造图)

    题意:根据图的度数列构造图 分析:该题可根据Havel定理来构造图.Havel定理对可图化的判定: 把序列排成不增序,即d1>=d2>=……>=dn,则d可简单图化当且仅当d’={d ...

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

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

  8. poj 2195 KM算法

    题目链接:http://poj.org/problem?id=2195 KM算法模板~ 代码如下: #include "stdio.h" #include "string ...

  9. poj 1659 Frogs' Neighborhood (DFS)

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

随机推荐

  1. HBuilder 打包 vue-cli 构建的 APP

    1.在打包之前需要修改一个地方,那就是config->index.js文件,修改assetsPublicPath: '/'为assetsPublicPath: './',截图如下 上面文件改好后 ...

  2. Vue 作用域插槽

    使用场景:复用子组件的slot,又可以使slot的内容不一样. <!DOCTYPE html> <html lang="zh"> <head> ...

  3. [leetcode]Path Sum--巧用递归

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  4. [物理题+枚举] hdu 4445 Crazy Tank

    题意: 给你N个炮弹的发射速度,以及炮台高度H和L1,R1,L2,R2. 问任选发射角度.最多能有几个炮弹在不打入L2~R2的情况下打入L1~R1 注意:区间有可能重叠. 思路: 物理题,发现单纯的依 ...

  5. Ubuntu Server 12.04 安装 Jabberd2 服务器

    本篇文章由:http://xinpure.com/install-ubuntu-server-12-04-jabberd2-server/ Ubuntu Server 12.04 安装 Jabberd ...

  6. Image.Save出错 GDI 一般错误

    一般是路径问题 需要转化为绝对路径 \转换为/ if (HttpContext.Current!=null) outPath = HttpContext.Current.Server.MapPath( ...

  7. centos7 安装php 多线程pthreads

    第一步:yum源更新 yum update 第二步:下载php7源码 wget http://124.202.164.8/files/312100000A0BB72D/cn.php.net/distr ...

  8. vivado error [Labtools 27-2149]

    使用vivado2014.3时遇到了这种错误 ERROR: [Labtools 27-2149] File E:/project/V7_PCIE/prj/PhasedProject/M_PcieGen ...

  9. vue路由使用

    <body> <div id="itany"> <div> <!--使用router-link组件来定义导航,to属性指定链接url--& ...

  10. 使用struts的时候form用struts的,不用html本身的

    同样的struts-config.xml, web.xml的配置,使用2个版本的form,只有struts的form才能成功运行 使用html版本的form导致post的路径不对,报404 HTML版 ...