ZOJ3732 Graph Reconstruction Havel-Hakimi定理
分析:
给定一个非负整数序列{dn},若存在一个无向图使得图中各点的度与此序列一一对应,则称此序列可图化。
进一步,若图为简单图,则称此序列可简单图化 (来自百度百科)
可简单图化的判定可以用Havel-Hakimi定理,然后简述 Havel-Hakimi定理
Havel-Hakimi定理的过程:
1,按度数排序。
2,选取度数最大的点,如果该点度数为0,结束,有解
3,每次选一个度数最大的点,然后将后面的点的度数依次减1,表示该顶点和相应的顶点有边相连,
如果有点的度数减到负数,结束,无解。
4,重复进行步骤1
然后这个题不只是判定有没有解,还需要判断是否有多解
“个人认为此题的难点在于多解时要输出两个。在Havel-Hakimi定理的构造过程中,如果把某两个点“互换”,那么就可以构造出多解的,
那么什么样的两个点才可以互换呢,比如我现在已经排完序,要减的度数序列一直到p,这时,如果p+1的点的度数和p是相同的,
那么p位置和p+1是可以"互换“的,连这两个点中的哪一个都不会影响结果。”(该段文字引用网上某大牛的想法)
所以通过判断p和p+1是否相等,就可以判断是否多解
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=1e2+;
const int INF=0x3f3f3f3f;
int a[N],d[N],c[N],n;
bool cmp(int x,int y)
{
return c[x]>c[y];
}
bool check()
{
for(int i=;i<=n;++i)c[i]=d[i];
for(int i=; i<=n; ++i)
{
sort(a+,a++n,cmp);
if(!c[a[]])break;
for(int j=,cnt=; j<=n&&cnt<=c[a[]]; ++j,++cnt)
{
--c[a[j]];
if(c[a[j]]<)return ;
}
c[a[]]=;
}
return ;
}
struct Edge
{
int u,v;
} x[N*N];
bool checkmul()
{
for(int i=; i<=n; ++i)c[i]=d[i];
for(int i=; i<=n; ++i)
{
sort(a+,a++n,cmp);
if(!c[a[]])break;
int k=c[a[]]+;
if(k<=n&&c[a[k]]==c[a[k-]])return ;
for(int j=,cnt=; j<=n&&cnt<=c[a[]]; ++j,++cnt)
--c[a[j]];
c[a[]]=;
}
return ;
}
void print()
{
int tot=;
for(int i=; i<=n; ++i)c[i]=d[i];
for(int i=; i<=n; ++i)
{
sort(a+,a++n,cmp);
if(!c[a[]])break;
for(int j=,cnt=; j<=n&&cnt<=c[a[]]; ++j,++cnt)
{
--c[a[j]];
++tot;
x[tot].u=a[],x[tot].v=a[j];
}
c[a[]]=;
}
for(int i=; i<=tot; ++i)
{
printf("%d",x[i].u);
if(i<tot)printf(" ");
}
printf("\n");
for(int i=; i<=tot; ++i)
{
printf("%d",x[i].v);
if(i<tot)printf(" ");
}
printf("\n");
}
int main()
{
while(~scanf("%d",&n))
{
int sum=;
for(int i=; i<=n; ++i)
{
scanf("%d",&d[i]);
sum+=d[i];
a[i]=i;
}
if(!check())
{
printf("IMPOSSIBLE\n");
continue;
}
if(checkmul())
{
printf("MULTIPLE\n");
printf("%d %d\n",n,sum/);
print();
printf("%d %d\n",n,sum/);
for(int i=; i<=n; ++i)c[i]=d[i];
int tot=;
bool flag=;
for(int i=; i<=n; ++i)
{
sort(a+,a++n,cmp);
if(!c[a[]])break;
if(flag)
{
int k=c[a[]]+;
if(k<=n&&c[a[k]]==c[a[k-]])
swap(a[k],a[k-]),flag=;
}
for(int j=,cnt=; j<=n&&cnt<=c[a[]]; ++j,++cnt)
{
--c[a[j]];
++tot;
x[tot].u=a[],x[tot].v=a[j];
}
c[a[]]=;
}
for(int i=; i<=tot; ++i)
{
printf("%d",x[i].u);
if(i<tot)printf(" ");
}
printf("\n");
for(int i=; i<=tot; ++i)
{
printf("%d",x[i].v);
if(i<tot)printf(" ");
}
printf("\n");
}
else
{
printf("UNIQUE\n");
printf("%d %d\n",n,sum/);
print();
}
}
return ;
}
ZOJ3732 Graph Reconstruction Havel-Hakimi定理的更多相关文章
- POJ1659 Frogs' Neighborhood(Havel–Hakimi定理)
题意 题目链接 \(T\)组数据,给出\(n\)个点的度数,问是否可以构造出一个简单图 Sol Havel–Hakimi定理: 给定一串有限多个非负整数组成的序列,是否存在一个简单图使得其度数列恰为这 ...
- 2013长沙 G Graph Reconstruction (Havel-Hakimi定理)
Graph Reconstruction Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Let there ...
- zoj3732&& hdu4797 Graph Reconstruction
Graph Reconstruction Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Let there ...
- 2013亚洲区域赛长沙站 ZOJ 3732 Graph Reconstruction
题目链接 Graph Reconstruction 题意 给你无向图每个点的度数, 问是否存在唯一解, 存在输出唯一解, 多解输出两个, 无解输出IMPOSSIBLE 思路 这里用到了 Havel-H ...
- Codeforces Round #192 (Div. 1) C. Graph Reconstruction 随机化
C. Graph Reconstruction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/3 ...
- CodeForces-329C(div1):Graph Reconstruction(随机&构造)
I have an undirected graph consisting of n nodes, numbered 1 through n. Each node has at most two in ...
- Spectral Graph Theory的一些定理
邻接矩阵的特征值和特征向量不会随着节点的排列不同而变化.两个图同构可以推出他们的邻接矩阵具有相同的特征值和特征向量,但是反过来不行.
- POJ 1659 Frogs' Neighborhood(可图性判定—Havel-Hakimi定理)【超详解】
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 9897 Accepted: 41 ...
- Codeforces 1091E New Year and the Acquaintance Estimation Erdős–Gallai定理
题目链接:E - New Year and the Acquaintance Estimation 题解参考: Havel–Hakimi algorithm 和 Erdős–Gallai theore ...
随机推荐
- Webstorm 配置与使用 Less
* 安装完NodeJs * 将npm文件夹保存在C:\Users\Administrator\AppData\Roaming\下 * 在webStrom中,setting -> Tools -& ...
- SharePoint2013TimerJob计时器发送邮件
http://www.3fwork.com/b500/000307MYM008190/
- IIS tilde directory enumeration 漏洞以及解决方案
2015年6月16日15:19:24 出现 IIS tilde directory enumeration 漏洞 Acunetix Web Vulnerability Scanner 9.5 测试出 ...
- Cassandra1.2文档学习(6)—— 客户端数据请求
参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/a ...
- centos不能挂在ntfs
root@s 下载]# mount /dev/sdb1 /mnt mount: unknown filesystem type 'ntfs' wget http://www.tuxera.com/co ...
- 前端内容缓存技术:CSI,SSI,ESI
一.CSI (Client Side Includes) 含义:通过iframe.javascript.ajax 等方式将另外一个页面的内容动态包含进来. 原理:整个页面依然可以静态化为html ...
- VB-获取某字符在其中出现的次数
'方法1: Dim str As String " "))) '方法2: Dim n&, j& j = n = , text1.Text, "/ITEMN ...
- python import
在执行 import module 时 会从 1 当前目录 2 pythonpath(可以通过 os.sys.path 查看) 3 python 安装目录 b import 了 a, c impo ...
- mirantis fuel 学习
这些天看了mirantis中puppet的使用,对puppet的认识有了更深入的理解.mirantis公司的fuel主要是为了方便部署生产环境的openstack的工具.主要是在集群中自动化的安装op ...
- hdu 5429 Geometric Progression 高精度浮点数(java版本)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5429 题意:给一段长度不超过100的每个数字(可以是浮点数)的长度不超过1000的序列,问这个序列是否 ...