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 |
runtime error求大神解答_thx#include <iostream> #include <stdio.h> #include <string.h> using namespace std; int n,m; char prefix; char suffix; int**graph; int* arrLen; int* inDegree; int* inDegree_bak; bool* flag; char* str; int IsAcyclic(char* str); int IsAcyclic(char* str) { int strLen = 0; int visited = 0; int cc = 0; int r = 0; bool updated = false; for(int i=0;i<n;i++) { if(inDegree[i]==0) { cc++; } } while(visited<n) { updated = false; for(int i=0;i<n;i++) { if(!flag[i]&&inDegree[i]==0) { if(cc!=1) { r = 1; } str[strLen++] = i+'A'; str[strLen] = '\0'; cc--; visited ++; updated = true; flag[i] = true; int len = arrLen[i]; for(int j=0;j<len;j++) { if((--inDegree[graph[i][j]])==0) cc++; } } } if(!updated) return 0;//incomsisitency } if(r==0) { return 2;//determined }else return 1;//undetermined } int main() { str = new char[27]; bool isFirstRound = true; while(1) { scanf("%d%d",&n,&m); fflush(stdin); if(!isFirstRound) { for(int i=0;i<n;i++) { delete graph[i]; } delete graph; delete arrLen; delete inDegree; delete flag; delete inDegree_bak; isFirstRound = false; } if(n==0&&m==0) break; graph = new int*[n]; arrLen = new int[n]; inDegree = new int[n]; inDegree_bak = new int[n]; flag = new bool[n]; memset(flag,0,sizeof(bool)*n); memset(inDegree,0,sizeof(int)*n); memset(inDegree_bak,0,sizeof(int)*n); memset(arrLen,0,sizeof(int)*n); for(int i=0;i<n;i++) { graph[i] = new int[26]; memset(graph[i],0,sizeof(int)*26); } int temp = 0; int endRound = 0; for(int i=0;i<m;i++) { scanf("%c<%c",&prefix,&suffix); fflush(stdin); if(endRound==1) continue; //Add A node; int f = prefix-'A'; int b = suffix-'A'; graph[f][arrLen[f]++] = b; inDegree_bak[b]++; memcpy(inDegree,inDegree_bak,sizeof(int)*n); memset(flag,0,sizeof(bool)*n); temp = IsAcyclic(str); if(temp==0) { printf("Inconsistency found after %d relations.\n",i+1); endRound = 1; }else if(temp==2) { printf("Sorted sequence determined after %d relations: %s.\n",i+1,str); endRound = 1; } } if(temp==1) { printf("Sorted sequence cannot be determined.\n"); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator