Connected Graph
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3156 | Accepted: 1533 |
Description
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.
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
Source
n个点之间任取两点连边,按照组合数公式,共有$ C(n,2)=n*(n-1)/2 $条边可连
每条边可连可不练,所以总情况有 P=2^C(n,2) 种。
我们要求的是所有点都连通的情况数,可以用总数P减去不连通的情况数
设F[i]为i个点构成连通图的情况数,任取一点为基准,当与其构成连通图的点有j-1个时,共有F[j]种连通情况。则若在总图中有j个点一定连通,共有$C(i-1,j-1)*F[j] $种情况,而剩下的点可以随意连边,共有$2^C(i-j,2)$种情况。
若总点数为i,则答案为:$F[i]=P[i]-sum$; sum=sum+(C(i-1,j-1)*F[j]*2^C(i-j,2)) {1<=j<i 累加求和}
然而高精度各种写不对,我选择死亡。
先放一张表
1 INPUT: 1 2 OUTPUT: 1 3 INPUT: 2 4 OUTPUT: 1 5 INPUT: 3 6 OUTPUT: 4 7 INPUT: 4 8 OUTPUT: 38 9 INPUT: 5 10 OUTPUT: 728 11 INPUT: 6 12 OUTPUT: 26704 13 INPUT: 7 14 OUTPUT: 1866256 15 INPUT: 8 16 OUTPUT: 251548592 17 INPUT: 9 18 OUTPUT: 66296291072 19 INPUT: 10 20 OUTPUT: 34496488594816 21 INPUT: 11 22 OUTPUT: 35641657548953344 23 INPUT: 12 24 OUTPUT: 73354596206766622208 25 INPUT: 13 26 OUTPUT: 301272202649664088951808 27 INPUT: 14 28 OUTPUT: 2471648811030443735290891264 29 INPUT: 15 30 OUTPUT: 40527680937730480234609755344896 31 INPUT: 16 32 OUTPUT: 1328578958335783201008338986845427712 33 INPUT: 17 34 OUTPUT: 87089689052447182841791388989051400978432 35 INPUT: 18 36 OUTPUT: 11416413520434522308788674285713247919244640256 37 INPUT: 19 38 OUTPUT: 2992938411601818037370034280152893935458466172698624 39 INPUT: 20 40 OUTPUT: 1569215570739406346256547210377768575765884983264804405248 41 INPUT: 21 42 OUTPUT: 1645471602537064877722485517800176164374001516327306287561310208 43 INPUT: 22 44 OUTPUT: 3450836972295011606260171491426093685143754611532806996347023345844224 45 INPUT: 23 46 OUTPUT: 14473931784581530777452916362195345689326195578125463551466449404195748970496 47 INPUT: 24 48 OUTPUT: 121416458387840348322477378286414146687038407628418077332783529218671227143860518912 49 INPUT: 25 50 OUTPUT: 2037032940914341967692256158580080063148397956869956844427355893688994716051486372603625472 51 INPUT: 26 52 OUTPUT: 68351532186533737864736355381396298734910952426503780423683990730318777915378756861378792989392896 53 INPUT: 27 54 OUTPUT: 4586995386487343986845036190980325929492297212632066142611360844233962960637520118252235915249481987129344 55 INPUT: 28 56 OUTPUT: 615656218382741242234508631976838051282411931197630362747033724174222395343543109861028695816566950855890811486208 57 INPUT: 29 58 OUTPUT: 165263974343528091996230919398813154847833461047104477666952257939564080953537482898938408257044203946031706125367800496128 59 INPUT: 30 60 OUTPUT: 88725425253946309579607515290733826999038832348034303708272765654674479763074364231597119435621862686597717341418971119460584259584 61 INPUT: 31 62 OUTPUT: 95268202520385449790227094691687836722278710954949736428196756305746453532341035148366531266372862653739009088659598082113309304400438624256 63 INPUT: 32 64 OUTPUT: 204586909944926298207861553173799965921067126517774603507480126827588404754232387878919170016875623577048105576068684204467114231315623298308706926592 65 INPUT: 33 66 OUTPUT: 878694093745349914731889727208157807680003171098920968952145189548012830636076748530741378813207711246134152874638123892704663922045456803250047261786444398592 67 INPUT: 34 68 OUTPUT: 7547924819767483287594694542205326068855891655862820018679189530528628155893698967796630219069788201405972928386025644172169109953194652176102437455457970998547197198336 69 INPUT: 35 70 OUTPUT: 129672361263353660216004848405397154497075914498088480263529787446798464815868889966259599220355751574955667311875199310825316757090836792227021420332597263591744872066219249762304 71 INPUT: 36 72 OUTPUT: 4455508410978470003213152055317479855991723332650114280703483486331017198541367912550307040027205813596014620050254013798901452927850711294962075802234712748298505435020109941966616435621888 73 INPUT: 37 74 OUTPUT: 306180206751230090930313674296749763317292930219833760674864513181351793147422958983304199997791891477494238067606067864147691875149221011750587805454462256284237767964756224079011437145490032917741568 75 INPUT: 38 76 OUTPUT: 42081087200752140195116730773102052524009718837902621183664949269856744858385083976643391056195246283737633254986683196506525739229100562028667655727478159896469450443625002559600024194689577683162985133342982144 77 INPUT: 39 78 OUTPUT: 11567161173227696466220457283329529101751379197153495724502457893891478829937149071434453800538222228465001645119757350054456753856800058471020811256328606811309950183460999195585736337722940242137574318489684508433109221376 79 INPUT: 40 80 OUTPUT: 6359114105601017351375465630036218352726964545083913061809864302427743340641476112983635151514041188995967358659226381513838435962182371853731281705837980150384424607870600516842502175922529566100381861494213531965265765000213275082752 81 INPUT: 41 82 OUTPUT: 6991919901710702396948942815573257427744311018004588489866790612959056357721564695830748688904669995738081555372234543689358610668809196548322563461899302515136978058611651369187392760821440875968116963440793130046454847480988052748303630065467392 83 INPUT: 42 84 OUTPUT: 15375394465098365435098131065240195173750887603455691084898736566282027607324662718653380384318359771738669872579070523864682029424324656980343742654131923883848453279046887366030428581980234722002609397042921130626427482776226373410811403774539364168814821376 85 INPUT: 43 86 OUTPUT: 67621699984704009571087635348261788647460730411971168452281282746962798999895717916292043207408657855232972628889146834646084600650980317820241001687549180689983916950502853108787655643356237905731863505593837387547463783553663104052737827256888296815897621036524900450304 87 INPUT: 44 88 OUTPUT: 594806763388137870319868932592503661181879874998563369872608575294390559331829154567126246824792929668641338543467328561106071308881273503814138669414317911219402066314092130747535752627679688399993515689603622744525243838714230998285264232171322066511990049433899384262102238508351488 89 INPUT: 45 90 OUTPUT: 10463951242026625501784363274596214619943325701401522513836100192928357652762255136769619473700702276949844553770347735730521468871772581157963359677917896206658361141741863952608795675733168160935829452838892433190712974942475048711118429563334205007874224852816312589287727030417085994911901155328 91 INPUT: 46 92 OUTPUT: 368167554019320956145827247050509963076959450983143444578072117098399777382502455552633802915095691807005512740224345254318634273382517137823997743877511866703540358482988273801636313118482363728678083259725882776454656507629131210255280738244476783496709369751571318821222548711309212127848471930415455355797504 93 INPUT: 47 94 OUTPUT: 25907488423318455274080473672019976083009208996271003791416218114322853582878049179546761491016196610119349803222490393175612695149120594742502991139032865749979736985340247224801444473477196529096332604358326020598992443433363048888842556850935198901353471923472154386768107635993449205071378228596636214817388982756553261056 95 INPUT: 48 96 OUTPUT: 3646154850293767810262810894999553363628589110640769385457986485984919161321600546344826908488589572223649058216506920510786720770519258252897810249930214560211056122090333850686659187132094273815095247787669459869137017783625755540375408272361426098383313551230976557640520636974573279383371834513917048967432546435999569365350430111956992 97 INPUT: 49 98 OUTPUT: 1026301351570055077911628972867042177680735585635225345203536190737910863123857244548313982876228994987864700400759811456244128889754306386459557887432298148719591734971030611474690885904247396313959818854940592795291449937598794070517570167551607950979266237997797283563645242105244737520881371410960067902176629829514256225641238164014573644333472284672 99 INPUT: 50 100 OUTPUT: 577756298062641319815321284633539861082132919998722885657507672188606317696301924134068233518707877841769252356274834883678320922291785288952259324960085933885572481476441044041666245632947630667669900623389069655523344952222114179660086674251300523449279256078271770682664276058349275922600493471476178420154378012048571333436567365397136152469165480980158369042006016
然后是我一直改不对的代码
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 struct bgnum{ 8 int l; 9 int a[600]; 10 bgnum operator + (const bgnum &x) const{ 11 bgnum ans; 12 memset(ans.a,0,sizeof(ans.a)); 13 int len=max(l,x.l); 14 ans.l=1; 15 for(int i=1;i<=len;i++){ 16 ans.a[i]+=a[i]+x.a[i]; 17 ans.a[i+1]+=ans.a[i]/10; 18 ans.a[i]%=10; 19 20 } 21 len++; 22 while(!ans.a[len]&&len)len--; 23 ans.l=len; 24 return ans; 25 } 26 bgnum operator - (const bgnum &x) const{ 27 bgnum ans; 28 memset(ans.a,0,sizeof(ans.a)); 29 for(int i=1;i<=l;i++){ 30 ans.a[i]+=a[i]-x.a[i]; 31 if(ans.a[i]<0){ 32 ans.a[i]+=10; 33 ans.a[i-1]--; 34 } 35 } 36 ans.l=l; 37 while(!ans.a[ans.l] && ans.l) ans.l--; 38 return ans; 39 } 40 bgnum operator * (const bgnum &x) const{ 41 bgnum ans; 42 memset(ans.a,0,sizeof(ans.a)); 43 for(int i=1;i<=l;i++) 44 for(int j=1;j<=x.l;j++){ 45 ans.a[i+j-1]+=a[i]*x.a[j]; 46 ans.a[i+j]+=ans.a[i+j-1]/10; 47 ans.a[i+j-1]%=10; 48 } 49 int len=l+x.l; 50 while(!ans.a[len] && len)len--; 51 ans.l=len; 52 return ans; 53 } 54 }f[60],//[i]个点构不同图的方案数 55 c[60][60],//[i]个点中选[j]个任意连边的方案数 56 mi[1600],//2的[i]次方 57 sum; 58 59 60 void Print(bgnum p){ 61 for(int i=p.l;i>=1;i--){ 62 printf("%d",p.a[i]); 63 } 64 printf(" "); 65 return; 66 } 67 bgnum p1,p2; 68 int main(){ 69 p1.l=1;p1.a[1]=1;//高精度数1 70 p2.l=1;p2.a[1]=2;//高精度数2 71 int i,j; 72 mi[0]=p1; 73 for(i=1;i<=1250;i++) 74 mi[i]=mi[i-1]*p2; 75 for(i=0;i<=50;i++) 76 c[i][0]=p1; 77 for(i=1;i<=50;i++) 78 for(j=1;j<=i;j++){ 79 c[i][j]=c[i-1][j]+c[i-1][j-1];//组合数递推公式 80 } 81 for(i=1;i<=50;i++){ 82 sum.l=1; 83 memset(sum.a,0,sizeof(sum.a)); 84 for(j=1;j<i;j++){ 85 sum=sum+(c[i-1][j-1]*f[j]*mi[(i-j)*(i-j-1)/2]); 86 } 87 // Print(sum); 88 f[i]=mi[i*(i-1)/2]-sum; 89 } 90 int n; 91 scanf("%d",&n); 92 Print(f[n]); 93 return 0; 94 }
再放隔壁某dalao的AC题解
http://blog.csdn.net/orion_rigel/article/details/51812864