CodeForces - 876E National Property(2-sat)
题意:有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)的更多相关文章
- Codeforces 875C National Property(拓扑排序)
题目链接 National Property 给定n个单词,字符集为m 现在我们可以把其中某些字母变成大写的.大写字母字典序大于小写字母. 问是否存在一种方案使得给定的n个单词字典序不下降. 首先判 ...
- Codeforces 876E National Property ——(2-SAT)
在这题上不是标准的“a或b”这样的语句,因此需要进行一些转化来进行建边.同时在这题上点数较多,用lrj大白书上的做法会T,因此采用求强连通分量的方法来求解(对一个点,如果其拓扑序大于其为真的那个点,则 ...
- Codeforces 828B Black Square(简单题)
Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...
- 1114 Family Property (25 分)
1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...
- Codeforces C. Maximum Value(枚举二分)
题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【刷题-PAT】A1114 Family Property (25 分)
1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...
- 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 ...
- CodeForces 215B Olympic Medal(数学啊)
题目链接:http://codeforces.com/problemset/problem/215/B Description The World Programming Olympics Medal ...
- CodeForces 993B Open Communication(STL 模拟)
https://codeforces.com/problemset/problem/993/b 这题不难,暴力就能过,主要是题意太难懂了 题意: 现在有两个人,每个人手中有一对数,第一个人手中的数是n ...
随机推荐
- Dart语言学习(十五) Dart函数方法
Dart函数方法可分为两类: 一.内置方法/函数: print(); 二.自定义方法: 自定义方法的基本格式: 返回类型 方法名称(参数1,参数2,...){ 方法体 return 返回值; } vo ...
- 【网寻】mui - 点击事件
//本来是需要这样的,结果点击无响应 $(".mui-control-item").on("click",function(event){ alert(1) } ...
- 珠心算测验(0)<P2014_1>
珠心算测验 (count.cpp/c/pas) 问题描述] 珠心算是一种通过在脑中模拟算盘变化来完成快速运算的一种计算技术.珠心算训练,既能够开发智力,又能够为日常生活带来很多便利,因而在很多学校得 ...
- Hibernate框架报错:org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.mikey.hibernate.domain.Person.pid
报错信息 org nate.PropertyAccessException:IllegalArgumentException在调用com.mikey.Hibernate.domain.Person.p ...
- mysql操作之密码的那点小事
mysql 修改密码的2种方式: 进入mysql库的user表中修改 update mysql.user password = password("新密码") where 条件; ...
- 十八 OGNL特殊符号的作用,#,%,$
主要有哪些字符? #:获取Context的数据,构建map %: 强制解析OGNL,强制不解析OGNL $ : 在配置文件中(xml,属性文件(国际化))使用OGNL #的用法: <body&g ...
- Java基础 -1.4
标识符与关键字 对于标识符的组成在Java之中的定义如下:由字母.数字._.$ 组成 其中不能使用Java的保留字(关键字) 其中 $ 一般都有特殊的含义 不建议出现在自己所编写的代码上 关键字 是系 ...
- 布线问题&魔法花园_最短路径
布线问题 问题描述:印刷电路板将布线区域划分成n×m个方格阵列,精确的电路布线问题要求确定连接方格a到方格b的最短布线方案:布线时,电路只能沿着直线或直角(方格)布线:已经布线的方格被锁定,即不允许其 ...
- Redis 事务在 SpringBoot 中的应用 (io.lettuce.core.RedisCommandExecutionException: ERR EXEC without MULTI)
我们在 SpringBoot 中使用 Redis 时,会引入如下的 redis starter <dependency> <groupId>org.springframewor ...
- 如何申请自己的Flag Counter?
申请自己的Flag Counter是一件简单的事. 首先,进入Flag Counter主页. 之后,选择好样式,点击"GET YOUR FLAG COUNTER".会弹出一个对话窗 ...