Connected Graph

POJ - 1737

An undirected graph is a set V of vertices and a set of E∈{V*V} edges.An undirected graph is connected if and only if for every pair (u,v) of vertices,u is reachable from v. 
You are to write a program that tries to calculate the number of different connected undirected graph with n vertices. 
For example,there are 4 different connected undirected graphs with 3 vertices. 

Input

The input contains several test cases. Each test case contains an integer n, denoting the number of vertices. You may assume that 1<=n<=50. The last test case is followed by one zero.

Output

For each test case output the answer on a single line.

Sample Input

1
2
3
4
0

Sample Output

1
1
4
38 题意:求n个点的联通图个数 sol:就是所有图-不连通的个数,令P[i]表示i个点的图的所有情况,那么P[i]=2i*(i-1)/2,Ans[i]表示答案
枚举j∑(1,i-1)表示节点1所在的联通块大小,Ans[i]=P[i]-Ans[j]*C(i-1,j-1)*P[i-j]
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=; bool f=; char ch=' ';
while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
while(isdigit(ch)) {s=(s<<)+(s<<)+(ch^); ch=getchar();}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<) {putchar('-'); x=-x;}
if(x<) {putchar(x+''); return;}
write(x/); putchar((x%)+'');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int power=,Base=;
struct Int
{
int a[];
Int() {memset(a,,sizeof a);}
Int(int x)
{
memset(a,,sizeof a);
// cout<<"x="<<x<<endl;
while(x>)
{
a[++a[]]=x%Base; x/=Base;
}
}
inline void print()
{
int i;
write(a[a[]]);
for(i=a[]-;i>=;i--)
{
if(a[i]<) putchar('');
if(a[i]<) putchar('');
if(a[i]<) putchar('');
write(a[i]);
}
}
}Bin[],C[][],Ans[];
#define P(x) x.print(),putchar(' ')
#define Pl(x) x.print(),putchar('\n')
inline Int operator+(Int p,Int q)
{
int i; Int res=p; res.a[]=max(p.a[],q.a[]);
for(i=;i<=q.a[];i++)
{
res.a[i]+=q.a[i];
res.a[i+]+=res.a[i]/Base;
res.a[i]%=Base;
}
while(res.a[res.a[]+]) res.a[]++;
return res;
}
inline Int operator-(Int p,Int q)
{
int i; Int res=p;
for(i=;i<=q.a[];i++)
{
res.a[i]-=q.a[i];
if(res.a[i]<)
{
res.a[i+]--; res.a[i]+=Base;
}
}
while(!res.a[res.a[]]) res.a[]--;
return res;
}
inline Int operator*(Int p,int q)
{
int i; Int res=p;
for(i=;i<=res.a[];i++) res.a[i]*=q;
for(i=;i<=res.a[];i++)
{
res.a[i+]+=res.a[i]/Base; res.a[i]%=Base;
}
while(res.a[res.a[]+])
{
res.a[]++; res.a[res.a[]+]+=res.a[res.a[]]/Base; res.a[res.a[]]%=Base;
}
return res;
}
inline Int operator*(Int p,Int q)
{
int i,j; Int res; res.a[]=p.a[]+q.a[];
for(i=;i<=p.a[];i++) for(j=;j<=q.a[];j++)
{
res.a[i+j-]+=p.a[i]*q.a[j];
res.a[i+j]+=res.a[i+j-]/Base;
res.a[i+j-]%=Base;
}
while(!res.a[res.a[]]) res.a[]--;
return res;
}
int main()
{
// freopen("data.in","r",stdin);
int i,j,n;
Bin[]=Int(); for(i=;i<=;i++) Bin[i]=Bin[i-]*;
// for(i=1;i<=32;i++) Pl(Bin[i]);
C[][]=Int();
for(i=;i<=;i++)
{
C[i][]=Int();
for(j=;j<=i;j++) C[i][j]=C[i-][j]+C[i-][j-];
}
// for(i=0;i<=4;i++)
// {
// for(j=0;j<=i;j++) P(C[i][j]);
// puts("");
// }
Ans[]=Int();
for(i=;i<=;i++)
{
Ans[i]=Bin[i*(i-)/];
for(j=;j<i;j++)
{
Ans[i]=Ans[i]-Ans[j]*C[i-][j-]*Bin[(i-j)*((i-j)-)/];
}
}
for(;;)
{
R(n); if(n==) break; Pl(Ans[n]);
}
return ;
}
/*
input
1
2
3
4
0
output
1
1
4
38
*/
 

poj1737的更多相关文章

  1. 【poj1737】 Connected Graph

    http://poj.org/problem?id=1737 (题目链接) 题意 求n个节点的无向连通图的方案数,不取模w(゚Д゚)w Solution 刚开始想了个第二类斯特林数,然而并不知道怎么求 ...

  2. POJ1737 Connected Graph

    Connected Graph Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3156   Accepted: 1533 D ...

  3. 【Java】【高精度】【组合数】【递推】poj1737 Connected Graph

    http://blog.csdn.net/sdj222555/article/details/12453629 这个递推可以说是非常巧妙了. import java.util.*; import ja ...

  4. [poj1737]Connected Graph(连通图计数)

    题意:输出题中带有$n$个标号的图中连通图的个数. 解题关键: 令$f(n)$为连通图的个数,$g(n)$为非联通图的个数,$h(n)$为总的个数. 则$f(n) + g(n) = h(n)$ 考虑标 ...

  5. $Poj1737\ Connected\ Graph$ 计数类$DP$

    AcWing Description 求$N$个节点的无向连通图有多少个,节点有标号,编号为$1~N$. $1<=N<=50$ Sol 在计数类$DP$中,通常要把一个问题划分成若干个子问 ...

  6. OJ题目分类

    POJ题目分类 | POJ题目分类 | HDU题目分类 | ZOJ题目分类 | SOJ题目分类 | HOJ题目分类 | FOJ题目分类 | 模拟题: POJ1006 POJ1008 POJ1013 P ...

  7. 【bzoj3456】城市规划(多项式求逆+dp)

    Description 求\(~n~\)个点组成的有标号无向连通图的个数.\(~1 \leq n \leq 13 \times 10 ^ 4~\). Solution 这道题的弱化版是poj1737, ...

  8. 0x5C 计数类DP

    cf 559C 考虑到黑色的格子很少,那么我把(1,1)变成黑色,然后按每个黑色格子接近终点的程度排序,计算黑色格子不经过另一个黑色格子到达终点的方案,对于当前的格子,要减去在它右下角的所有方案数(注 ...

  9. 【2018.10.4】CXM笔记(图论)

    .1.给你一个无向图,问这张图的最小割是否唯一.输出yes或no. 跑一边最大流,那么最小割的那些正向边一定满流(也就是过不了了).所以在残余网络上,从S到T和从T到S各广搜找一组最小割的边(即正向边 ...

随机推荐

  1. C库函数:scanf、fscanf、printf、fprintf、sprintf、 snprintf

    1. scanf 函数原型 int scanf(const char *format, ...);  功能:从标准输入 stdin 读取格式化输入. 2.fscanf 函数原型 int fscanf( ...

  2. 第11章:使用Python打造MySQL专家系统

    1.Python语言高级特性 1).深入浅出Python生成器 1).生成器函数:与普通函数定义类似,使用yield语句而不是return语句返回结果.yield语句一次返回一个结果,在每个结果中间挂 ...

  3. 分享一些JVM常见的面试题(转)

    出处:  分享一些JVM常见的面试题 前言: 在遨游了一番 Java Web 的世界之后,发现了自己的一些缺失,所以就着一篇深度好文:知名互联网公司校招 Java 开发岗面试知识点解析 ,来好好的对 ...

  4. 审计一套CMS中的SQL注入

    漏洞分为系统漏洞和应用漏洞,系统漏洞以二进制漏洞为代表,其挖掘难度较高需要对反汇编和操作系统原理深入理解,而除了系统漏洞以外还有一些应用漏洞,包括不限MySQL,Apache,为代表的Web漏洞,这里 ...

  5. git、git bash、git shell

    git 一个快速的分布式版本控制系统(工具),支持该工具的网站有Github等. shell 是linux.unix系统的外壳(区别于核),用于输入并执行命令(命令解析器). 它类似于DOS下的com ...

  6. Sql Server 收缩日志文件原理及always on 下的实践

    一.准备知识 1.LSN LSN用来标识特定日志在日志文件中位置(详情请见什么是LSN:日志序列号),它由两部分组成:一部分用来标识VLF(虚拟日志文件)的序列号,剩下的用来标识该日志在VLF中的具体 ...

  7. ListUtils的简单集合操作和原理

    1 Maven依赖 <dependency> <groupId>org.apache.commons</groupId> <artifactId>com ...

  8. layer,备受青睐的web弹层组件

    //http://layer.layui.com/ 特别说明:事件需自己绑定,以下只展现调用代码. //初体验 layer.alert('内容') //第三方扩展皮肤 layer.alert('内容' ...

  9. DX使用随记--ImageComboBoxEdit

    1. 增加按钮: Combo_订单类型.Properties.Items.Clear()Select Case Combo_客户名称.EditValue Case "ABC" Co ...

  10. python 中startswith()和endswith() 方法

    startswith()方法 Python startswith() 方法用于检查字符串是否是以指定子字符串开头如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在 ...