Topic outline
Given some lower-case strings, some words are required to be capitalized so that the lexicographic order of these strings is the same as the order given by the input.
Solving problems
For a character, there are only two states: change and unchanged, so I immediately think of 2-sat.
Consider how to build edges. For two adjacent strings, suppose you find the nearest two different characters a.b.
If a > b, then a must be capitalized and B must be capitalized.
If a
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
inline int _read(){
int num=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') num=num*10+ch-48,ch=getchar();
return num*f;
}
const int maxn=200005;
vector<int> a,b;
int m,n;
int tot,lnk[maxn],nxt[maxn],son[maxn];
int s[maxn],dfn[maxn],low[maxn],time,top,G,scc[maxn],ans[maxn],num,h[maxn];
bool ins[maxn];
void add(int x,int y){nxt[++tot]=lnk[x];lnk[x]=tot;son[tot]=y;}
void work(){
int i=0;
while(i<a.size()&&i<b.size()&&a[i]==b[i]) i++;
if (i>=a.size()||i>=b.size()){
if (a.size()>b.size()){printf("No\n");exit(0);}
return;
}
if (a[i]<b[i]) add((b[i]-1)*2+1,(a[i]-1)*2+1),add((a[i]-1)*2,(b[i]-1)*2);
if (a[i]>b[i]) add((b[i]-1)*2+1,(b[i]-1)*2),add((a[i]-1)*2,(a[i]-1)*2+1);
}
void tarjan(int x){
dfn[x]=++time;low[x]=time;
s[++top]=x;ins[x]=1;
for (int j=lnk[x];j;j=nxt[j])
if (!dfn[son[j]]){tarjan(son[j]);low[x]=min(low[x],low[son[j]]);}
else if (ins[son[j]]) low[x]=min(low[x],dfn[son[j]]);
if (dfn[x]==low[x]){
G++;
while(s[top]!=x) scc[s[top]]=G,ins[s[top--]]=0;
scc[x]=G;ins[s[top--]]=0;
}
}
int main(){
freopen("exam.in","r",stdin);
freopen("exam.out","w",stdout);
m=_read(),n=_read();
for (int i=1;i<=m;i++){
int x=_read();b.clear();
for (int j=1;j<=x;j++) b.push_back(_read());
if (i>1) work();
a=b;
}
for (int i=0;i<2*n;i++) if (!dfn[i]) tarjan(i);
for (int i=0;i<n;i++)
if (scc[2*i]==scc[2*i^1]){printf("No\n");return 0;}
else if (scc[2*i]<scc[2*i^1]) ans[i+1]=0;else ans[i+1]=1;
printf("Yes\n");
for (int i=1;i<=n;i++) if (ans[i]) h[++num]=i;
printf("%d\n",num);
for (int i=1;i<=num;i++) printf("%d ",h[i]);
return 0;
}