codeforces 875c National Property

http://www.elijahqi.win/archives/1305
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
Note
In the first example after Denis makes letters 2 and 3 large, the sequence looks like the following:

2’
1
1 3’ 2’
1 1
The condition 2’ < 1 holds, so the first word is not lexicographically larger than the second word. The second word is the prefix of the third word, so the are in lexicographical order. As the first letters of the third and the fourth words are the same, and 3’ < 1, then the third word is not lexicographically larger than the fourth word.

In the second example the words are in lexicographical order from the beginning, so Denis can do nothing.

In the third example there is no set of letters such that if Denis capitalizes them, the sequence becomes lexicographically ordered.

所有大写字母均字典序小于小写字母 同大小写的 字母按照字母表的顺序排字典序

出题人给的思路是简化版2-sat然而 一些神犇直接把完整版的2-sat贴过来A了这题 太强ORZ。

简化的思路就是 如果我上一串字符对应的相同位置小于下一个 那么 我应该把上一个和下一个这两种字符连一个有向边 从大指向小用来表示如果改了大的 那么小的也一定要改

如果我上一串字符对应的相同位置大于下一个 直接把前一个串中的 改大写就好

然后dfs全部改一遍 最后检查一遍 判是否冲突即可

Let the strings si and si + 1 are not prefixes of each other. Then it is necessary that si, k < si + 1, k, where k is the first position, where si and si + 1 differ.
Consider strings si and si + 1. Let k be the first position in which they differ. Then there are two cases:
If si, k > si + 1, k, you capitalize si, k and not capitalize si, k + 1.
If si, k < si + 1, k, both these letters should be capitalized or not capitalizes simultaneously.
Let’s make a graph in which letters will be vertexes. If si, k > si + 1, k, then mark si, k as capitalized, otherwise make a directed edge between si + 1, k and si, k. It means that if we capitalize si + 1, k, you also should capitalize si, k.
Note that our graph is acyclic because the edges are directed from big letters to small letters. Using dfs we capitalize all the letters, that are reachable from the capitalized letters and check the answer. If the answer is wrong, there is no answer.

#include<cstdio>
#include<algorithm>
#include<vector>
#define N 110000
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
    return x*f;
}
struct node{int y,next;}data[N];
vector<int> array[N],ans;int num,n,m,h[N],mark[N],s[N];
inline void insert1(int x,int y){
    data[++num].y=y;data[num].next=h[x];h[x]=num;
}
void dfs(int x){
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;if (mark[y]) continue;
        mark[y]=1,ans.push_back(y);dfs(y);
    }
}
inline bool les(int x,int y){
    if (mark[x]^mark[y]){
        if (mark[x]) return 1;else return 0;
    }return x<y;
}
inline bool great(int x,int y){
    if (mark[x]^mark[y]){
        if (mark[x]) return 0;else return 1;
    }return x>y;
}
inline bool check(int x1,int x2){
    int minn=min(array[x1].size(),array[x2].size());
    for (int i=0;i<minn;++i){
        if (les(array[x1][i],array[x2][i])) return 1;
        if (great(array[x1][i],array[x2][i])) return 0;
    }
}
int main(){
    freopen("cf.in","r",stdin);
    n=read();m=read();int cnt=1;
    for (int i=1;i<=n;++i){
        int len_next=read(),len_pre=array[cnt].size();
        for (int j=0;j<len_next;++j) s[j]=read();
        int minn=min(len_next,len_pre);bool flag=0;
        for (int j=0;j<minn;++j){
            if (array[cnt][j]<s[j]) {insert1(s[j],array[cnt][j]);flag=1;break;}
            if (array[cnt][j]>s[j]) {if (!mark[array[cnt][j]])ans.push_back(array[cnt][j]),mark[array[cnt][j]]=1;flag=1;break;}
        }
        if (flag){
            ++cnt;for (int j=0;j<len_next;++j) array[cnt].push_back(s[j]);continue;
        }else if (len_pre>len_next) {printf("No");return 0;}
        for (int j=len_pre;j<len_next;++j) array[cnt].push_back(s[j]);
    }
    for (int i=m;i>=1;--i) if(mark[i]) dfs(i);
    for (int i=1;i<cnt;++i) if(!check(i,i+1)) {printf("No");return 0;}printf("Yes\n%d\n",ans.size());
    for (int i=0;i<ans.size();++i) printf("%d ",ans[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值