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 ...
随机推荐
- KVM 存储虚拟化
KVM 的存储虚拟化是通过存储池(Storage Pool)和卷(Volume)来管理的. Storage Pool 是宿主机上可以看到的一片存储空间,可以是多种类型,后面会详细讨论.Volume 是 ...
- Mysql Binlog日志文件介绍
一.Binlog简介 官方文档参考 https://dev.mysql.com/doc/refman/5.5/en/binary-log.html Binlog(Binary Log) 指数据库的表创 ...
- Lighttpd 服务器的安装
https://www.cnblogs.com/rongfengliang/articles/3503228.html
- js中window.location.search的用法和作用
用该属性获取页面 URL 地址: window.location 对象所包含的属性 属性 描述 hash 从井号 (#) 开始的 URL(锚) host 主机名和当前 URL 的端口号 hostnam ...
- 实验十二 swing图形界面设计
1.源程序 package information;import java.awt.Container;import java.awt.FlowLayout;import java.awt.event ...
- ios实现下载图片的裁减和显示
使用如下的方法可以裁减的同时保证了不丢失像素. - (void)connectionDidFinishLoading:(NSURLConnection *)connection{ // Set ...
- [置顶] MySQL -- 创建函数(Function
目标 如何在MySQL数据库中创建函数(Function) 语法 CREATE FUNCTION func_name ( [func_parameter] ) //括号是必须的,参数是可选的 RETU ...
- BUPT复试专题—众数(2014)
题目描述 有一个长度为N的非降数列,求数列中出现最多的数,若答案不唯一输出最小的数 输入 第一行T表示测试数据的组数(T<100) 对于每组测试数据: 第一行是一个正整数N表示数列长度 第二行有 ...
- Zabbix监控Mongo
安装Zabbix-agent # groupadd zabbix # useradd -g zabbix zabbix # yum -y install gcc mysql-community-dev ...
- Solidworks如何绘制装饰螺纹线
1 插入-注解,装饰螺纹线 2 绘制装饰螺纹线,选择螺纹的边线,标准选择ISO,下面可以选择的范围就确定了(M6的孔,只能选择M8的螺纹或者M10的螺纹),画好之后在3D图中并没有明确的螺纹样式 ...