ZOJ1060

/**zoj 1060 *考查点:由输入构造有向图,判断是否有环,判断是否满足线序,最后排序确定输出序列. *思路: 刚看了离散数学,看到传递闭包时想起了这个题目,可以用求传递闭包的方法来求解,     求传递闭包有一个有效的算法---warshall方法,不过是O(n^3)的,效果不是太好,但     求出传递闭包后判断是否有环和排序都只有o(n)的复杂度,即只需看邻接矩阵对角线上     元素是否为1。而排序采用的是计数排序,可以直接定位。 *提交情况:wrong answer 3次,原因:在行及列的计数时没有考虑重复的情况。 *收获:加深了对传递闭包和偏序的理解 *经验: 问题的转化很重要,细节要注意 **/ #include <stdio.h> #include <string.h> char adj[26][26]; int rowVal[26], columnVal[26]; int reverse_order[26]; void warshall(int num) /* warshall algorithm : to calculate the transitive closure */ { int i, j, k; for (j = 0; j < num; j++) { if (!columnVal[j]) continue; for (i = 0; i < num; i++) { if (adj[i][j]) { if (i != j) { for (k = 0; k < num; k++) { if (!adj[i][k] && (adj[i][k] |= adj[j][k])) { rowVal[i]++; columnVal[k]++; } } } } } } } int has_loop(int num) /* based on the warshall algorithm, to check loop existence*/ { int i; for (i = 0; i < num; i++) { if (adj[i][i]) return 1; } return 0; } int order(int num) { int i, sum = 0; for (i = 0; i < num; i++) sum += rowVal[i]; if (sum != (num-1)*num/2) return 0; else { for (i = 0; i < num; i++) reverse_order[rowVal[i]] = i; return 1; } } int main() { int i, j, lnum, rnum, loop, ordered; char lc, rc; while (scanf("%d%d", &lnum, &rnum) == 2 && lnum) { memset(adj, 0, sizeof(char)*26*26); memset(rowVal, 0, sizeof(int)*26); memset(columnVal, 0, sizeof(int)*26); memset(reverse_order, 255, sizeof(int)*26); loop = ordered = 0; for (i = 0; i < rnum; i++) { getchar(); lc = getchar(); getchar(); rc = getchar(); if (loop || ordered) continue; if (!adj[lc-'A'][rc-'A'] && (adj[lc-'A'][rc-'A'] = 1)) { rowVal[lc-'A']++; /* total number of '1's in a row */ columnVal[rc-'A']++; /* total number of '1's in a column used in the warshall algorithm*/ } warshall(lnum); if (has_loop(lnum)) { loop = 1; printf("Inconsistency found after %d relations./n", i+1); } if (order(lnum)) { ordered = 1; printf("Sorted sequence determined after %d relations: ", i+1); for (j = lnum-1; j >= 0; j--) printf("%c", reverse_order[j]+'A'); printf("./n"); } } if (!loop && !ordered) { printf("Sorted sequence cannot be determined./n"); } } return 0; }

你可能感兴趣的:(Algorithm,c,算法)