HDU 5631 Rikka with Graph(无向图去边搜索)

Rikka with Graph

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 905 Accepted Submission(s): 404


Problem 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) ——The number of the testcases.

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

Then n+1 lines follow. Each line contains two numbers u,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

Source

/*
思路:题目意思是一个n个点的无向图,给你n+1条边,求有多少种删除办法,使得图还能连通
至少删除一条边
我的思路是,n个点,要连通,至少是n-1条边,所以最多删除2条,枚举每一条边,删除求连通
若可以,+1,尝试枚举下一条边被删,看能不能继续连通 
*/

  /*
  超时 
  没用邻接表  复杂度O(n*n)的递归
  */
  /*
#include<iostream>
#include <string.h>
using namespace std;
const int N=100+5;
struct Node{
	int x,y;
}node[N];
int check; 
int map[N][N];  //记录边数   用int可以累加 
bool book[N];
int n;
void dfs(int now){   //深搜 
	book[now]=1;
	check++;
	for(int i=1;i<=n;i++){     //这里时间复杂度n^2 
		if(book[i])
          continue;
		 if(map[now][i]>0)
		  dfs(i);
	}
}
int main()
{  
    int t,re;
	cin>>t;
	while(t--){
		  re=0;
		 cin>>n;
		 memset(map,0,sizeof(map));
		 for(int i=0;i<n+1;i++){
		 	 cin>>node[i].x>>node[i].y;
		 	 map[node[i].x][node[i].y]++;
		 	 map[node[i].y][node[i].x]++;
		 }
		 for(int i=0;i<n+1;i++){
		 	  map[node[i].x][node[i].y]--;
		 	  map[node[i].y][node[i].x]--;
		 	  memset(book,0,sizeof(book)); 
		 	  check=0;
		 	  dfs(1);
		 	  if(check==n)
		 	   {
		 	   	  re++;
		 	   	  for(int j=i+1;j<n+1;j++){
		 	   	  	    map[node[j].x][node[j].y]--;
		 	   	  	    map[node[j].y][node[j].x]--;
		 	   	  	    memset(book,0,sizeof(book)); 
		 	   	  	    check=0;
					    dfs(1);
					     if(check==n)
					     	 re++;
					    map[node[j].x][node[j].y]++;
					    map[node[j].y][node[j].x]++;
					  }
		 	   	  
				}
				map[node[i].x][node[i].y]++;
		 	  map[node[i].y][node[i].x]++;
		 }
		 cout<<re<<endl;
	} 
	return 0;
}
*/


/*忘了每次初始化vector数组,我就说我的复杂度不高啊 终于AC了
16772534 2016-04-04 22:34:00 Accepted 5631 78MS 1784K 3734 B C++ Sterben 
*/

#include<iostream>
#include <vector> 
#include <cstdio>
#include <string.h>
using namespace std;
const int N=100+5;
struct Node{
	int x,y;
}node[N];
vector<int> ve[N];
int map[N][N];  //记录边数   用int可以累加 
bool book[N];
int check; 
int n;
void dfs(int now){   //深搜 
	book[now]=1;
	check++;
	for(int i=0;i<ve[now].size();i++){     //邻接表优化  复杂度减少了O(n*n) 
		if(book[ve[now][i]])
          continue;     
		 if(map[now][ve[now][i]]>0)
		  dfs(ve[now][i]);
	}
}

//用一个全局变量减少判断,复杂度减少O(n)
//inline bool check(){      //求连通 
//	for(int i=1;i<=n;i++)
//	  if(!book[i])
//	     return false;
 //   return true;
//}


int main()
{  
    int t,re;
	scanf("%d",&t);
	while(t--){
		  re=0;
		 scanf("%d",&n);
		 memset(map,0,sizeof(map));
		 for(int i=1;i<=n;i++){
		  ve[i].clear();
	}
		 for(int i=0;i<n+1;i++){
		 	scanf("%d%d",&node[i].x,&node[i].y);
		 	// cin>>node[i].x>>node[i].y;
		 	 ve[node[i].x].push_back(node[i].y);
		 	  ve[node[i].y].push_back(node[i].x);
		 	 map[node[i].x][node[i].y]++;
		 	 map[node[i].y][node[i].x]++;
		 }
		 memset(book,0,sizeof(book)); 
		check=0;
		 dfs(1);
		 if(check!=n){
		 	printf("0\n");
		 	continue;
		 }
		 for(int i=0;i<n+1;i++){
		 	  map[node[i].x][node[i].y]--;
		 	  map[node[i].y][node[i].x]--;
		 	  memset(book,0,sizeof(book)); 
		 	  check=0;
		 	  dfs(1);  //深搜起点 
		 	  if(check==n)   //判断连通图深搜递归标记是不是n个 
		 	   {
		 	   	  re++;
		 	   	  for(int j=i+1;j<n+1;j++){
		 	   	  	    map[node[j].x][node[j].y]--;
		 	   	  	    map[node[j].y][node[j].x]--;
		 	   	  	    memset(book,0,sizeof(book)); 
		 	   	  	    check=0;  
					    dfs(1);  //深搜起点 
					     if(check==n)   //判断连通图深搜递归标记是不是n个 
					     	 re++;
					    map[node[j].x][node[j].y]++;
					    map[node[j].y][node[j].x]++;
					  }
		 	   	  
				}
				map[node[i].x][node[i].y]++;
		 	     map[node[i].y][node[i].x]++;
		 }
		 //cout<<re<<endl;
		 printf("%d\n",re);
	} 
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值