输入数据:
4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0
Sorted sequence determined after 4 relations: ABCD.(4组出现后就能确定)
Inconsistency found after 2 relations.(两组出现后发现有环)
Sorted sequence cannot be determined.(不能确定,有多种可能)
- /*
- * TopSortDemo.cpp
- *
- * Created on: 2012-6-17
- * Author: Administrator
- */
- #include<stdio.h>
- #include<string.h>
- #define M 100
- struct ArcNode{
- int to;
- struct ArcNode *next;
- };
- ArcNode *List[M];//表示图
- int counts[M];//表示顶点的入度 //同时,也充当了栈的作用
- char output[M];
- int n,m;//边和顶点
- void topSort(){
- //把入度为零的顶点入栈
- int top=-1;
- for(int i=0;i<n;++i){
- if(counts[i]==0){
- counts[i]=top;
- top=i;
- }
- }
- bool bcycle=false;
- int pos=0;
- ArcNode *temp;
- for(int i=0;i<n;++i){
- if(top==-1){//如果在顶点没有处理完之前,没有入度为零的顶点,则说明有环
- bcycle=true;
- break;
- }else{
- int j=top;top=counts[top];//取出顶点
- pos+=sprintf(output+pos,"%d ",j+1);//把顶点输出到output数组中
- //把与顶点相邻顶点的入度减1
- temp=List[j];
- while(temp!=NULL){
- int k=temp->to;
- --counts[k];
- if(counts[k]==0){//如果顶点的入度减少到零,则入栈
- counts[k]=top;top=k;
- }
- temp=temp->next;
- }
- }
- }
- //最后把结果输出
- if(bcycle){
- printf("Network has a cycle!\n");
- }else{
- int len=strlen(output);
- output[len-1]=0;
- printf("%s\n",output);
- }
- }
- int main(){
- while(1){
- scanf("%d%d",&n,&m);//输入点数和边数
- if(n==0&&m==0)break;
- memset(List,0,sizeof(List));
- memset(counts,0,sizeof(counts));
- memset(output,0,sizeof(output));
- ArcNode *temp;
- int v,u;
- for(int i=0;i<m;++i){
- scanf("%d%d",&v,&u);//输入一条边
- --v;--u;
- ++counts[u];
- temp=new ArcNode;
- temp->to=u;
- temp->next=NULL;
- if(temp==NULL){
- List[v]=temp;
- }else{
- temp->next=List[v];
- List[v]=temp;
- }
- }
- //拓扑排序
- topSort();
- //收回内存
- for(int i=0;i<n;++i){
- temp=List[i];
- while(temp=NULL){
- List[i]=temp->next;
- delete temp;
- temp=List[i];
- }
- }
- }
- return 0;
- }
- /*
- * TopSortDemo.cpp
- *
- * Created on: 2012-6-17
- * Author: Administrator
- */
- #include<stdio.h>
- #include<string.h>
- #include<stack>
- using namespace std;
- #define M 100
- struct ArcNode{
- int to;
- struct ArcNode *next;
- };
- ArcNode *List[M];//表示图
- int counts[M];//表示顶点的入度 //同时,也充当了栈的作用
- char output[M];
- int n,m;//边和顶点
- stack<int> S;
- void topSort(){
- //把入度为零的顶点入栈
- for(int i=0;i<n;++i){
- if(counts[i]==0){
- S.push(i);
- }
- }
- bool bcycle=false;
- int pos=0;
- ArcNode *temp;
- for(int i=0;i<n;++i){
- if(S.empty()){//如果在顶点没有处理完之前,没有入度为零的顶点,则说明有环
- bcycle=true;
- break;
- }else{
- int j=S.top();S.pop();//取出顶点
- pos+=sprintf(output+pos,"%d ",j+1);//把顶点输出到output数组中
- //把与顶点相邻顶点的入度减1
- temp=List[j];
- while(temp!=NULL){
- int k=temp->to;
- --counts[k];
- if(counts[k]==0){//如果顶点的入度减少到零,则入栈
- S.push(k);
- }
- temp=temp->next;
- }
- }
- }
- //最后把结果输出
- if(bcycle){
- printf("Network has a cycle!\n");
- }else{
- int len=strlen(output);
- output[len-1]=0;
- printf("%s\n",output);
- }
- }
- int main(){
- while(1){
- scanf("%d%d",&n,&m);//输入点数和边数
- if(n==0&&m==0)break;
- memset(List,0,sizeof(List));
- memset(counts,0,sizeof(counts));
- memset(output,0,sizeof(output));
- ArcNode *temp;
- int v,u;
- for(int i=0;i<m;++i){
- scanf("%d%d",&v,&u);//输入一条边
- --v;--u;
- ++counts[u];
- temp=new ArcNode;
- temp->to=u;
- temp->next=NULL;
- if(temp==NULL){
- List[v]=temp;
- }else{
- temp->next=List[v];
- List[v]=temp;
- }
- }
- //拓扑排序
- topSort();
- //收回内存
- for(int i=0;i<n;++i){
- temp=List[i];
- while(temp=NULL){
- List[i]=temp->next;
- delete temp;
- temp=List[i];
- }
- }
- }
- return 0;
- }
- /*
- * POJ_1094.cpp
- *
- * Created on: 2012-6-17
- * Author: Administrator
- */
- #include<stdio.h>
- #include<string.h>
- #include<algorithm>
- #include<queue>
- using namespace std;
- #define M 27
- struct ArcNode{
- int to;
- struct ArcNode *next;
- };
- ArcNode *List[M];
- int numbers[M];
- int cnumbers[M];//numbers的copy
- char output[M];
- char alph[M];
- char relation[4];//输入的关系
- int n,m;
- int topSort(int c){
- copy(numbers,numbers+n,//source
- cnumbers);//des
- // for(int i=0;i<n;i++)
- // printf("%d\t",cnumbers[i]);
- int top=-1;
- ArcNode *temp;
- int pos=0;
- int zeros=0;
- bool flag=true;
- for(int i=0;i<n;++i){
- if(cnumbers[i]==0){
- cnumbers[i]=top;
- top=i;
- zeros++;
- }
- }
- if(zeros>1)
- flag=false;
- while(c--){
- zeros=0;
- if(top==-1){
- return -1;
- }else{
- int j=top;top=cnumbers[top];
- output[pos++]=j+'A';
- temp=List[j];
- while(temp!=NULL){
- int k=temp->to;
- cnumbers[k]--;
- if(cnumbers[k]==0){
- cnumbers[k]=top;top=k;
- zeros++;
- }
- temp=temp->next;
- }
- if(zeros>1){
- flag=false;
- }
- }
- }
- if(flag)
- return pos;
- else
- return 0;
- }
- int main(){
- while(scanf("%d%d",&n,&m)!=EOF&&n!=0&&m!=0){
- memset(List,0,sizeof(List));
- memset(numbers,0,sizeof(numbers));
- memset(output,0,sizeof(output));
- memset(alph,0,sizeof(alph));
- int determined=0;//没有决定
- int c=0;
- int t,k;
- ArcNode *temp;
- for(int i=0;i<m;++i){
- scanf(" %s",relation);
- if(alph[relation[0]-'A']==0){
- alph[relation[0]-'A']=1; ++c;
- }
- if(alph[relation[2]-'A']==0){
- alph[relation[2]-'A']=1;++c;
- }
- numbers[relation[2]-'A']++;//入度数加一
- temp=new ArcNode;
- temp->to=relation[2]-'A';
- temp->next=NULL;
- if(List[relation[0]-'A']==NULL){
- List[relation[0]-'A']=temp;
- }else{
- temp->next=List[relation[0]-'A'];
- List[relation[0]-'A']=temp;
- }
- if(determined==0){//如果顺序还没有决定下来
- t=topSort(c);
- // printf("t=%d,c=%d\n",t,c);
- if(t==-1){
- determined=-1;k=i+1;
- }else if(t==n){
- determined=1;k=i+1;
- }
- }
- }
- if(determined==-1){
- printf("Inconsistency found after %d relations.\n",k);
- }else if(determined==0){
- printf("Sorted sequence cannot be determined.\n");
- }else{
- output[n]=0;
- printf("Sorted sequence determined after %d relations: %s.\n",k,output);
- }
- //收回内存
- for(int i=0;i<n;i++){
- temp=List[i];
- while(temp=NULL){
- List[i]=temp->next;delete temp;temp=List[i];
- }
- }
- }
- return 0;
- }