CodeForces - 876E National Property

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 < 3, 2' < 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 000, 1 ≤ 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 000, 1 ≤ 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 k distinct 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种字母(编号从1到m)。字典序的规则是字母编号小的排前边,编号大的排右边。

另外,还能给某个字母打撇,打撇的字母排在没打撇的前边。同样打了撇的字母,编号小的排前边。

第一行输入n和m。接下来n行,第i行的第一个数字Li表示第i单词有Li字母,后面Li个数字是该单词的字母顺序。

现在你可以给某些编号的字母打撇,要使得从上到下的单词符合上面规定的字典序。。

要是打撇也不能做到,就输出No。若打撇能做到,第一行Yes,第二行输出需要打撇的字母的数量,并且

输出对应需要打撇的字母编号。可能有多种打撇的方案,随意输出一个就行。

 

 

题解

要使从上到下的单词符合上面规定的字典序,有两种情况必须得考虑。。

 1.上面的字母为xxxxa

    下面的字母为xxxxb  (这两个字母左边的都一样,然后a>b)

这时候编号为a的字母必须打撇,b一定不能打撇。否则,你懂的。

 

2. 上面的字母为xxxxa

    下面的字母为xxxxb  (这两个字母左边的都一样,然后a

如果b要打撇,那么a必须也得打撇,我们说a被b牵连。

用一个vector数组Link[b]记录被b牵连的所有字母的编号。

一个字母可能牵连多个字母。。

 

我们把加撇叫做add_skim,不加撇叫做no_skim,用一个数组叫做必须加撇must_add,另一个数组记录

不能加撇叫做no_add。must_add[i]=1 表示编号为i的字母必须加撇。的如果碰到情况1,就把情况1所说的字母a及其牵连的b都列为必须加

撇的。如果加撇的时候发现某个数字是已经被no_add记录为不能加撇的,说明最后得输出No了。。

如果碰到情况2,若a被b牵连,就把a放到Link[b]里面。若b已经在must_add里面,即must_add[b] = 1,

就把a及其被牵连的字母加撇,此过程要加撇的字母若在no_add里面,说明最后得输出No。。

加撇的操作用递归(树的遍历)实现。要输出加撇的字母的编号很容易,这里就不说了

 

#include
#include
#include
#include
#include
using namespace std;
vector word1,word2;
queue Link[101000];
int must_add[101000], no_add[101000];
vector answer;
int n,m,ans = 1,cnt = 0;
void add_skim(int x){
    if(no_add[x]==1){
        ans = false;
        return;
    }
    answer.push_back(x);
    if(must_add[x]==0)
        cnt++;
    must_add[x] = 1;
    while(!Link[x].empty()){
        add_skim(Link[x].front());
        Link[x].pop();
    }
}
void no_skim(int x){
    if(must_add[x]==1){
        ans = false;
        return;
    }
    no_add[x] = 1;
}
int main(){
//    freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    memset(must_add,0,sizeof(must_add));
    memset(no_add,0,sizeof(no_add));
    int num , temp ,flag = 1; 
    answer.clear();
    scanf("%d",&num);
    word1.clear();
    while(num--){
        scanf("%d",&temp);
        word1.push_back(temp);
    }
    for(int i=1;i         flag = 1;
        scanf("%d",&num);
        word2.clear();
        for(int j=0;j             scanf("%d",&temp);
            word2.push_back(temp);
            if(ans==0 || flag == 0) 
                continue;
            if(j>=word1.size())
                continue;
            if(word1[j]>word2[j]){
                flag = 0;
                add_skim(word1[j]);
                no_skim(word2[j]);
            }
            else if(word1[j]                 flag = 0;
                Link[word2[j]].push(word1[j]);
                if(must_add[word2[j]])
                    add_skim(word1[j]);
            }
        }
        if(flag==1 && word1.size() > word2.size())
            ans =  false;
        word1 = word2;    
    }
    if(ans==1){
        printf("Yes\n%d\n",cnt);
        for(int i=1;i<=m;i++){
            if(must_add[i]==1)
                printf("%d ",i);
        }
        printf("\n");
    }
    else
        printf("No\n");
    return 0;

你可能感兴趣的:(CodeForces - 876E National Property)