题目链接
每个字符只能取小写和大写两种情况,因此满足2-sat的要求。
我们从第2个word开始,与前一个word进行比较。比较的时候从word左边的字符开始,找到左起的第一个不相同的字符(位置为pos)。
1.如果word[i-1][pos]<word[i][pos]。
那么表明如果word[i-1][pos]取小写的话,那么word[i][pos]一定要取小写。
同时word[i][pos]取大写的时候,word[i-1][pos]也一定要取大写。
于是连边(word[i-1][pos]+n) ->(word[i][pos] + n) 以及 (word[i][pos]) -> (word[i-1][pos])
2.如果word[i-1][pos] > word[i][pos]的话
表明word[i-1][pos]一定要取大写,word[i][pos]一定要取小写。
即:
word[i-1][pos] -> word[i-1][pos] +n
word[i][pos]+n->word[i-1][pos]
note:
+n代表大写
不加代表小写
题解:
#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int maxn = 1e6;
int head[maxn];
int DFN[maxn],LOW[maxn],stk[maxn],visit[maxn],belong[maxn];
vector<int> scc[maxn];
int tot,idx,cnt,sccnum;
int n,color[maxn],degree[maxn],pos[maxn];
struct Es{ int v; int next; int cost;
}Es[maxn<<1];
vector<int> bkG[maxn];
void init(){ sccnum = tot = idx = cnt = 0; memset(head,-1,sizeof(head)); memset(DFN,0,sizeof(DFN)); memset(LOW,0,sizeof(LOW)); memset(visit,0,sizeof(visit)); memset(color,0,sizeof(color)); memset(degree,0,sizeof(degree));memset(pos,0,sizeof(pos));for(int i = 0;i < maxn;i++) scc[i].clear();for(int i = 0;i < maxn;++i) bkG[i].clear();
}
inline void add_edge(int i,int j,int cost = 1){ Es[cnt].v = j; Es[cnt].cost = cost; Es[cnt].next = head[i]; head[i] = cnt++;
}
void tarjan(int x)
{DFN[x]=LOW[x]=++tot;stk[++idx]=x;visit[x]=1;for(int i=head[x];i!=-1;i=Es[i].next){if(!DFN[Es[i].v]) {tarjan(Es[i].v);LOW[x] = min(LOW[x],LOW[Es[i].v]);}else if(visit[Es[i].v ]){ LOW[x] = min(LOW[x],DFN[Es[i].v]);}}if(LOW[x]==DFN[x]){++sccnum;do{int item = stk[idx];belong[item] = sccnum;scc[sccnum].push_back(item);visit[item]=0;idx--;}while(x!=stk[idx+1]);}return ;
}
bool check(){for(int i = 1;i <= n;i++){if(belong[i] == belong[i+n]) return false;}return true;
}
bool solve(){for(int i = 1;i <= 2*n;++i )if(!DFN[i]) tarjan(i);if(!check()) return false;for(int i = 1;i <= n;++i){if(!pos[belong[i]]){pos[belong[i]] = belong[i+n];pos[belong[i+n]] = belong[i];}}for(int i = 1;i <= 2*n;++i){for(int e = head[i];e != -1;e = Es[e].next){int v = Es[e].v;if(belong[i] != belong[v]){degree[belong[i]]++;bkG[belong[v]].push_back(belong[i]);}}}queue<int> que;for(int i = 1;i <= sccnum;++i){if(!degree[i])que.push(i);}while(!que.empty()){int u = que.front();que.pop();if(!color[u]){color[u] = 1;color[pos[u]] = 2;}for(int i = 0;i < bkG[u].size();++i){int v = bkG[u][i];degree[v]--;if(!degree[v])que.push(v);}}vector<int> ans;for(int i = 1;i <= n;++i){if(color[belong[i]] != 1) {//Yesans.push_back(i);}} printf("Yes\n%d\n",ans.size());for(int i = 0;i < ans.size();++i){printf("%d ",ans[i]);}return true;
}
int m;
vector<int> vec[maxn];
int main(){init();scanf("%d%d",&m,&n);for(int i = 0;i < m;++i){int num;scanf("%d",&num);for(int j = 0;j < num;++j){int x;scanf("%d",&x);vec[i].push_back(x);}}int f = 1;for(int i = 1;i < m;++i){int id = 0;while(id < vec[i-1].size() && id < vec[i].size() && vec[i-1][id] == vec[i][id]) ++id;if(id >= vec[i-1].size() || id >= vec[i].size()){if(vec[i-1].size() <= vec[i].size()) continue;else{f = 0;break;}}else if(vec[i-1][id] < vec[i][id]){add_edge(vec[i][id]+n,vec[i-1][id]+n);add_edge(vec[i-1][id],vec[i][id]);}else{add_edge(vec[i][id]+n,vec[i][id]);add_edge(vec[i-1][id],vec[i-1][id]+n);}}if(!f || !solve()){puts("No");}return 0;
}