UVA 539 The Settlers of Catan

UVA_539

    这个题目可以直接用深搜去解。

#include<stdio.h>
#include
<string.h>
int G[30][30],vis[30][30];
int n,m,ans;
void dfs(int i,int tot)
{
int j,flag;
flag
=0;
for(j=0;j<n;j++)
if(G[i][j]&&!vis[i][j])
{
flag
=1;
vis[i][j]
=1;
vis[j][i]
=1;
dfs(j,tot
+1);
vis[i][j]
=0;
vis[j][i]
=0;
}
if(!flag&&tot>ans)
ans
=tot;
}
int main()
{
int i,j,k,u,v;
while(1)
{
scanf(
"%d%d",&n,&m);
if(n==0)
break;
memset(G,
0,sizeof(G));
for(i=0;i<m;i++)
{
scanf(
"%d%d",&u,&v);
G[u][v]
=1;
G[v][u]
=1;
}
ans
=0;
memset(vis,
0,sizeof(vis));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j])
{
vis[i][j]
=1;
vis[j][i]
=1;
dfs(j,
1);
vis[i][j]
=0;
vis[j][i]
=0;
}
printf(
"%d\n",ans);
}
return 0;
}

  

你可能感兴趣的:(set)