Codeforces Round #441 (Div. 2): E. National Property(模拟?)

time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

You all know that the Library of Bookland is the largest library in the world. There are dozens of thousands of books in the library.

Some long and uninteresting story was removed...

The alphabet of Bookland is so large that its letters are denoted by positive integers. Each letter can be small or large, the large version of a letter x is denoted by x'. BSCII encoding, which is used everywhere in Bookland, is made in that way so that large letters are presented in the order of the numbers they are denoted by, and small letters are presented in the order of the numbers they are denoted by, but all large letters are before all small letters. For example, the following conditions hold: 2 < 32' < 3'3' < 2.

A word x1, x2, ..., xa is not lexicographically greater than y1, y2, ..., yb if one of the two following conditions holds:

  • a ≤ b and x1 = y1, ..., xa = ya, i.e. the first word is the prefix of the second word;
  • there is a position 1 ≤ j ≤ min(a, b), such that x1 = y1, ..., xj - 1 = yj - 1 and xj < yj, i.e. at the first position where the words differ the first word has a smaller letter than the second word has.

For example, the word "3' 7 5" is before the word "2 4' 6" in lexicographical order. It is said that sequence of words is in lexicographical order if each word is not lexicographically greater than the next word in the sequence.

Denis has a sequence of words consisting of small letters only. He wants to change some letters to large (let's call this process a capitalization) in such a way that the sequence of words is in lexicographical order. However, he soon realized that for some reason he can't change a single letter in a single word. He only can choose a letter and change all of its occurrences in all words to large letters. He can perform this operation any number of times with arbitrary letters of Bookland's alphabet.

Help Denis to choose which letters he needs to capitalize (make large) in order to make the sequence of words lexicographically ordered, or determine that it is impossible.

Note that some words can be equal.

Input

The first line contains two integers n and m (2 ≤ n ≤ 100 0001 ≤ m ≤ 100 000) — the number of words and the number of letters in Bookland's alphabet, respectively. The letters of Bookland's alphabet are denoted by integers from 1 to m.

Each of the next n lines contains a description of one word in format li, si, 1, si, 2, ..., si, li (1 ≤ li ≤ 100 0001 ≤ si, j ≤ m), where li is the length of the word, and si, j is the sequence of letters in the word. The words are given in the order Denis has them in the sequence.

It is guaranteed that the total length of all words is not greater than 100 000.

Output

In the first line print "Yes" (without quotes), if it is possible to capitalize some set of letters in such a way that the sequence of words becomes lexicographically ordered. Otherwise, print "No" (without quotes).

If the required is possible, in the second line print k — the number of letters Denis has to capitalize (make large), and in the third line print kdistinct integers — these letters. Note that you don't need to minimize the value k.

You can print the letters in any order. If there are multiple answers, print any of them.

Examples
input
4 3
1 2
1 1
3 1 3 2
2 1 1
output
Yes
2
2 3 
input
6 5
2 1 2
2 1 2
3 1 2 3
2 1 5
2 4 4
2 4 4
output
Yes
0
input
4 3
4 3 2 2 1
3 1 1 3
3 2 3 3
2 3 1
output
No


题意:

第一行输入n,m表示有n个数列,数列中数字的范围为1到m

之后每行第一个数字len表示第i个数列的长度

接下来len个数就是数列的元素

你可标记某些数字,被标记的数字一定小于没有被标记的数字

当然如果两个数字都被标记了或者都没被标记,那么就直接比大小

求出一种标记方案,使得所有的数列按字典序从小到大排序

如果存在输出Yes并输出任意一种,否则输出No

注意如果标记了数字5,那么所有序列的所有数字5相当于都被标记!也就是说标记的是种类


开个一个标记数组flag[](默认为0)

其实只有3种情况:

①:

第x个序列:    1 2 3 4 7

第x+1个序列:1 2 3 4 5

这个时候7必须被标记,5必须不被标记,记下flag[7] = -1;  flag[5] = 1

②:

第x个序列:    1 2 3 4 5

第x+1个序列:1 2 3 4 7

这个时候如果7被标记了,5就必须被标记,如果7没被标记,5就无所谓,7到5连接一条有向边

连无向边会挂终测!WA13)

③:

第x个序列:    1 2 3 4 5

第x+1个序列:1 2 3 4

特判,直接输出No,没特判也要挂终测


遍历完之后,对于flag[]=-1的点DFS一遍,所有和它连通的点全部flag[] = -1!

中间只要有冲突,例如一个点一定要标记,一定又不能标记答案就是No

否则Yes输出所有flag[]=-1的点即可

#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
int flag[100005];
int ok, a[100005], b[100005], vis[100005];
vector<int> G[100005];
void Sech(int u, int p)
{
	int i, v;
	vis[u] = 1;
	if(flag[u]==1)
		ok = -1;
	flag[u] = -1;
	for(i=0;i<G[u].size();i++)
	{
		v = G[u][i];
		if(v==p || vis[v])
			continue;
		Sech(v, u);
	}
}
int main(void)
{
	ok = 1;
	int n, m, i, j, len2, len1, ans;
	scanf("%d%d", &n, &m);
	for(i=1;i<=n;i++)
	{
		if(i==1)
		{
			scanf("%d", &len2);
			for(j=1;j<=len2;j++)
				scanf("%d", &b[j]);
			continue;
		}
		for(j=1;j<=len2;j++)
			a[j] = b[j];
		len1 = len2;
		scanf("%d", &len2);
		for(j=1;j<=len2;j++)
			scanf("%d", &b[j]);
		for(j=1;j<=min(len1, len2);j++)
		{
			if(a[j]<b[j])
			{
				G[b[j]].push_back(a[j]);
				break;
			}
			else if(a[j]>b[j])
			{
				if(flag[a[j]]==1 || flag[b[j]]==-1)
					ok = -1;
				flag[a[j]] = -1, flag[b[j]] = 1;
				break;
			}
		}
		if(len1>len2 && j==len2+1)
		{
			printf("No\n");
			return 0;
		}
	}
	if(ok==-1)
	{
		printf("No\n");
		return 0;
	}
	for(i=1;i<=m;i++)
	{
		if(vis[i]==0 && flag[i]==-1)
			Sech(i, -1);
		if(ok==-1)
			break;
	}
	if(ok==-1)
	{
		printf("No\n");
		return 0;
	}
	printf("Yes\n");
	ans = 0;
	for(i=1;i<=m;i++)
	{
		if(flag[i]==-1)
			ans++;
	}
	printf("%d\n", ans);
	for(i=1;i<=m;i++)
	{
		if(flag[i]==-1)
			printf("%d ", i);
	}
	if(ans!=0)
		printf("\n");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值