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. 计算机网络(TCP/IP)

    概述:网络协议通常分不同的层次进行开发,每一层分别不同的通信功能.TCP/IP通常分为4层协议系统. 1.链路层,有时也称为数据链路层或者网络接口层,通常包括操作系统中的设备驱动程序和计算机中对应的网 ...

  2. ORACLE触发器的自治事务的注意事项

    直接上代码: Create OR replace Trigger TR_ROBXMX_CLDJBHHX After INSERT OR UPDATE OR DELETE ON ROBXMX1 --要监 ...

  3. C# enum枚举知识总结

    C#中除了简单的变量类型外,还提供了三种较为复杂的变量类型,包括枚举.结构和数组.本文主要讲述枚举相关的知识. 枚举类型(也称为枚举),提供了一种有效的方式,来定义可能分配给变量的一组已命名的整数常量 ...

  4. python selenium1 模拟点击 测试对象 58同城验证码

    #!/usr/bin/python# -*- coding: UTF-8 -*-# @Time : 2019/12/5 17:30# @Author : shenghao/10347899@qq.co ...

  5. python之random库的使用以及程序的异常处理

    1.random库的使用: random库是使用随机数的Python标准库从概率论角度来说,随机数是随机产生的数据(比如抛硬币),但时计算机是不可能产生随机值,真正的随机数也是在特定条件下产生的确定值 ...

  6. FFmpeg里面的时间单位

    pts单位:1/90 ms(每个单位代表1/90 ms)  RTP包头有个STAMP 对于视频 STAMP/90 就是 PTS (毫秒) 对于音频 STAMP/samplerate * 1000 才是 ...

  7. 如何部署自定义的servlet

    1 首先找到与exlipse绑定的tomcat安装位置 2 打开web.xml添加如下信息: <display-name>servletDemo</display-name> ...

  8. JavaWeb-用过滤器修改请求的返回状态码

    问题: 客户SDK对接服务,默认只识别200和非200的请求状态码.需要修改当前应用的状态码(如将201转为200) 解决方案:通过扩展HttpServletResponseWrapper,获取到每个 ...

  9. android项目笔记整理(2)

    31.利用SharedPreferences存储时间     读取时间:     SharedPreferences sp=this.getSharedPreferences("actm&q ...

  10. Spring Cloud(十)高可用的分布式配置中心 Spring Cloud Config 中使用 Refresh

    上一篇文章讲了SpringCloudConfig 集成Git仓库,配和 Eureka 注册中心一起使用,但是我们会发现,修改了Git仓库的配置后,需要重启服务,才可以得到最新的配置,这一篇我们尝试使用 ...