题意:有n个有小写字母组成的字符串,将部分小写字母改成对应的大写字母,注意某种小写字母更改,所有的这种小写字母都会更改。若能使这给定的n个字符串符合字典序由小到大排序,则输出Yes,并输出需要修改的字母。定义所有的大写字母字典序小于小写字母。

分析:

1、起决定作用的是前后两个字符串中第一个不同的字母。每个字母要么小写要么大写,显然2-sat问题。

2、假设前后两个串中第一个不同的字母分别为a,b。

当a < b 时,只有a < B矛盾。则当a为小写时,b一定为小写。b为大写时,a一定为大写。由此建边。

当a  > b时,a > b, A > B, a > B,都矛盾,因此,若a为小写,修改为大写,若b为大写,修改为小写。由此建边。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
vector<int> v[MAXN], G[MAXN << 1], rG[MAXN << 1], vs;
bool used[MAXN << 1];
int cmp[MAXN << 1];
int n, m;
vector<int> ans;
void addedge(int from, int to){
G[from].push_back(to);
rG[to].push_back(from);
}
void dfs(int v){
used[v] = true;
for(int i = 0; i < G[v].size(); ++i){
if(!used[G[v][i]]) dfs(G[v][i]);
}
vs.push_back(v);
}
void rdfs(int v, int k){
used[v] = true;
cmp[v] = k;
for(int i = 0; i < rG[v].size(); ++i){
if(!used[rG[v][i]]) rdfs(rG[v][i], k);
}
}
void scc(){
memset(used, false, sizeof used);
vs.clear();
for(int v = 2; v <= (m << 1 | 1); ++v){
if(!used[v]) dfs(v);
}
memset(used, false, sizeof used);
int k = 0;
for(int i = vs.size() - 1; i >= 0; --i){
if(!used[vs[i]]) rdfs(vs[i], k++);
}
}
int main(){
scanf("%d%d", &n, &m);
for(int i = 0; i < n; ++i){
int l, x;
scanf("%d", &l);
for(int j = 0; j < l; ++j){
scanf("%d", &x);
v[i].push_back(x);
}
}
for(int i = 1; i < n; ++i){
int id = -1;
int tmp = min(v[i - 1].size(), v[i].size());
for(int j = 0; j < tmp; ++j){
if(v[i - 1][j] != v[i][j]){
id = j;
break;
}
}
if(id == -1){
if(v[i - 1].size() > v[i].size()){
printf("No\n");
return 0;
}
}
else if(v[i - 1][id] < v[i][id]){
addedge(v[i - 1][id] << 1, v[i][id] << 1);
addedge(v[i][id] << 1 | 1, v[i - 1][id] << 1 | 1);
}
else{
addedge(v[i - 1][id] << 1, v[i - 1][id] << 1 | 1);
addedge(v[i][id] << 1 | 1, v[i][id] << 1);
}
}
scc();
for(int i = 2; i <= 2 * m; ++i){
if(cmp[i] == cmp[i + 1]){
printf("No\n");
return 0;
}
}
printf("Yes\n");
for(int i = 2; i <= 2 * m; i += 2){
if(cmp[i] < cmp[i + 1]){
ans.push_back(i >> 1);
}
}
int len = ans.size();
printf("%d\n", len);
if(len){
for(int i = 0; i < len; ++i){
if(i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
return 0;
}

  

CodeForces - 876E National Property(2-sat)的更多相关文章

  1. Codeforces 875C National Property(拓扑排序)

    题目链接  National Property 给定n个单词,字符集为m 现在我们可以把其中某些字母变成大写的.大写字母字典序大于小写字母. 问是否存在一种方案使得给定的n个单词字典序不下降. 首先判 ...

  2. Codeforces 876E National Property ——(2-SAT)

    在这题上不是标准的“a或b”这样的语句,因此需要进行一些转化来进行建边.同时在这题上点数较多,用lrj大白书上的做法会T,因此采用求强连通分量的方法来求解(对一个点,如果其拓扑序大于其为真的那个点,则 ...

  3. Codeforces 828B Black Square(简单题)

    Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...

  4. 1114 Family Property (25 分)

    1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...

  5. Codeforces C. Maximum Value(枚举二分)

    题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. 【刷题-PAT】A1114 Family Property (25 分)

    1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...

  7. Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) E. National Property(2-sat)

    E. National Property time limit per test 1 second memory limit per test 512 megabytes input standard ...

  8. CodeForces 215B Olympic Medal(数学啊)

    题目链接:http://codeforces.com/problemset/problem/215/B Description The World Programming Olympics Medal ...

  9. CodeForces 993B Open Communication(STL 模拟)

    https://codeforces.com/problemset/problem/993/b 这题不难,暴力就能过,主要是题意太难懂了 题意: 现在有两个人,每个人手中有一对数,第一个人手中的数是n ...

随机推荐

  1. 洛谷 P3009 [USACO11JAN]利润Profits

    嗯... 题目链接:https://www.luogu.org/problemnew/show/P3009 这是DP的另一个功能,求最大子段和(最大子段和模板:https://www.luogu.or ...

  2. 8.1.1默认的map函数、reduce函数、分区函数

    1.1.1         默认的map函数和reduce函数 (1)Maper和Reuducer默认类 如果没有指定maper类和reduce类,则会用默认的Maper和Reuducer类去处理数据 ...

  3. 「JLOI2014」松鼠的新家

    「JLOI2014」松鼠的新家 传送门 两种做法: 树上差分 \(O(n)\) 树链剖分 \(O(nlogn)\) 树剖比较好写而且无脑,树上差分复杂度优秀一些但是会有点难调. 这里给出树剖写法: 唯 ...

  4. Elasticsearch 6.8.4 启动报错解决方法

    运行环境:centos 7,jdk 1.8 问题一: ERROR: bootstrap checks failed max ] ] 原因:无法创建本地文件问题,用户最大可创建文件数太小 解决方案:切换 ...

  5. 软件工程 - 防御式编程EAFP vs LBYL

    概念 EAFP:easier to ask forgiveness than permission LBYL:look before you leap 代码 # LBYL def getUserInf ...

  6. SZWI3800

    xml <mapper namespace="jp.co.alsok.g6.zwi.dao.mapper.g6.custom.SZWI3800Mapper"> < ...

  7. 吴裕雄--天生自然ORACLE数据库学习笔记:Oracle系统调优

    --修改 alter system set large_pool_size=64m; --显示 show parameter large_pool_size; select sum(getmisses ...

  8. IntelliJ IDEA常用快捷键大全

    如果想要非常高效的使用IDEA这款工具,应该掌握图中已被标记的快捷键. 另: 代码实时模板生成:psvm/sout/ifn等 按Tab键快速生成模板. 转载请保留或注明出处:http://www.cn ...

  9. Spring开发踩坑记录

    #1 @EnableEurekaServer无法正常import原因是spring-cloud-dependencies版本太低,改成高版本的Edgware.SR4即可.参考:https://www. ...

  10. Android学习笔记17:单项选择RadioButton和多项选择CheckBox的使用

    请参见 http://www.android100.org/html/201406/05/19495.html