题意:有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. C — 小知识

    老是记错int与void*之间的转换,所以记录一个,顺便用一下一些宏.预处理... int与void*的转换.打印变量名: #include <stdio.h> // 打印变量名 #def ...

  2. BUUCTF知识记录

    [强网杯 2019]随便注 先尝试普通的注入 发现注入成功了,接下来走流程的时候碰到了问题 发现过滤了select和where这个两个最重要的查询语句,不过其他的过滤很奇怪,为什么要过滤update, ...

  3. python操作日志

    # # import nnlog# my_log=nnlog.Logger('nhy.log',when='S',backCount=5)# my_log.debug('这是dedug')# my_l ...

  4. Go Start

    一.安装 下载解压后,配置PATH tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz export PATH=$PATH:/usr/local/go ...

  5. 吴裕雄--天生自然ORACLE数据库学习笔记:管理表空间和数据文件

    col tablespace_name for a10 col file_name for a50 col bytes ,, select tablespace_name,file_name,byte ...

  6. 杭电 2028 ( Lowest Common Multiple Plus )

    链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2028 题目要求:就是求最大公倍数,我百度了一下,最好实现的算法就是:       公式法 由于 ...

  7. 该怎样应对IoT和边缘计算安全挑战

    导读 虽然智能家居的响应延迟似乎不是大问题,但如果自动驾驶汽车需要刹车,而数据出现延迟或者被黑客拦截或操纵,这可能造成灾难性后果.这里将需要边缘计算安全. 边缘计算可在靠近远程设备的位置提供计算.存储 ...

  8. Educational Codeforces Round 72 (Rated for Div. 2)C(暴力)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[200007];int a[20 ...

  9. vue + element ui table表格二次封装 常用功能

    因为在做后台管理项目的时候用到了大量的表格, 且功能大多相同,因此封装了一些常用的功能, 方便多次复用. 组件封装代码: <template> <el-table :data=&qu ...

  10. R语言 table()函数

    table函数 用 table() 函数统计因子各水平的出现次数(称为频数或频率).也可以对一般的向量统计每个不同元素的出现次数.如 sex = c("女","女&quo ...