2347: Transport Gate 最大团

在魔法国度ACM中,通过Transport Gate来往各个城市。输入包含城市数n和Transport Gate的数量m,输出最大闭合城市集合的数目。代码实现深度优先搜索策略寻找最大闭合集合。
摘要由CSDN通过智能技术生成

2347: Transport Gate


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s16384K10337Standard

In ACM (A Country of Magic) world, there are many city. One can travel to another city through Transport Gate. It is very expensive to build one Transport Gate from one city to another. If there are way between any city in a city set, this city set is called close. The king of ACM want know the maximal close city set in his country.

Input

Input is multi-case. At first line of one case, there are two integer n (n<=30), m. n is the number of citys, m is the number of existing Transport Gate. The next m lines identified the two city code (from 1 to n) which are connected by a different Transport Gate. The Transport Gate is bi-directional.

Output

For each case, you should output the quantity of the maximal clost city set in a single line.

Sample Input

4 4
1 2
2 3
3 1
4 1
4 3
1 2
2 3
3 4

Sample Output

3
2

 

Problem Source: 5th JLU Programming Contest - Personal, skywind

#include<iostream>
#include<algorithm>
using namespace std;
int n,m;
int cn;//当前顶点数
int best;//当前最大顶点数
int vis[50];//当前解
int bestn[50];//最优解
int map[50][50];//临界表
void dfs(int i)
{
    if(i>n)
    {
        for(int j=1;j<=n;j++) bestn[j]=vis[j];
        best=cn;
        return ;
    }
    int ok=1;
    for(int j=1;j<i;j++)
    {
        if(vis[j]==1&&map[i][j]==0)
        {
            ok=0;
            break;
        }
    }
    if(ok)
    {
        //进入左子树
        vis[i]=1;
        cn++;
        dfs(i+1);
        cn--;
    }
    if(cn+n-i>best)
    {
        //进入右子树
        vis[i]=0;
        dfs(i+1);
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        memset(vis,0,sizeof(vis));
        memset(map,0,sizeof(map));
        while(m--)
        {
            int p,q;
            scanf("%d%d",&p,&q);
            map[p][q]=map[q][p]=1;
        }
        cn=0;
        best=0;
        dfs(1);
        printf("%d/n",best);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值