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. Python之random.seed()用法

    import random # 随机数不一样 random.seed() print('随机数1:',random.random()) random.seed() print('随机数2:',rand ...

  2. mysqlbinlog实战

    关于mysqlbinlog命令,下列参数应用频率较高:--base64-output:选项有三个参数,never表示不处理ROW格式日志,只处理传统的基于STATEMENT格式日志.decode-ro ...

  3. k8s-搭建 EFK 日志系统

    搭建 EFK 日志系统 大家介绍了 Kubernetes 集群中的几种日志收集方案,Kubernetes 中比较流行的日志收集解决方案是 Elasticsearch.Fluentd 和 Kibana( ...

  4. hdu 2189还是dp..

    题目的意思比较简单,类似计数dp. 一开始我想让dp[i]+=dp[i-prime] 每次遍历比i小的所有素数,然后发现有重叠的 比如 2+3 3+2 就导致错误.看了其他人的填充方式,发现定下pri ...

  5. Winform界面GridView中XCDataGridViewCheckBoxAllColumn改变触发事件

    1.首先利用CurrentCellDirtyStateChanged事件 监测状态改变后判断是否有未提交的更改,若有则提交 private void CurrentCellDirtyStateChan ...

  6. 获取windows进程信息及CListCtrl控件(List Control)练习

    环境:VS2010/MFC/对话框 效果图: 目录: 1.  关于windows进程信息获取 2.  CListCtrl的使用 ------------------------------------ ...

  7. jquery model 框设定

    https://www.bootcdn.cn/   国内网址引用 js function searchItemInfo(conditionNo,lotCD,itemKey) { var conditi ...

  8. python并发编程之线程(二):死锁和递归锁&信号量&定时器&线程queue&事件evevt

    一 死锁现象与递归锁 进程也有死锁与递归锁,在进程那里忘记说了,放到这里一切说了额 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将 ...

  9. CentOs Linux 对于编辑文本内容时无法退出的几个小命令

    编辑完保存退出的四种方式 1. Esc+:+wq+回车(w是write,q是quit) 2. Esc+:+x+回车(x=wq) 3. Esc+shift+zz 4. Esc+ZZ(在大写开启下)

  10. 解决Python中出现的问题: “You are using pip version 9.0.1, however version 19.2.3 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.”

    1. 一开始我在使用Pycharm时,导入numpy库,发现导入错误: Non-zero exit code (1) 2. 于是我通过更新的方法来解决,哪知道在更新的时候也出现了错误,错误如下图: 这 ...