ZOJ Problem Set - 1060 Sorting It All Out
2012-05-06 18:35
330 查看
这题终于过了~~搞了很久的题啊~~N久啊~~
过了真他妈的爽啊~~
整理下思路~
1、边输入边判断是否矛盾,这是我今天搞的最久的操作。一开始的做法是输入A<B,就只判断所有比A小的数要小于B,其实还有一些情况也会出现的,我也举不出例子,但是肯定这是会遗漏某些情况的做法。后来改成全部的A<B都进行处理!如果出现矛盾则记录起来。
有的人理解成判断是否存在回路的方法也可以,一开始我的思路就是这样的,然后用dfs()判断一下,发现有些数据会坑爹地循环。也可以用传递闭包的算法做。
2、用数组来记录第几个操作。要拓扑排序的话,如果每次的输入都排一次那就太浪费时间,有可能会TLE。所以先把操作数记录起来,最后一次拓扑排序找出最后的顺序,然后按照顺序找出最大的操作数,就是结果啦!
3、拓扑排序~这里我用我自己的做法拓扑~ 不多说。
这题要是没有poj ,discuss 里神牛的测试数据的话,搞不出什么头绪~
http://poj.org/showmessage?message_id=133905
过了真他妈的爽啊~~
整理下思路~
1、边输入边判断是否矛盾,这是我今天搞的最久的操作。一开始的做法是输入A<B,就只判断所有比A小的数要小于B,其实还有一些情况也会出现的,我也举不出例子,但是肯定这是会遗漏某些情况的做法。后来改成全部的A<B都进行处理!如果出现矛盾则记录起来。
有的人理解成判断是否存在回路的方法也可以,一开始我的思路就是这样的,然后用dfs()判断一下,发现有些数据会坑爹地循环。也可以用传递闭包的算法做。
2、用数组来记录第几个操作。要拓扑排序的话,如果每次的输入都排一次那就太浪费时间,有可能会TLE。所以先把操作数记录起来,最后一次拓扑排序找出最后的顺序,然后按照顺序找出最大的操作数,就是结果啦!
3、拓扑排序~这里我用我自己的做法拓扑~ 不多说。
这题要是没有poj ,discuss 里神牛的测试数据的话,搞不出什么头绪~
http://poj.org/showmessage?message_id=133905
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <algorithm> #include <vector> #include <limits.h> #include <queue> #include <stack> using namespace std; int mp[27][27]; bool cot[27][27]; bool top[27][27]; int in[27]; int main() { int i,j,n,m; string str; int f1,f2,f3; //freopen("a.txt","r",stdin); // freopen("b.txt","w",stdout); while(cin>>n>>m) { if(n==0&&m ==0) break; memset(mp,0,sizeof(mp)); memset(cot,0,sizeof(cot)); memset(top,0,sizeof(top)); memset(in,0,sizeof(in)); f1 = -1;f2 = -1;f3 = -1; for(i = 1;i <= m;i ++) { cin>>str; if(cot[str[2]-'A'][str[0]-'A'] == 1) {f1 = i;continue;} cot[str[0]-'A'][str[2]-'A'] = 1; if(mp[str[0]-'A'][str[2]-'A']==0){mp[str[0]-'A'][str[2]-'A'] = i;top[str[0]-'A'][str[2]-'A'] = 1;} for(int ii = 0;ii < n;ii ++){ for(j = 0;j < n;j ++) { if(cot[ii][j]){ for(int k = 0;k < n;k ++) { if(cot[k][ii]) cot[k][j] = 1; } } } } } string ans = ""; int c =0,pos; for(i = 0;i < n;i ++) { for(j = 0;j < n;j ++) { if(top[j][i]) in[i] ++; } } //cout<<f2<<endl; for(i = 0;i < n;i ++) { c = 0; for(j = 0;j < n;j ++) { if(in[j] == 0) {c ++;pos = j;} } if(c > 1) {f2 = 1;break;} else if(c == 0) {f2 = 1;break;} else { ans += char(pos+'A'); in[pos] = -1; for(int k =0;k < n;k ++) { if(top[pos][k]) in[k] --; } } } if(f2==-1) { int len = ans.size(); int ans1 = 0; for(i = 1;i < len;i ++) { if(mp[ans[i-1]-'A'][ans[i]-'A']>ans1) ans1 =mp[ans[i-1]-'A'][ans[i]-'A']; } if(f1 != -1){ if(ans1 < f1) cout<<"Sorted sequence determined after "<<ans1<<" relations: "<<ans<<"."<<endl; else cout<<"Inconsistency found after "<<f1<<" relations."<<endl; }else{ cout<<"Sorted sequence determined after "<<ans1<<" relations: "<<ans<<"."<<endl; } } else { if(f1 != -1) cout<<"Inconsistency found after "<<f1<<" relations."<<endl; else cout<<"Sorted sequence cannot be determined."<<endl; } } }
相关文章推荐
- ZOJ-1060 Sorting It All Out
- ZOJ 1060 Sorting It All Out (POJ1094) (拓扑排序)
- ZOJ 1060 Sorting It All Out(POJ 1094)(关系闭包+计数排序)
- zoj 1060 && poj 1094 Sorting It All Out
- zoj 1060 || poj 1094 Sorting It All Out(拓扑排序)
- ZOJ 1060 poj 1094 Sorting It All Out(拓扑排序或 弗洛德)
- ZOJ 1060 poj 1094 Sorting It All Out(拓扑排序或 弗洛德)
- zoj 1060 Sorting It All Out 拓扑排序
- zoj 1060 Sorting It All Out
- ZOJ Problem Set - 1060
- POJ 3660 && NYIST 211 Cow Contest && ZOJ 1060 Sorting It All Out【Floyd算法扩展应用】
- POJ 1094 && ZOJ 1060 Sorting It All Out 【拓扑排序入门】
- POJ 3660 && NYIST 211 Cow Contest && ZOJ 1060 Sorting It All Out【Floyd算法扩展应用】
- POJ 1094 && ZOJ 1060 Sorting It All Out 【拓扑排序入门】
- ZOJ 1060 (POJ 1094) Sorting It …
- zoj 1060 Sorting It All Out(拓扑排序)
- ZOJ 1060 Sorting It All Out 拓扑排序
- JOJ 1089 & ZOJ 1060 & poj 1094 Sorting It All Out (邻接表的栈拓扑排序模板)
- ZOJ-1060-Sorting It All Out
- zoj 1060 Sorting It All Out(拓扑排序)