CodeForces875C[拓扑排序] Codeforces Round #440 [Div2E/Div1C]
只要保存每相邻两行字符串 第一个不同位 即可。然后按照 第一个不同位上的字符有: " 来自下一行的 大于 来自上一行的" 构图,跑拓扑排序即可。
当然要判断一下有没有环构成, 有环一定是NO(可以思考一下)。
还可以提前判断下一行是不是上一行的前缀, 如果是,那么一定是NO。
在拓扑排序的过程中保存答案。
比如说对于 test9 :
10 10
8 1 1 6 10 2 2 9 7
6 2 7 1 9 5 10
1 5
7 3 6 9 6 3 7 6
10 3 9 10 3 6 7 10 6 9 6
10 4 4 9 8 2 10 3 6 2 9
8 4 8 6 4 6 4 8 6
2 7 5
6 8 6 2 1 9 8
3 10 2 10
可以得到相互关系:2 > 1,5 > 2,3 > 5,9 > 6,4 > 3,8 > 4,7 > 4,8 > 7,10 > 8。 在进行拓扑排序的过程中, 如果发现需要 2>3 这种情况时, 就要给3打上标记 , 在代码中我使用一个 f[3] 代表 3的实际值, 当3被
标记时, 直接 -2000000,以方便比较。
当然,如果在比较时发现,就算后面的元素加上了标记,依旧大于前面的 (f[v]>f[u]),此时直接No(这也意味着两个元素都被标记了);
#include <bits/stdc++.h>
using namespace std;
const int N = ;
int n, m, t, tt;
bool mark[N];
vector<int> a[N];
vector<int> b;
vector<int> G[N];
int f[N], in[N];
void out() {
puts("No");
exit();
}
void solve() {
for (int i = ; i <= m; i++) f[i] = i + ;//类似于一个映射函数,方便比较
for (int i = ; i < n; i++) {
bool flag = true;
int len = min(a[i - ].size(), a[i].size());
for (int j = ; j < len; j++) {
if (a[i - ][j] != a[i][j]) {
G[a[i][j]].push_back(a[i - ][j]);
in[a[i - ][j]]++;
flag = false;
break;
}
}
if (flag && a[i - ].size() > a[i].size()) {
out();//后一个是前一个的前缀
}
}
int cnt = ;
//拓扑排序开始
queue<int>q;
for (int i = ; i <= m; i++) {
if (in[i] == ) {
cnt++;
q.push(i);
}
}
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i];
in[v]--;
if (f[v] > f[u]) {
f[v] -= ; //等于加了个标记
mark[v] = true;
if (f[v] > f[u]) out(); //如果加了标记以后,还是小,那么NO
b.push_back(v);
}
if (!in[v]) {
cnt++;
q.push(v);
}
}
}
//拓扑排序结束
if (cnt < m) out(); //判断是不是所有元素的拓扑序都被判定了(判环)
puts("Yes");
cout << b.size() << endl;
for (int i = ; i < b.size(); i++) {
cout << b[i] << ' ';
} puts("");
}
int main() {
scanf("%d%d", &n, &m);
for (int i = ; i < n; i++) {
scanf("%d", &t);
for (int j = ; j < t; j++) {
scanf("%d", &tt);
a[i].push_back(tt);
}
}
solve();
return ;
}
CodeForces875C[拓扑排序] Codeforces Round #440 [Div2E/Div1C]的更多相关文章
- 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names
题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...
- Codeforces Round #440 (Div. 2)【A、B、C、E】
Codeforces Round #440 (Div. 2) codeforces 870 A. Search for Pretty Integers(水题) 题意:给两个数组,求一个最小的数包含两个 ...
- HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)
逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...
- codeforces Round #440 A Search for Pretty Integers【hash/排序】
A. Search for Pretty Integers [题目链接]:http://codeforces.com/contest/872/problem/A time limit per test ...
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
A. Search for Pretty Integers 题目链接:http://codeforces.com/contest/872/problem/A 题目意思:题目很简单,找到一个数,组成这个 ...
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) D. Something with XOR Queries
地址:http://codeforces.com/contest/872/problem/D 题目: D. Something with XOR Queries time limit per test ...
- [日常] Codeforces Round #440 Div.2 大力翻车实况
上次打了一发ABC然后大力翻车...上午考试又停电+Unrated令人非常滑稽...下午终于到了CF比赛... 赛前大力安利了一发然后拉了老白/ $ljm$ / $wcx$ 一起打, 然后搞了个 TI ...
- Codeforces Round #440 Div. 1
A:显然应该尽量拆成4.如果是奇数,先拆一个9出来即可. #include<iostream> #include<cstdio> #include<cmath> # ...
- Codeforces Round #440 (Div. 2) A,B,C
A. Search for Pretty Integers time limit per test 1 second memory limit per test 256 megabytes input ...
随机推荐
- 解决MVC运行controller的时候只有有参构造函数但是程序一定要走无参构造函数的方法
方法如下 https://www.codeproject.com/Articles/560798/ASP-NET-MVC-Controller-Dependency-Injection-for-Be
- RabbitMQ使用教程(五)如何保证队列里的消息99.99%被消费?
1. 前情回顾 RabbitMQ使用教程(一)RabbitMQ环境安装配置及Hello World示例 RabbitMQ使用教程(二)RabbitMQ用户管理,角色管理及权限设置 RabbitMQ使用 ...
- cf550D. Regular Bridge(构造)
题意 给出一个$k$,构造一个无向图,使得每个点的度数为$k$,且存在一个桥 Sol 神仙题 一篇写的非常好的博客:http://www.cnblogs.com/mangoyang/p/9302269 ...
- SpringBoot之自动配置原理
我在前面的Helloworld的程序中已经分析过一次,配置原理了: 1).SpringBoot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration 2).@En ...
- mongodb添加管理员密码
use admin db.createUser( { user: "adminUser", pwd: "adminPass", roles: [ { role: ...
- PHP namespace、abstract、interface、trait使用介绍
小菜鸟一枚,一直搞不懂 namespace.abstract.interface.trait 这些关系,就抽出几天时间研究,做个总结,不足之处希望大家指正交流. namespace 命名空间 介绍:顾 ...
- 致敬wusir懒孩子自有懒孩子的生存之道之二
https://www.cnblogs.com/wupeiqi/ https://www.cnblogs.com/Eva-J/ https://www.cnblogs.com/wupeiqi/p/90 ...
- bootmem_free_node
该函数设置: 1.pgdata节点的成员 2.pgdata->zone的成员 3.初始化zone->free_area 4.初始化zone所包含的所有页对应的页框描述符page结构体 /* ...
- 笔记-python-standard library-8.10 copy
笔记-python-standard library-8.10 copy 1. copy source code:Lib/copy.py python中的赋值语句不复制对象,它创建了对象和目 ...
- 从头开始学习数据库及ADO.NET——竹子整理
目前为止,学习编程一年有余,写过管理系统,写过商城,写过桌面,接触的多了,乱七八糟的点太多,一堆前段框架,后台类库,纷纷杂杂,更新迭代之快也是令人咋舌.于是我就在想,作为一名程序员,哪些内容是实打实的 ...