uva539 - The Settlers of Catan

#include <cstdio>
#include <cstring>
bool f[26][26], visit[26][26];
int n, m;
int dfs(int u)
{
    int ans = 0, t;
    for(int i = 0; i < n; i++) if(f[u][i]&&!visit[u][i])
    {
        visit[u][i] = visit[i][u] = 1;
        t = dfs(i)+1;
        ans = ans < t?t:ans;
        visit[u][i] = visit[i][u] = 0;
    }
    return ans;
}
int main ()
{
    int a, b;
    while(scanf("%d%d", &n,&m)&&m+n)
    {
        memset(f,false,sizeof(f));
        memset(visit,false,sizeof(visit));
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d",&a, &b);
            f[a][b] = f[b][a] = 1;
        }
        int t, max = 0;
        for(int i = 0; i < n; i++)
        {
            t = dfs(i);
            max = max < t?t:max;
        }
        printf("%d\n",max);
    }
    return 0;
}

学如逆水行舟,不进则退!!=_=!

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