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. 2.安装阿里yum源

    1.删除自带的yum源:#cd /etc/yum.repos.d/#rm -rf *   2.配置远程yum源:wget -O /etc/yum.repos.d/CentOS-Base.repo ht ...

  2. 【IntelliJ IDEA】添加一个新的tomcat,tomcat启动无法访问欢迎页面,空白页,404

    ===================================第一部分,添加一个tomcat================================================== ...

  3. Java语言中:float、double数据类型在内存中是如何存储的

    引用参考 https://www.cnblogs.com/chenmingjun/p/8415464.html#4291528 https://blog.csdn.net/yansmile1/arti ...

  4. C语言如何才能使用bool类型

    解决办法:加上头文件#include<stdbool.h> ,就可以直接使用类型bool = true/false; #include<stdio.h> 2 #include& ...

  5. 4. Java入门程序

    以eclipse为例,建立一个简单的Java程序. 首先启动eclipse,进入到如下主页面: 新建一个项目,选择“File-New-Java Project”: 弹出了一个如下页面,假设命名为Tes ...

  6. css对应rgb码表16进制

  7. LeetCode:184.部门工资最高的员工

    题目链接:https://leetcode-cn.com/problems/department-highest-salary/ 题目 Employee 表包含所有员工信息,每个员工有其对应的 Id, ...

  8. DVA-subscriptions

    import { routerRedux } from 'dva/router' export default { namespace: 'notice', state: { notices:[], ...

  9. C++ STL 之 函数对象

    重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象,也叫仿函数(functor),其实就是重载“()”操作符,使得类对象可以像函数那样调用.注意 ...

  10. 16.Listener(监听器)

    /*监听器*/ java的事件监听机制(主要是对一些web元素的监听 (ServletContext(计时器),HttpSession和ServletRequest)) 1.事件监听涉及到三个组件:事 ...