CodeForces - 876E National Property

本文介绍了一种解决字典序排列问题的算法,通过字母大小写转换来确保一系列单词按规定的字典序排列。文章详细解释了两种关键情况的处理方式,包括如何确定哪些字母必须转换为大写,以及如何处理字母之间的牵连关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

如果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<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
vector<int> word1,word2;
queue<int> Link[101000];
int must_add[101000], no_add[101000];
vector<int> 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<n;i++){
        flag = 1;
        scanf("%d",&num);
        word2.clear();
        for(int j=0;j<num;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]<word2[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;

内容概要:本文介绍了基于SMA-BP黏菌优化算法优化反向传播神经网络(BP)进行多变量回归预测的项目实例。项目旨在通过SMA优化BP神经网络的权重和阈值,解决BP神经网络易陷入局部最优、收敛速度慢及参数调优困难等问题。SMA算法模拟黏菌寻找食物的行为,具备优秀的全局搜索能力,能有效提高模型的预测准确性和训练效率。项目涵盖了数据预处理、模型设计、算法实现、性能验证等环节,适用于多变量非线性数据的建模和预测。; 适合人群:具备一定机器学习基础,特别是对神经网络和优化算法有一定了解的研发人员、数据科学家和研究人员。; 使用场景及目标:① 提升多变量回归模型的预测准确性,特别是在工业过程控制、金融风险管理等领域;② 加速神经网络训练过程,减少迭代次数和训练时间;③ 提高模型的稳定性和泛化能力,确保模型在不同数据集上均能保持良好表现;④ 推动智能优化算法与深度学习的融合创新,促进多领域复杂数据分析能力的提升。; 其他说明:项目采用Python实现,包含详细的代码示例和注释,便于理解和二次开发。模型架构由数据预处理模块、基于SMA优化的BP神经网络训练模块以及模型预测与评估模块组成,各模块接口清晰,便于扩展和维护。此外,项目还提供了多种评价指标和可视化分析方法,确保实验结果科学可信。
### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值