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 ...
随机推荐
- SQL入门随笔(上机实验报告)
<数据定义部分> 一.定义模式和删除模式 a.为用户定义一个模式学生—课程模式 S-T CREATE SCHEMA "S-T" AUTHORIZATION USE ...
- BZOJ——1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏
http://www.lydsy.com/JudgeOnline/problem.php?id=1720 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1 ...
- raspi串口、python串口模块pyserial
一.安装 1.下载软件包pyserial-2.7.tar.gz 网址:https://pypi.python.org/pypi/pyserial 2.8uftp上传至/usr/local/src/ ...
- PYTHON 源码
http://www.wklken.me/index2.html http://blog.csdn.net/dbzhang800/article/details/6683440
- 异常来自 HRESULT:0x800A01A8
Windows 10 Enterprise Microsoft Office 2013 – Excel Oracle BI Publisher Desktop 11.1.1.7 异常来自 HRESUL ...
- 第24章、OnLongClickListener长按事件(从零开始学Android)
在Android App应用中,OnLongClick事件表示长按2秒以上触发的事件,本章我们通过长按图像设置为墙纸来理解其具体用法. 知识点:OnLongClickListener OnLongCl ...
- linux 源码编译安装apache
cc1 是c语言的编译器.
- ivy 入门
ivy 入门 http://www.blogjava.net/aoxj/archive/2009/03/31/263012.html https://www.cnblogs.com/end/archi ...
- [CSS3] Understand CSS Selector Specificity
It is hard to explain css selector specificty, to easy way to understand it is by playing around wit ...
- RESTful Web Service