POJ1094:Sorting It All Out(拓扑排序)
2017-08-04 01:27
525 查看
Sorting It All Out
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
Sample Output
Source
East Central North America 2001
题意:给N个字母,M个大小关系,问能否确定这N个字母的大小顺序,输出有三种:通过前X个关系能确定,通过前X个关系发现矛盾,所有关系都不能确定他们的大小顺序。
思路:这题怎么没给M范围,每增加一个关系都进行一次拓扑排序即可,处理起来不算难。
# include <iostream>
# include <cstdio>
# include <cstring>
# include <vector>
# include <algorithm>
# define pb push_back
using namespace std;
const int maxn = 30;
vector<int>v[maxn];
int in[maxn], In[maxn], q[maxn<<1], vis[maxn], ans[maxn];
int n, m, flag, flag2, cur, id[28];
bool cmp(int x, int y){return ans[x]<ans[y];}
void check()
{
int l=0, r=0, icount=0;
memset(ans, 0, sizeof(ans));
for(int i=0; i<n; ++i) In[i] = in[i];
for(int i=0; i<n; ++i) if(vis[i] && In[i]==0) q[r++] = i, ans[i]=1, ++icount;
while(l<r)
{
int u = q[l++];
for(int j=0; j<v[u].size(); ++j)
{
if(ans[v[u][j]]) {flag = 1; return;}
if(--In[v[u][j]] == 0)//只要它入度不为0,说明还有比它等级小的,先不放进队列。
{
++icount;
ans[v[u][j]] = ans[u]+1;
q[r++] = v[u][j];
}
}
}
if(icount < cur) flag = 1;//icount记录拓扑排序遍历过的节点数,小于当前已经出现的节点数,说明存在有向环。
if(cur >= n)
{
int tmp[28]={0};
for(int i=0; i<n; ++i)
if(++tmp[ans[i]] > 1) return;//判断这N个节点的“等级”有无重复。
flag2 = 1;
}
}
int main()
{
//freopen("in.txt","w",stdout);
while(~scanf("%d%d",&n,&m),n+m)
{
char c, d;
flag = flag2 = cur = 0;
memset(vis, 0, sizeof(vis));
memset(in, 0, sizeof(in));
for(int i=0; i<n; ++i) id[i]=i, v[i].clear();
for(int i=1; i<=m; ++i)
{
getchar();
scanf("%c<%c",&c,&d);
if(flag || flag2) continue;
if(++vis[c-'A'] == 1) ++cur;
if(++vis[d-'A'] == 1) ++cur;
v[c-'A'].pb(d-'A');
++in[d-'A'];
check();
if(flag) {printf("Inconsistency found after %d relations.\n",i);}
else if(flag2)
{
printf("Sorted sequence determined after %d relations: ",i);
sort(id, id+n, cmp);
for(int i=0; i<n; ++i) printf("%c",id[i]+'A');
printf(".\n");
}
}
if(!flag&&!flag2) printf("Sorted sequence cannot be determined.\n");
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 35361 | Accepted: 12417 |
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
East Central North America 2001
题意:给N个字母,M个大小关系,问能否确定这N个字母的大小顺序,输出有三种:通过前X个关系能确定,通过前X个关系发现矛盾,所有关系都不能确定他们的大小顺序。
思路:这题怎么没给M范围,每增加一个关系都进行一次拓扑排序即可,处理起来不算难。
# include <iostream>
# include <cstdio>
# include <cstring>
# include <vector>
# include <algorithm>
# define pb push_back
using namespace std;
const int maxn = 30;
vector<int>v[maxn];
int in[maxn], In[maxn], q[maxn<<1], vis[maxn], ans[maxn];
int n, m, flag, flag2, cur, id[28];
bool cmp(int x, int y){return ans[x]<ans[y];}
void check()
{
int l=0, r=0, icount=0;
memset(ans, 0, sizeof(ans));
for(int i=0; i<n; ++i) In[i] = in[i];
for(int i=0; i<n; ++i) if(vis[i] && In[i]==0) q[r++] = i, ans[i]=1, ++icount;
while(l<r)
{
int u = q[l++];
for(int j=0; j<v[u].size(); ++j)
{
if(ans[v[u][j]]) {flag = 1; return;}
if(--In[v[u][j]] == 0)//只要它入度不为0,说明还有比它等级小的,先不放进队列。
{
++icount;
ans[v[u][j]] = ans[u]+1;
q[r++] = v[u][j];
}
}
}
if(icount < cur) flag = 1;//icount记录拓扑排序遍历过的节点数,小于当前已经出现的节点数,说明存在有向环。
if(cur >= n)
{
int tmp[28]={0};
for(int i=0; i<n; ++i)
if(++tmp[ans[i]] > 1) return;//判断这N个节点的“等级”有无重复。
flag2 = 1;
}
}
int main()
{
//freopen("in.txt","w",stdout);
while(~scanf("%d%d",&n,&m),n+m)
{
char c, d;
flag = flag2 = cur = 0;
memset(vis, 0, sizeof(vis));
memset(in, 0, sizeof(in));
for(int i=0; i<n; ++i) id[i]=i, v[i].clear();
for(int i=1; i<=m; ++i)
{
getchar();
scanf("%c<%c",&c,&d);
if(flag || flag2) continue;
if(++vis[c-'A'] == 1) ++cur;
if(++vis[d-'A'] == 1) ++cur;
v[c-'A'].pb(d-'A');
++in[d-'A'];
check();
if(flag) {printf("Inconsistency found after %d relations.\n",i);}
else if(flag2)
{
printf("Sorted sequence determined after %d relations: ",i);
sort(id, id+n, cmp);
for(int i=0; i<n; ++i) printf("%c",id[i]+'A');
printf(".\n");
}
}
if(!flag&&!flag2) printf("Sorted sequence cannot be determined.\n");
}
return 0;
}
相关文章推荐
- Poj1094 Sorting It All Out (拓扑排序)
- POJ1094 Sorting It All Out 拓扑排序判大小关系
- poj1094 Sorting It All Out 拓扑排序
- POJ1094 Sorting It All Out (拓扑排序)
- poj1094 Sorting It All Out (拓扑排序)
- poj1094_Sorting It All Out_拓扑排序
- POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang
- POJ 1094 Sorting It All Out (拓扑排序)
- Sorting It All Out(拓扑排序)
- poj_1094 Sorting It All Out(拓扑排序 + floyd)
- POJ 1094:Sorting It All Out:拓扑排序的较为繁琐的逻辑变形
- POJ 1094 Sorting It All Out【拓扑排序】
- POJ-1094-Sorting It All Out(拓扑排序)
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
- POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)
- zoj 1060 || poj 1094 Sorting It All Out(拓扑排序)
- [poj1094]Sorting It All Out_拓扑排序
- poj1094 Sorting It All Out
- POJ 1094 Sorting It All Out(拓扑排序)
- zoj 1060 Sorting It All Out(拓扑排序)