2347: Transport Gate
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 16384K | 103 | 37 | Standard |
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;
}