Problem Description
Problem Analysis
Given n vertices and n+1 edges, how many ways are there to remove some edges while keeping the graph connected?
By the property of connected graphs, n vertices need at least n-1 edges to be connected.
Since the data range is small,
we try an enumeration approach to check whether the graph is still connected after removing one edge or two edges.
Union-Find can be used to determine whether the graph is connected.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
| #include<stdio.h>
int par[120];
struct node {
int n,m;
} a[120];
int find(int m) {
if(m==par[m])
return m;
else
return par[m]=find(par[m]);
}
void unite(int x,int y) {
x=find(x);
y=find(y);
if(x==y)
return ;
else
par[y]=x;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
int n;
scanf("%d",&n);
for(int i=0; i<n+1; i++) {
scanf("%d %d",&a[i].n,&a[i].m);
}
int res=0;
for(int i=0; i<n+1; i++) {
for(int j=i; j<n+1; j++) {
for(int k=1; k<=n; k++)
par[k]=k;
for(int k=0; k<n+1; k++) {
if(k==i||k==j)
continue;
unite(a[k].n,a[k].m);
}
int k;
for(k=1; k<n; k++) {
if(find(n)!=find(k))
break;
}
if(k==n)
res++;
}
}
printf("%d\n",res);
}
return 0;
}
|