Channel Allocation(DFS着色问题)

#include <cstdio>
#include <cstring>
int T,grid[30][30],color[30],ret[30];
void dfs(int x)
{
    int c[30]={0},i;
    for (i=0;i<T;i++)
        if (grid[x][i])
            c[color[i]] = 1;
    for (i=1;i<=T;i++)
        if (!c[i])
        {
            color[x] = i;
            if (!ret[i])
                ret[i] = 1;
            break;
        }
    for (i=0;i<T;i++)
        if (!color[i])
            dfs(i);
    return;
}
int main()
{
    char rela[30];
    int len,i,j;
    while (scanf("%d", &T) && T)
    {
        memset(grid,0,sizeof (grid));
        memset(color,0,sizeof (color));
        memset(ret,0,sizeof (ret));
        for (i=0;i<T;i++)
        {
            scanf("%s",rela);
            len=strlen(rela);
            for (j=2;j<len;j++)
                grid[i][rela[j]-‘A‘] = 1;
        }
        dfs(0);
        int sum=0;
        for (i=1;i<=T;i++)
            if (ret[i])
                sum++;
        printf("%d",sum);
        if (sum==1)
            printf(" channel needed.\n");
        else
            printf(" channels needed.\n");
    }
    return 0;
}

Channel Allocation(DFS着色问题)

上一篇:Java开发必备linux命令集锦


下一篇:面试疑难点解析——Java数据库开发(六)