Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
TL了,测试数据正确,路过的帮忙看一下读一次数据判断一次 初始化state = 0; 1.当最深节点的深度 == 字符数时 找到 state = 1; 2.出现回路 state = -1; #include <iostream> #include <string> using namespace std; int state, map[26][26], deep[26]; //map存储有向边,deep存储深度 void updateDeep(int index, int value, int n) //当父节点深度改变时更新子节点深度 { int i; deep[index] = value + 1; for (i = 0; i <= n; i++) { if (map[index][i] == 1) { updateDeep(i, deep[index], n); } } } void GoInto(int index, int i, int n) //判断是否有回路的深度搜索函数 { int j; if (i == index) { state = -1; return; } for (j = 0; j <= n; j++) { if (map[i][j] == 1) { GoInto(index, j, n); } } return; } void IsCircle(int index, int n) //判断是否有回路 { int i; for (i = 0; i <= n; i++) { if (map[index][i] == 1) { GoInto(index, i, n); } } } int Judge(int index, int n, char a, char c) //判断状态 { int i, max; max = -1; IsCircle(index , n); if (state == -1) { return -1; } if (deep[a - 'A'] + 1 >= deep[c - 'A']) { updateDeep(c - 'A', deep[a - 'A'], n - 1); } for (i = 0; i <= n; i++) { if (max < deep[i]) { max = deep[i]; } } if (max == n) { return 1; } return 0; } string OutputSort(int n) //按次序是出字符串 { string str; int i, j; str = ""; for (i = 0; i <= n; i++) { for (j = 0; j <= n; j++) { if (deep[j] == i) { str += (j + 'A'); } } } return str; } int main() { int n, m, i, j, tm; char a, op, c; while (cin >> n >> m) { if (n == 0 && m == 0) { break; } //Initialize state = 0; tm = 0; for (i = 0; i < 26; i++) { deep[i] = 0; for (j = 0; j < 26; j++) { map[i][j] = 0; } } for (i = 1; i <= m; i++) { cin >> a >> op >> c; if (state == 1 || state == -1) { continue; } map[a - 'A'][c - 'A'] = 1; state = Judge(a - 'A', n - 1, a, c); if (state != 0) { tm = i; } } if (state == 0) { cout << "Sorted sequence cannot be determined." << endl; } else if (state == -1) { cout << "Inconsistency found after " << tm << " relations." << endl; } else if (state == 1) { cout << "Sorted sequence determined after " << tm << " relations:" << OutputSort(n - 1) << endl; } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator