HDU 5631 Rikka with Graph

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34621169/article/details/51316445

B - Rikka with Graph
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: 

Yuta has 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?
 

Input

The first line contains a number  T(T30)T(T≤30)――The number of the testcases. 

For each testcase, the first line contains a number  n(n100)n(n≤100)

Then n+1 lines follow. Each line contains two numbers  u,vu,v , which means there is an edge between u and v.
 

Output

For each testcase, print a single number.
 

Sample Input

 
       
1 3 1 2 2 3 3 1 1 3
 

Sample Output

 
       
9
 

t组测试数据,n个人,下面有n+1条线,问去掉几条线后这整个图依旧连通,求方法...

因为n个人,n+1条线,所以最多需要n-1条线来连通全部的人,所以最多去两条线,然后就是输出有几种不同的方法了

分类做吧,先去掉一条线然后看看连不连通,再去掉两条线,最后数一下有多少种方法了





AC代码:

/*#include <iostream> 
#include <cstdio> 
#include <fstream> 
#include <algorithm> 
#include <cmath> 
#include <deque> 
#include <vector> 
#include <queue> 
#include <string> 
#include <cstring> 
#include <map> 
#include <stack> 
#include <set>
using namespace std;
int a[200];
int find(int x)
{
    if(x!=a[x]) a[x]=find(a[x]);
    return a[x];
}
int main(){
int t,u[200],v[200];
cin>>t;
while(t--){
int n;
cin>>n;
memset(a,0,sizeof(a));
memset(u,0,sizeof(u));
memset(v,0,sizeof(v));
for(int i=1;i<=n+1;i++)
cin>>u[i]>>v[i];
for(int i=1;i<=n;i++)
a[i]=i;
int ans=0;
int unit=n;
for(int j=1;j<=n+1;j++){
           int fu=find(u[j]);
int fv=find(v[j]);
if(fu!=fv){
unit--;
a[fu]=fv;
}
}
if(unit!=1)
ans=0;
else{
for(int i=1;i<=n+1;i++){
for(int k=1;k<=n;k++)
a[k]=k;
unit=n;
for(int j=1;j<=n+1;j++){
if(i==j)
continue;
int fu=find(u[j]);
int fv=find(v[j]);
if(fu!=fv){
unit--;
a[fu]=fv;
}
}
if(unit==1)
ans++;
}
for(int i=1;i<=n+1;i++){
for(int q=i+1;q<=n+1;q++){
for(int k=1;k<=n;k++)
a[k]=k;
unit=n;
for(int j=1;j<=n+1;j++){
if(j==i||j==q)
continue;
int fu=find(u[j]);
int fv=find(v[j]);
if(fu!=fv){
unit--;
a[fu]=fv;
}
}
if(unit==1)
ans++;
}
}
}
cout<<ans<<endl;
}
return 0;
}
*/




#include <iostream> 
#include <cstdio> 
#include <fstream> 
#include <algorithm> 
#include <cmath> 
#include <deque> 
#include <vector> 
#include <queue> 
#include <string> 
#include <cstring> 
#include <map> 
#include <stack> 
#include <set>
using namespace std;
int a[200];
int find(int x)
{
    if(x!=a[x]) a[x]=find(a[x]);
    return a[x];
}
int main(){
int t,u[200],v[200];
cin>>t;
while(t--){
int n;
cin>>n;
memset(a,0,sizeof(a));
memset(u,0,sizeof(u));
memset(v,0,sizeof(v));
for(int i=1;i<=n+1;i++)
cin>>u[i]>>v[i];
for(int i=1;i<=n;i++)
a[i]=i;
int ans=0;
for(int i=1;i<=n+1;i++){
for(int k=1;k<=n;k++)
a[k]=k;
for(int j=1;j<=n+1;j++){
if(i==j)
continue;
int fu=find(u[j]);
int fv=find(v[j]);
if(fu!=fv){
a[fu]=fv;
}
}
int flag=1;
for(int k=1;k<=n;k++){
if(find(1)!=find(k))
flag=0;
}
if(flag)
ans++;
}
for(int i=1;i<=n+1;i++){
for(int q=i+1;q<=n+1;q++){
for(int k=1;k<=n;k++)
a[k]=k;
for(int j=1;j<=n+1;j++){
if(j==i||j==q)
continue;
int fu=find(u[j]);
int fv=find(v[j]);
if(fu!=fv){
a[fu]=fv;
}
}
int flag=1;
for(int k=1;k<=n;k++){
if(find(1)!=find(k))
flag=0;
}
if(flag)
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}





错误代码:

/*#include <iostream> 
#include <cstdio> 
#include <fstream> 
#include <algorithm> 
#include <cmath> 
#include <deque> 
#include <vector> 
#include <queue> 
#include <string> 
#include <cstring> 
#include <map> 
#include <stack> 
#include <set>
using namespace std;
int a[200];
int find(int x)
{
    if(x!=a[x]) a[x]=find(a[x]);
    return a[x];
}
int main(){
int t,u[200],v[200];
cin>>t;
while(t--){
int n;
cin>>n;
memset(a,0,sizeof(a));
memset(u,0,sizeof(u));
memset(v,0,sizeof(v));
for(int i=1;i<=n+1;i++)
cin>>u[i]>>v[i];
for(int i=1;i<=n;i++)
a[i]=i;
int ans=0;
int unit=n;
for(int j=1;j<=n+1;j++){
           if(find(u[j])!=find(v[j])){
unit--;
if(a[u[j]]!=u[j])
a[v[j]]=u[j];
else
a[u[j]]=v[j];
}
}
if(unit!=1)
ans=0;
else{
for(int i=1;i<=n+1;i++){
for(int k=1;k<=n;k++)
a[k]=k;
unit=n;
for(int j=1;j<=n+1;j++){
if(i==j)
continue;
int fu=find(u[j]);
int fv=find(v[j]);
if(fu!=fv){
unit--;
a[fu]=fv;
}
}
if(unit==1)
ans++;
}
for(int i=1;i<=n+1;i++){
for(int q=i+1;q<=n+1;q++){
for(int k=1;k<=n;k++)
a[k]=k;
unit=n;
for(int j=1;j<=n+1;j++){
if(j==i||j==q)
continue;
int fu=find(u[j]);
int fv=find(v[j]);
if(fu!=fv){
unit--;
a[fu]=fv;
}
}
if(unit==1)
ans++;
}
}
}
cout<<ans<<endl;
}
return 0;
}
*/








#include <iostream> 
#include <cstdio> 
#include <fstream> 
#include <algorithm> 
#include <cmath> 
#include <deque> 
#include <vector> 
#include <queue> 
#include <string> 
#include <cstring> 
#include <map> 
#include <stack> 
#include <set>
using namespace std;
int a[200];
int find(int x)
{
    if(x!=a[x]) a[x]=find(a[x]);
    return a[x];
}
int main(){
int t,u[200],v[200];
cin>>t;
while(t--){
int n;
cin>>n;
memset(a,0,sizeof(a));
memset(u,0,sizeof(u));
memset(v,0,sizeof(v));
for(int i=1;i<=n+1;i++)
cin>>u[i]>>v[i];
for(int i=1;i<=n;i++)
a[i]=i;
int ans=0;
for(int i=1;i<=n+1;i++){
for(int k=1;k<=n;k++)
a[k]=k;
for(int j=1;j<=n+1;j++){
if(i==j)
continue;
if(find(u[j])!=find(v[j])){
if(a[u[j]]!=u[j])
a[v[j]]=u[j];
else
a[u[j]]=v[j];
}
}
int flag=1;
for(int k=1;k<=n;k++){
if(find(1)!=find(k))
flag=0;
}
if(flag)
ans++;
}
for(int i=1;i<=n+1;i++){
for(int q=i+1;q<=n+1;q++){
for(int k=1;k<=n;k++)
a[k]=k;
for(int j=1;j<=n+1;j++){
if(j==i||j==q)
continue;
if(find(u[j])!=find(v[j])){
if(a[u[j]]!=u[j])
a[v[j]]=u[j];
else
a[u[j]]=v[j];
}
}
int flag=1;
for(int k=1;k<=n;k++){
if(find(1)!=find(k))
flag=0;
}
if(flag)
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34621169/article/details/51316445