UVA 10160

 双递归

结构:

int dfs()
多重判断是否return
如:if(cnt == m ) return 

一些改变操作
dfs(cur + 1, cnt + 1 ) 含递增的变量
恢复改变操作
dfs(cur, cnt + 1 )

 

#include<iostream>
#include<numeric>
#include<string.h>
using namespace std;

int adj[36][36], vis[36], sum[36];
int m, n, minvalue;

int judge()
{
	for (int i = 1; i <= m; i++)
		if (!vis[i])
			return 0;
	return 1;
}
int dfs(int cur, int cnt)
{
	if (cur - 1 > minvalue)
		return 0;
	if (judge())
	{
		minvalue = cur - 1;
		return 0;
	}
	if (cnt - 1 == m)
		return 0;
	for (int i = 1; i < cnt; i++)
		if (sum[i] == 1)
			if(!vis[i])
				return 0;

	int ok = 0;
	for (int i = 1; i <= m; i++)
	{
		if (adj[cnt][i])
		{
			ok = adj[cnt][i] ^ vis[i];
			vis[i]++;
		}		
	}
	if (ok)
		dfs(cur + 1, cnt + 1);
	for (int i = 1; i <= m; i++)
		if (adj[cnt][i])
			vis[i]--;
	dfs(cur, cnt + 1);
	return 0;
}

int main()
{
	while (cin >> m >> n && m && n)
	{
		int a, b;
		minvalue = m;
		memset(vis, 0, sizeof(vis));
		memset(adj, 0, sizeof(adj));
		while (n--)
		{
			cin >> a >> b;
			adj[a][b] = 1;
			adj[b][a] = 1;
		}
		for (int i = 1; i <= m; i++)
		{
			adj[i][i] = 1;
			sum[i] = accumulate(adj[i] + 0, adj[i] + m + 1, 0);
		}	
		dfs(1, 1);
		cout << minvalue << endl;
	}
}
//32 132
//18 27
//20 25
//19 9
//1 13
//8 30
//16 24
//22 8
//3 27
//30 25
//17 18
//17 20
//8 26
//9 12
//6 31
//5 10
//6 28
//3 22
//2 21
//2 25
//27 10
//31 21
//29 10
//23 30
//22 19
//29 7
//2 30
//28 25
//15 23
//9 4
//1 23
//7 24
//14 28
//20 10
//20 23
//28 21
//12 2
//4 14
//29 3
//30 21
//12 26
//28 7
//28 29
//15 32
//21 17
//2 29
//16 15
//19 15
//14 27
//29 20
//6 26
//24 6
//1 8
//7 15
//29 19
//18 15
//9 24
//8 29
//12 10
//23 4
//11 4
//11 1
//4 2
//18 9
//5 8
//25 29
//30 27
//10 19
//16 19
//9 15
//16 28
//20 31
//30 29
//30 24
//5 13
//23 7
//19 25
//27 31
//31 29
//27 16
//18 21
//10 16
//7 2
//14 30
//9 8
//18 31
//17 22
//17 13
//30 32
//14 22
//25 18
//7 9
//17 10
//4 17
//31 32
//6 1
//13 20
//27 25
//26 10
//22 6
//11 18
//21 13
//6 19
//12 8
//10 18
//20 8
//2 6
//27 6
//28 9
//17 24
//16 25
//30 15
//22 26
//3 31
//30 3
//21 26
//23 3
//4 12
//22 25
//23 22
//23 14
//17 7
//7 11
//19 24
//20 14
//29 4
//24 12
//28 27
//19 18
//17 3
//2 18
//22 28
//15 10

单递归

结构:

for()

{
    int ok = 1

    条件判断ok为1还是0
    如for(int j = 0;j < cur;j++)
      {
           if(c[j]==c[cur])
           {ok = 0;break;}
      }
    if(ok)
    {     
        dfs()   
    }      
}
#include<iostream>
#include<algorithm>
#include<numeric>
#include<string.h>
using namespace std;

int adj[36][36], vis[36],c[36];
int m, n, minvalue;

int judge()
{
	for (int i = 1; i <= m; i++)
	{
		if (!vis[i])
			return 0;
	}
	return 1;
}
int dfs(int cur)
{ 
	if (cur - 1 > minvalue)
		return 0;
	if (judge())
	{
		minvalue = cur - 1;
		return 0;
	}	
	if (cur - 1 == m)       
		return 0;
	for (int i = 1; i <= m; i++)
	{
		c[cur] = i;
		int ok = 1;
		for (int j = 1; j < cur; j++)
		{
			if (c[cur] == c[j])
			{
				ok = 0;
				break;
			}
		}
		if (ok)
		{
			int idx[36];
			memset(idx, 0, sizeof(idx));
			for (int k = 1; k <= m; k++)
			{
				if (adj[i][k])
				{
					vis[k]++;
					idx[k] = 1;
				}
			}
			dfs(cur + 1, vis);
			for (int k = 1; k <= m; k++)
			{
				if (idx[k])
					vis[k]--;
			}
		}
	}
	return 0;
}

int main()
{
	while (cin >> m >> n && m && n)
	{ 
		int a, b;
		minvalue = m;
		memset(vis, 0, sizeof(vis));
		memset(adj, 0, sizeof(adj));
		memset(c, 0, sizeof(c));
		while (n--)
		{
			cin >> a >> b;
			adj[a][b] = 1;
			adj[b][a] = 1;
			adj[a][a] = 1;
		}
		dfs(1);
		cout << minvalue << endl;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值