zoukankan      html  css  js  c++  java
  • POJ1094-Sorting It All Out

    题目链接:点击打开链接

    Sorting It All Out

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 38356   Accepted: 13525

    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.

    题目大意:给出以下关系,①处理到第几个时能确定所有的关系,②处理到第几个时能发现矛盾,③没有矛盾所有点都能确定

    思路:先是想到并查集,但是谁比谁大不好比较。后来构建有向图,每读入一组关系,就拓扑排序一次。

    AC代码:

    #include<iostream>
    #include<cstdio>
    #include<set>
    #include<queue>
    #include<string.h>
    using namespace std;
    
    int n, m, cnt;
    
    int indegree[1010], temp[1010];
    
    vector<int> G[1010];//邻接表
    queue<int> q;
    char ans[31];
    
    bool check(int x, int y) {
    	for(int i = 0; i < G[x].size(); i++) {
    		if(G[x][i] == y)
    			return true;
    	}
    	return false;
    }
    
    int topo() {
    	for(int i = 0; i < n; i++) {//这里是跑所有的点
    		if(!indegree[i])
    			q.push(i);
    	}
    	bool unsure = false;//不确定的情况
    	cnt = 0;
    	while(!q.empty()) {//因为是跑所有的点,一旦出现有的点没有涉及,入读为0的点必然大于1
    		if(q.size() > 1)
    			unsure = true;
    		int u = q.front(); //取队列首个顶点u
    		ans[cnt++] = u + 'A';
    		q.pop();
    		for(int i = 0; i < G[u].size(); i++) {
    			int v = G[u][i];
    			indegree[v]--; //入度减一 
    			if(indegree[v] == 0) {//入度为0,入队 
    				q.push(v);
    			}
    		} 
    	}
    	if(cnt < n)//有环,矛盾
    		return 2;
    	if(unsure)
    		return 3;
    	return 1;
    }
    
    char s[3];
    int main() {
    	int step, flag;
    	while(~scanf("%d %d", &n, &m)) {
    		memset(G, 0, sizeof(G));
    		while(!q.empty()) q.pop();
    		if(n == 0 && m == 0)
    			break;
    		bool ok = false;
    		memset(indegree, 0, sizeof(indegree));
    		for(int i = 0; i < m; i++) {
    			scanf("%s", s);
    			if(ok)//就算知道了结果,也要读入所有数据
    				continue;
    			int l = s[0]-'A';
    			int r = s[2]-'A';
    			if(!check(l, r)) {//有过了,就不用再加入了
    				G[l].push_back(r);
    				indegree[r]++;
    			}
    			memcpy(temp, indegree, sizeof(indegree));//把当前入度情况记录下来
    			flag = topo();//拓扑判断一下
    			memcpy(indegree, temp, sizeof(temp));//再把入读情况放进入,进行下一条的时候,上个入读情况还是保留的
    			if(flag != 3) {
    				step = i+1;//记录当前是第几条关系
    				ok = true;
    			}
    		}//三种情况
    		if(flag == 2) {
    			printf("Inconsistency found after %d relations.
    ", step);
    		} else if(flag == 3){
    			printf("Sorted sequence cannot be determined.
    ");
    		} else {
    			ans[cnt] = '';
    			printf("Sorted sequence determined after %d relations: %s.
    ", step, ans);
    		} 
    	}	
    }
  • 相关阅读:
    打印九九乘法表
    PAT (Basic Level) Practice (中文) 1091 N-自守数 (15分)
    PAT (Basic Level) Practice (中文)1090 危险品装箱 (25分) (单身狗进阶版 使用map+ vector+数组标记)
    PAT (Basic Level) Practice (中文) 1088 三人行 (20分)
    PAT (Basic Level) Practice (中文) 1087 有多少不同的值 (20分)
    PAT (Basic Level) Practice (中文)1086 就不告诉你 (15分)
    PAT (Basic Level) Practice (中文) 1085 PAT单位排行 (25分) (map搜索+set排序+并列进行排行)
    PAT (Basic Level) Practice (中文) 1083 是否存在相等的差 (20分)
    PAT (Basic Level) Practice (中文) 1082 射击比赛 (20分)
    PAT (Basic Level) Practice (中文) 1081 检查密码 (15分)
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9572969.html
Copyright © 2011-2022 走看看