Codeforces 876E National Property【思维建图+2-sat+输出可行解】好题~

45 篇文章 0 订阅
35 篇文章 0 订阅

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


题目大意:


现在给出N个数字串,所有数字串中的数字是1~m的。

现在有一种操作,可以使得一种数字变成带'的,1'<2'<3'<4'<.................<1<2<3<4...............................

现在问是否存在一种操作方式,使得这N个数字串从上到下是按照字典序递增排列的(不需要严格递增);


思路:


对于一个数字来讲,要么选择它本身,要么选择带'。那么问题相当于一个2-sat模型。


建图方式如下:

①对于相邻的两个字符串,决定其大小的位子就是两个数字串第一个出现数字不同的位子。

②我们设定相邻的两个数字串,对于决定其大小的位子上的两个数,上边的数字为a,下边的数字为b,那么存在两种情况需要进行讨论:

1.a<b,对于四种架构方式:

a<b,a<b' a'<b a'<b'中,为了变成上边小于下边的情况,只有a<b'是矛盾的,是不可行的,那么对应建立矛盾边:(a,b)和(b',a');

2.a>b,对于四种架构方式:

a>b a>b' a'>b a'>b'中,为了变成上边小于下边的情况,其中矛盾的有:a>b a>b' a'>b',同理建立矛盾边即可。


问题需要输出可行解,那么2-sat判定之后,反向建图跑拓扑排序维护一下就行了。


Ac代码:

#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
int temp,n,sig,cnt,tot,cont;
vector<int>mp[1006000],a[1006000],mp2[1006000];
int output[1006000];
int vis[1006000];
int low[1006000];
int dfn[1006000];
int print[1006000];
int stack[1006000];
int color[1006000];
int pos[1006000];
int degree[1006000];
void add(int x,int y)
{
    mp[x].push_back(y);
}
void top()
{
    memset(print,0,sizeof(print));
    queue<int >s;
    for(int i=1;i<=sig;i++)
    {
        if(degree[i]==0)
        {
            s.push(i);
        }
    }
    while(!s.empty())
    {
        int u=s.front();
        if(print[u]==0)
        {
            print[u]=1;print[pos[u]]=2;
        }
        s.pop();
        for(int i=0;i<mp2[u].size();i++)
        {
            int v=mp2[u][i];
            degree[v]--;
            if(degree[v]==0)s.push(v);
        }
    }
    cont=0;
    for(int i=1;i<=n;i++)if(print[color[i]]==1)output[cont++]=i;
}
void Tarjan(int u)
{
    vis[u]=1;
    dfn[u]=low[u]=cnt++;
    stack[++tot]=u;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(vis[v]==0)Tarjan(v);
        if(vis[v]==1)low[u]=min(low[u],low[v]);
    }
    if(low[u]==dfn[u])
    {
        sig++;
        do
        {
            vis[stack[tot]]=-1;
            color[stack[tot]]=sig;
        }
        while(stack[tot--]!=u);
    }
}
int Slove()
{
    sig=0;
    cnt=1;
    tot=-1;
    memset(degree,0,sizeof(degree));
    memset(stack,0,sizeof(stack));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(vis,0,sizeof(vis));
    memset(color,0,sizeof(color));
    for(int i=1;i<=n*2;i++)
    {
        if(vis[i]==0)
        {
            Tarjan(i);
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(color[i]==color[i+n])return 0;
        pos[color[i]]=color[i+n];
        pos[color[i+n]]=color[i];
    }
    for(int i=1;i<=n*2;i++)
    {
        for(int j=0;j<mp[i].size();j++)
        {
            int v=mp[i][j];
            if(color[i]!=color[v])
            {
                degree[color[i]]++;
                mp2[color[v]].push_back(color[i]);
            }
        }
    }
    top();
    return 1;
}
int main()
{
    while(~scanf("%d%d",&temp,&n))
    {
        for(int i=1;i<=n*2+5;i++)mp[i].clear(),a[i].clear();
        for(int i=1;i<=temp;i++)
        {
            int k;scanf("%d",&k);
            for(int j=1;j<=k;j++)
            {
                int x;scanf("%d",&x);
                a[i].push_back(x);
            }
        }
        int ok=1;
        for(int i=1;i<=temp-1;i++)
        {
            int flag=0;
            for(int j=0;j<min(a[i].size(),a[i+1].size());j++)
            {
                if(a[i][j]==a[i+1][j])continue;
                else
                {
                    flag=1;
                    if(a[i][j]<a[i+1][j])
                    {
                        add(a[i][j],a[i+1][j]);
                        add(a[i+1][j]+n,a[i][j]+n);
                    }
                    else
                    {
                        add(a[i][j],a[i+1][j]+n);
                        add(a[i+1][j],a[i][j]+n);

                        add(a[i][j],a[i+1][j]);
                        add(a[i+1][j]+n,a[i][j]+n);

                        add(a[i][j]+n,a[i+1][j]);
                        add(a[i+1][j]+n,a[i][j]);
                    }
                    break;
                }
            }
            if(flag==0&&a[i].size()>a[i+1].size())ok=0;
        }
        if(ok==0)printf("No\n");
        else
        {
            int ans=Slove();
            if(ans==1)
            {
                printf("Yes\n");
                memset(vis,0,sizeof(vis));
                for(int i=0;i<cont;i++)vis[output[i]]=1;
                vector<int>tmp;
                for(int i=1;i<=n;i++)if(vis[i]==0)tmp.push_back(i);
                printf("%d\n",tmp.size());
                for(int i=0;i<tmp.size();i++)
                {
                    printf("%d ",tmp[i]);
                }
                printf("\n");
            }
            else printf("No\n");
        }
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值