Graph theory (Spanning Tree): HDU 5631Rikka with Graph

Source: Internet
Author: User

Rikka with Graph

Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 118 Accepted Submission (s): 52


Problem Descriptionas We know, Rikka is poor at math. Yuta is worrying on this situation, so he gives Rikka some math tasks to practice. There is one of the them:

Yuta have a non-direct graph with n vertices and n+1 edges. Rikka can choose some of the edges (at least one) and delete them from the graph.

Yuta wants to know the number of the ways to choose the edges in order to make the remaining graph connected.

It is too difficult for Rikka. Can you help her?

Inputthe First line contains a numberT(t≤) --the number of the testcases.

For each testcase, the first line contains a numbern(n≤).

Then n+1 lines follow. Each line contains the numbers u,v , which means there are an edge between U and v.

Outputfor each testcase, print a single number.

Sample Input131 22 33 11 3

Sample Output9

Sourcebestcoder Round #73 (Div.2)

Recommendhujie | We have carefully selected several similar problems for you:5634 5633 5632 5630 5629

Test instructions: Give a graph of n-point n+1 edges, you can select some edges (at least one) to delete. How many options are there to make the diagram still connected after deletion?

This is the best coder above the title, but I play the array is small!!!!!

The dog's got a!!!!!!!!!!!!!!!!!!!!! and a wooden heart.

Here I use a combination of mathematics, 0ms (in fact, violence n³ can also be too) ~ ~ ~

First place Oh!!!

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intmaxn= the;6 intedge[maxn][3];7 intFa[maxn],cnt=1,fir[maxn],nxt[maxn<<1],to[maxn<<1];8 intdep[maxn],pre[maxn],ret[maxn],id[maxn<<1];9 intFindintx)Ten { One     returnfa[x]==x?x:fa[x]=find (Fa[x]); A } -  - voidAddedge (intAintBintID) the { -nxt[++cnt]=fir[a];id[cnt]=ID; -to[cnt]=b;fir[a]=CNT; - } +  - voidDFS (intnode) + { A      for(intI=fir[node];i;i=Nxt[i]) at     { -         if(Dep[to[i]])Continue; -dep[to[i]]=dep[node]+1; -pre[to[i]]=i; - DFS (To[i]); -     } in } - intSearch (intXintYintd) to { +      while(x!=y) { -         if(dep[x]<Dep[y]) the swap (x, y); *ret[id[pre[x]]]+=D; $x=to[pre[x]^1];Panax Notoginseng     } - } the voidInit () + { Amemset (Fir,0,sizeof(FIR)); thememset (ret,0,sizeof(ret)); +Cnt=1; - } $  $ intMain () - {     -Freopen ("data.in","R", stdin); theFreopen ("Wrong.out","W", stdout); -     intT,n;Wuyiscanf"%d",&T); the      while(t--) -     { Wu Init (); -scanf"%d",&n); About          for(intI=1; i<=n+1; i++){ $fa[i]=i; -         } -      -          for(intI=1; i<=n+1; i++) Ascanf"%d%d", &edge[i][0],&edge[i][1]); +          for(intI=1; i<=n+1; i++){ the             intA=find (edge[i][0]), B=find (edge[i][1]); -             if(a==b) $edge[i][2]=1; the             Else{ thefa[a]=b; theAddedge (edge[i][0],edge[i][1],i); theAddedge (edge[i][1],edge[i][0],i); -             } in         } the         intflag=0; the          for(intI=2; i<=n;i++) About         if(Find (i)!=find (i)1)){ theprintf"0\n"); theflag=1; the              Break; +         } -         if(flag==1)Continue; thedep[1]=1;BayiDFS (1); the         intans=0, a=0, b=0, c=0; the          for(intI=1; i<=n+1; i++){ -             if(edge[i][2]){ -                 if(!ans) { theSearch (edge[i][0],edge[i][1],1); theret[i]+=1; the                 } the                      -                 Else { theSearch (edge[i][0],edge[i][1],Ten); theret[i]+=Ten; the                 }94ans+=1;  the             } the         } the          for(intI=1; i<=n+1; i++){98             if(ret[i]==1) a++; About             Else if(ret[i]==Ten) b++; -             Else if(ret[i]== One) c++,b++,a++;101         }102printf"%d\n", a+b-c+ (a+b-c-1) * (A+B-C)/2-(A-C) * (a-c-1)/2-(B-C) * (b-c-1)/2-c* (C-1)/2);103     }104     return 0; the}

Graph theory (Spanning Tree): HDU 5631Rikka with Graph

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.