poj1094Sorting It All Out
2013-09-06 17:50
148 查看
刚遇到这道题时我就想着把它建立成一个图,然后每输入一条边就就深度遍历一下。
判断是否有环和是否能判断出大小顺序。。。
但是之后才知道一般递归都很耗时间。。。
尽管我加入了几个强大的剪枝。而且在discuss里面的数据都通过了。
不过还是超时。。。。
我特别注明一下,下面的代码是超时的代码额!!
希望大神能帮我修改一下ac。。小弟感激不尽!!!
下面是用拓扑排序0ms ac的链接。。前辈的
点击打开链接
判断是否有环和是否能判断出大小顺序。。。
但是之后才知道一般递归都很耗时间。。。
尽管我加入了几个强大的剪枝。而且在discuss里面的数据都通过了。
不过还是超时。。。。
我特别注明一下,下面的代码是超时的代码额!!
希望大神能帮我修改一下ac。。小弟感激不尽!!!
#include<stdio.h> #include<string.h> #include<stdlib.h> int map[30][30]; int chvist[30],order[30],isnothead[30],rudu[30]; int n,m,vist[30],num; int DFS(int j,int layer,int xx[]) { //vist[j] = 1; xx[layer] = j; if(layer == n-1) { for(int j=0;j<n;j++) order[j] = xx[j]; return 1; } for(int i=0;i<n;i++) if(map[j][i]) { if(!vist[i]) { vist[i] = 1; int ab = DFS(i,layer+1,xx); vist[i] = 0; if(ab != 0) return ab; } else return -1; } return 0; } int main() { char ch[5]; int xx[30]; while(scanf("%d%d",&n,&m) && n && m) { int isnotco = 1; int ring_or_order,times; memset(map,0,sizeof(map)); memset(chvist,0,sizeof(chvist)); memset(isnothead,0,sizeof(isnothead)); memset(rudu,0,sizeof(rudu)); for(int i=0;i<m;i++) { scanf("%s",ch); chvist[ch[0]-'A'] = 1; chvist[ch[2]-'A'] = 1; rudu[ch[2]-'A']++; if(ch[1] == '<') { map[ch[0]-'A'][ch[2]-'A'] = 1; isnothead[ch[0]-'A'] = 1; isnothead[ch[2]-'A'] = 0; } else { map[ch[1]-'A'][ch[0]-'A'] = 1; isnothead[ch[0]-'A'] = 0; isnothead[ch[2]-'A'] = 1; } int rudunum = 0; for(int tt=0;tt<n;tt++) if(rudu[tt]==0) rudunum++; for(int j=0;j<n;j++) if(chvist[j] && isnotco && isnothead[j]==1 && rudunum==1) { num = 0; memset(vist,0,sizeof(vist)); vist[j] = 1; int flag = DFS(j,0,xx); if(flag == -1) { isnotco = 0; ring_or_order = 1; times = i+1; } else if(flag == 1) { isnotco = 0; ring_or_order = 2; times = i+1; } } } if(isnotco) printf("Sorted sequence cannot be determined.\n"); else { if(ring_or_order==1) printf("Inconsistency found after %d relations.\n",times); else { printf("Sorted sequence determined after %d relations: ",times); for(int s=0;s<n;s++) printf("%c",order[s]+'A'); printf(".\n"); } } } return 0; }
下面是用拓扑排序0ms ac的链接。。前辈的
点击打开链接
相关文章推荐
- poj1094Sorting It All Out(拓朴排序)
- poj1094Sorting It All Out(拓朴排序)
- poj 图相关之1094Sorting It All Out
- poj1094Sorting It All Out
- POJ1094 Sorting It All Out
- POJ 1094 Sorting It All Out
- POJ 1094 Sorting It All Out
- poj 1094 Sorting It All Out
- POJ-1094-Sorting It All Out
- ACM篇:POJ 1094 -- Sorting It All Out
- POJ 1094 Sorting It All Out
- POJ 1094 Sort it all out (Topsort)
- poj-1094-Sorting It All Out
- 拓扑序列变形 之 poj 1094 Sorting It All Out
- poj 1094 Sorting It All Out
- POJ - 1094 Sorting It All Out
- nyoj 349 poj 1094 Sorting It All Out
- 【改了一天的拓扑排序】POJ 1094——Sorting It All Out
- poj 1094 Sorting It All Out
- 初学拓扑排序---POJ1094(Sorting It All Out)