poj 1094 Sorting It All Out 解题报告
题目链接:http://poj.org/problem?id=1094
题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具体的字母顺序不能确定但至少不矛盾。这些关系均是这样的一种形式: 字母1 < 字母2
这道题目属于图论上的拓扑排序,由于要知道读入第几行可以确定具体的顺序,所以每次读入都需要进行拓扑排序来检验,这时每个点的入度就需要存储起来,所以就有了代码中memcpy 的使用了。
拓扑排序的思路很容易理解,但写起来还是需要注意很多细节。参考了别人的代码,第一次写,受益匪浅啊。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue> using namespace std; const int maxn = + ;
vector<int> vi[maxn];
queue<int> q;
int in[maxn], temp[maxn];
int n, m, cnt;
char ans[maxn], input[]; bool check(int x, int y) // 检查之前是否插入过该边
{
for (int i = ; i < vi[x].size(); i++)
{
if (vi[x][i] == y)
return true;
}
return false;
} int Toposort()
{
while (!q.empty())
q.pop();
for (int i = ; i < n; i++) // 将入度为0的点入队
{
if (!in[i])
q.push(i);
}
bool unsure = false;
cnt = ;
while (!q.empty()) // 取出第一个入度为0的点
{
if (q.size() > ) // 假如有n个点的DAG入度为0的点只有一个,那么就可以确定已排好序
unsure = true;
int tmp = q.front(); ans[cnt++] = tmp + 'A';
q.pop();
for (int i = ; i < vi[tmp].size(); i++) // 该点引出的边删除
{
in[vi[tmp][i]]--; // 即入度减1
if (!in[vi[tmp][i]]) // 恰好入度减1后,该点入度变为0
q.push(vi[tmp][i]);
}
}
if (cnt < n) // 有环
return ;
if (unsure) // 有待进一步斟酌
return ;
return ; // 已经排好序
} int main()
{
int step, flag;
while (scanf("%d%d", &n, &m) != EOF && (m || n))
{
for (int i = ; i < maxn; i++)
vi[i].clear();
bool ok = false;
memset(in, , sizeof(in));
for (int i = ; i <= m; i++)
{
scanf("%s", input);
if (ok)
continue;
int l = input[] - 'A';
int r = input[] - 'A';
if (!check(l, r))
{
vi[l].push_back(r);
in[r]++;
}
memcpy(temp, in, sizeof(in)); // 每个点的入度数处理前先拷贝一份以待下一次输入再用
flag = Toposort();
memcpy(in, temp, sizeof(temp));
if (flag != ) // 暂时不能判断属于哪种情况,先保存step数
{
step = i;
ok = true;
}
}
if (flag == )
{
ans[cnt] = '\0';
printf("Sorted sequence determined after %d relations: %s.\n", step, ans);
}
else if (flag == )
printf("Inconsistency found after %d relations.\n", step);
else
printf("Sorted sequence cannot be determined.\n");
}
return ;
}
poj 1094 Sorting It All Out 解题报告的更多相关文章
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- poj 1094 Sorting It All Out(图论)
http://poj.org/problem?id=1094 这一题,看了个大牛的解题报告,思路变得非常的清晰: 1,先利用floyd_warshall算法求出图的传递闭包 2,再判断是不是存在唯一的 ...
- poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- USACO Section2.1 Sorting a Three-Valued Sequence 解题报告
sort3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- 题解报告:poj 1094 Sorting It All Out(拓扑排序)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...
- nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】
Sorting It All Out 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 An ascending sorted sequence of distinct ...
- POJ 1094 Sorting It All Out【拓扑排序】
题目链接: http://poj.org/problem?id=1094 题意: 给定前n个字母的大小关系,问你是否 根据前xxx个关系得到上升序列 所有关系都无法确定唯一的一个序列 第xxx个关系导 ...
- [ACM] POJ 1094 Sorting It All Out (拓扑排序)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26801 Accepted: 92 ...
随机推荐
- 玩转css样式选择器----当父元素有多个子元素时选中第一个
- Codeforces Round #291 (Div. 2) C. Watto and Mechanism [字典树]
传送门 C. Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- python操作excel--生成图表
[问题] 想要折腾Python中的Excel中的图标,Chart,Graph. [解决过程] 1.参考: use python to generate graph in excel 说是可以用pywi ...
- CodeBlock换肤
CodeBlock换肤 conf文件下载地址 我的是在D:\Program Files (x86)\CodeBlocks\AppData\codeblocks\default.conf 然后替换本地的 ...
- List 与 ArrayList 的使用
最近回顾 java 集合,发现大部分程序中都在使用 List list = new ArrayList(); 也有部分程序使用 ArrayList list = new ArrayList(); 那么 ...
- delphi学习路线
酷派(53376063) 11:04:19 1.语法基础PASCAL精要 先看个1-3遍,这是基础中的基础,只要没弄清楚看10遍都不多,当然最好结合着代码实例去看.(以后遇到哪儿不熟练继续反复看)2 ...
- MySQL 5.6.20-4 and Oracle Linux DTrace
https://blogs.oracle.com/wim/entry/mysql_5_6_20_4?utm_source=tuicool&utm_medium=referral By WimC ...
- uibutton去掉点击后背景有阴影的方法
1,将normal和highlight两种方式都设置上图片即可 UIButton *goback = [[UIButton alloc]initWithFrame:CGRectMake(5.0f, 5 ...
- Qt编写串口通信程序全程图文讲解 .
在Qt中并没有特定的串口控制类,现在大部分人使用的是第三方写的qextserialport类,我们这里也是使用的该类.我们可以去 http://sourceforge.net/projects/qex ...
- Unity3d插件]EasyTouch简单使用方法
EasyTouch使用 EasyTouch 文件夹[-] 一.效果图 二.操作步骤 1.官方文档上的步骤 2.翻译一下以上的步骤 3.依据官方的这些提示.自己来做一个属于自己的人物遥感控制 对于移动平 ...