只要保存每相邻两行字符串 第一个不同位 即可。然后按照 第一个不同位上的字符有: " 来自下一行的 大于 来自上一行的" 构图,跑拓扑排序即可。

当然要判断一下有没有环构成, 有环一定是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]的更多相关文章

  1. 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names

    题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...

  2. Codeforces Round #440 (Div. 2)【A、B、C、E】

    Codeforces Round #440 (Div. 2) codeforces 870 A. Search for Pretty Integers(水题) 题意:给两个数组,求一个最小的数包含两个 ...

  3. HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)

    逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...

  4. 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 ...

  5. Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)

    A. Search for Pretty Integers 题目链接:http://codeforces.com/contest/872/problem/A 题目意思:题目很简单,找到一个数,组成这个 ...

  6. 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 ...

  7. [日常] Codeforces Round #440 Div.2 大力翻车实况

    上次打了一发ABC然后大力翻车...上午考试又停电+Unrated令人非常滑稽...下午终于到了CF比赛... 赛前大力安利了一发然后拉了老白/ $ljm$ / $wcx$ 一起打, 然后搞了个 TI ...

  8. Codeforces Round #440 Div. 1

    A:显然应该尽量拆成4.如果是奇数,先拆一个9出来即可. #include<iostream> #include<cstdio> #include<cmath> # ...

  9. 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 ...

随机推荐

  1. hdu-1317 XYZZY---Floyd判连通+bellman最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 题目大意: 题意:有n个房间(n<=100),每个房间有一个点权(第1号房间和第n号房间 ...

  2. POJ-1274 The Perfect Stall---二分图模板

    题目链接: https://vjudge.net/problem/POJ-1274 题目大意: 有n个奶牛和m个谷仓,现在每个奶牛有自己喜欢去的谷仓,并且它们只会去自己喜欢的谷仓吃东西,问最多有多少奶 ...

  3. vuejs里面v-if,v-show和v-for

    <div id='root'> <div v-if='show'>helle world</div> <button @click='handleClick' ...

  4. js转换时间戳成日期格式

    <script> function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().rep ...

  5. Vmware 不能上网

    Vmware 安装 WIN7 不能上网,如何解决? 情况一: 虚拟机右下角出现红色叉号,检查物理的服务是否开启“VMware NAT Service” 1 .开启方法:WIN + R -> 输入 ...

  6. oracle常用运维sql语句

    1.查询dblink语句 col owner for a20col db_link for a30col username for a20col host for a30set linesize 12 ...

  7. MSBuild常用方法

    打包后把nuget包复制到指定的目录 <Target Name="CopyPackage" AfterTargets="Pack"> <Cop ...

  8. Python 文件读写 文件和路径

    1.在Windows上,使用倒斜杆作为文件夹之间的分隔符,在Linux上,使用正斜杠作为路径分隔符.在编写Python脚本时,可以os.path.join()函数来处理 在Windows环境下命令如下 ...

  9. css清除浮动,清除子节点margin溢出问题

    清除浮动 .clearfix:after{ content:”.”; display:block; height:0; clear:both; visibility:hidden; } 清除margi ...

  10. 算法图解之大O表示法

    什么是大O表示法 大O表示法可以告诉我们算法的快慢. 大O比较的是操作数,它指出了算法运行时间的增速. O(n) 括号里的是操作数. 举例 画一个16个格子的网格,下面分别列举几种不同的画法,并用大O ...