zoukankan      html  css  js  c++  java
  • 27-拓扑排序-poj1094

    http://poj.org/problem?id=1094                                     
                                           Sorting It All Out
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 38379   Accepted: 13534

    Description

    An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.

    Input

    Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.

    Output

    For each problem instance, output consists of one line. This line should be one of the following three: 

    Sorted sequence determined after xxx relations: yyy...y. 
    Sorted sequence cannot be determined. 
    Inconsistency found after xxx relations. 

    where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence. 

    Sample Input

    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
    

    Sample Output

    Sorted sequence determined after 4 relations: ABCD.
    Inconsistency found after 2 relations.
    Sorted sequence cannot be determined.

    Source

    思路:就是利用拓扑排序,当条件不断增加时,依次判断,看是否能排好,同时要注意是否有环,注意在条件不足不能判断出先后顺序的情况下,依然要判断是否有环;
    其实有一点有疑问,就是在前面条件充足,可以排好序之后即没有对后续的进行判断了,但是如果后面的条件使得前面的成环呢?貌似样例没有这个,可以直接ac
    #include <iostream>
    #include <cstring> 
    
    using namespace std;
    int in[30];  //入度 
    int out[30]; //输出序列 
    int map[30][30]; //有向图 
    
    int TopoSort(int n){
    	int ct = 0, cpin[30];	
    	int flag = 1;
    	for(int i = 0; i < n; i++){
    		cpin[i] = in[i];
    	}
    	while(ct < n){
    		int p = -1, m = 0;
    		for(int i = 0; i < n; i++){
    			if(cpin[i] == 0){
    				p = i;
    				m++;
    			}
    		}
    		if(m == 0){
    			return -1; //有环 
    		}
    		if(m > 1){
    //			return 0;  //无法判断 
    			flag = 0;  //得出不能判断只有还有检测,因为可能后面可能有环 
    		}
    		out[ct++] = p;
    		cpin[p] = -1; 
    		for(int i = 0; i < n; i++){
    			if(map[p][i]){
    				cpin[i]--;
    	     	}
    		}  
    	}
    	return flag;  //排好序了 
    } 
    
    int main(){
    	std::ios::sync_with_stdio(false); 
    	int n, m;
    	char x, y;
    	char ch;
    	while(cin >> n >> m && (n != 0 && m != 0)){
    		memset(in, 0, sizeof(in));
    		memset(map, 0, sizeof(map));
    		int flag = 1;
    		for(int i = 0; i < m; i++){
    			cin >> x >> ch >> y;
    			if(flag == 0)
    				continue;  //注意就算前面已经判断出结果,也要输入完,但不需要判断了 
    			if(map[x - 'A'][y - 'A'] == 0){
    				map[x - 'A'][y - 'A'] = 1;
    				in[y - 'A']++;
    				int s = TopoSort(n); 
    				if(s == -1){
    					cout << "Inconsistency found after ";
    					cout << i + 1;
    					cout << " relations."  << endl;
    					flag = 0;
    				} 
    				if(s == 1){
    					cout << "Sorted sequence determined after ";
    					cout << i + 1;
    					cout << " relations: ";
    					for(int i = 0; i < n; i++){
    						cout << char(out[i] + 'A');
    					}
    					cout << "." << endl;
    					flag = 0;
    				}
    			}  
    		}
    		if(flag){
    			cout << "Sorted sequence cannot be determined." << endl;
    		}
    	}
    	return 0;
    } 
    

      

  • 相关阅读:
    hdu 2669 Romantic 扩展欧几里德
    fzu 1759 Super A^B mod C 大数幂取模
    POJ2429 SCU2106 GCD & LCM Inverse
    spring和hibernate整合时报sessionFactory无法获取默认Bean Validation factory
    SSH Secure Shell Client安装和使用
    乔布斯在斯坦福大学的演讲
    Java 事件处理机制
    Java 网络编程 简单接触TCP
    设计模式 职责链模式
    设计模式 命令模式
  • 原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/9316373.html
Copyright © 2011-2022 走看看