UVa 539 - The Settlers of Catan

题目链接:UVa 539 - The Settlers of Catan

简单回溯,注意一下题目给的是无向边。

#include <iostream>
#include <cstring>
#include <stdio.h>
//#define test
using namespace std;

const int MAX_N = 25 + 5;
int vis[MAX_N][MAX_N];
int G[MAX_N][MAX_N];
int m,n,_max;

void dfs(int x,int cur)
{
    for(int i = 0;i < n;i++)
    {
        if(!vis[x][i] && G[x][i])
        {
            vis[x][i] = vis[i][x] = 1;
            dfs(i,cur+1);
            vis[x][i] = vis[i][x] = 0;
        }
    }
    if(cur > _max)
        _max = cur;
}
int main()
{
#ifdef test
    freopen("in.txt","r",stdin);
#endif // test
    while(cin>>n>>m && n && m)
    {
        _max = -1;
        memset(G,0,sizeof(G));
        memset(vis,0,sizeof(vis));
        int a,b;
        for(int i = 0;i < m;i++)
        {
            cin>>a>>b;
            G[b][a] = G[a][b] = 1;
        }
        for(int i = 0;i < n;i++)
            dfs(i,0);
        cout<<_max<<endl;
    }

    return 0;
}


你可能感兴趣的:(UVa 539 - The Settlers of Catan)