2011年6月5日 星期日

ACM 280 Vertex

很單純的Warshall's Algorithm。因為忘記歸零還吃了個WA = ="


#include <stdio.h>
#include <stdbool.h>

bool map[101][101];
int p[100];

int main(void)
{
    int i, j, k, n, s, count;
   
    while (scanf("%d", &n) == 1 && n)
    {
        for (i = 1; i <= n; i++)    // 將圖全部歸零
            for (j = 1; j <= n; j++)
                map[i][j] = false;
        while (scanf("%d", &i) == 1 && i)   // 讀取輸入
            while (scanf("%d", &j) == 1 && j)
                map[i][j] = true;
        for (k = 1; k <= n; k++)    // 做Warshall's Algorithm
            for (i = 1; i <= n; i++)
                for (j = 1; j <= n; j++)
                    if (map[i][k] && map[k][j])
                        map[i][j] = true;
        scanf("%d", &k);    // 讀取要被處理的點的數目
        while (k--)     // 一個一個點做處理
        {
            scanf("%d", &s);
            for (count = 0, j = 1; j <= n; j++)
                if (!map[s][j])     // 若點s不能到達j,則將點j存入p[]
                {
                    p[count] = j;
                    count++;
                }
            printf("%d", count);
            for (i = 0; i < count; i++)
                printf(" %d", p[i]);
            printf("\n");
        }
    }
   
    return 0;
}

沒有留言:

張貼留言