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 ...
随机推荐
- dubbo的dispatcher设置原理
在上回<Dubbo源代码实现六>中我们已经了解到,对于Dubbo集群中的Provider角色,有IO线程池(默认无界)和业务处理线程池(默认200)两个线程池,所以当业务的并发比较高,或者 ...
- 折腾前端条形码(Barcode)扫描识别, 笔记
barcode @zxing/library 方案 本地勉强把 Demo 在 React 里面跑通, 但是不好控制开始结束, API 不明确.实际识别率很低. 我是用手机屏幕放的条形码, 大概也有影响 ...
- Vacuum Pump Manufacturer Introduction: Airless Pump Bottle
Fillable vacuum pump bottle with matt silver aluminum base and cap and shiny silver aluminum collar. ...
- selenium webdriver 等待元素
/**显示等待并返回元素 * @param driver * @param locator */ public static WebElement showWait(WebDriver driver, ...
- SpringData学习笔记一
Spring Data : 介绍: Spring 的一个子项目.用于简化数据库访问,支持NoSQL 和 关系数据存储.其主要目标是使数据库的访问变得方便快捷. SpringData 项目所支持 NoS ...
- Activity切换动画。从右边滑入,关闭时从左边滑入
直接贴代码吧 1. 动画文件(两个动画文件配置到res/anim目录下) activity_anim_in_right.xml <?xml version="1.0" e ...
- Android音频捕获(录音)(转)
原文:http://www.yiibai.com/android/android_audio_capture.html Android有一个内置的麦克风,通过它可以捕获音频和存储,或在手机进行播放.有 ...
- C语言中的结构体是怎么定义的_怎么使用?
结构体的定义 // 定义结构体st struct st{ int a; // 成员a int b; // 成员b }; #include <stdio.h> struct st{ int ...
- 题解 CF165D 【Beard Graph】
思路:将黑边标记为1,白边标记为100000,树链剖分 如果查询时ans超过100000,那就有白边,输出-1,不然直接输出ans #include<bits/stdc++.h> #def ...
- iframe切换
iframe(locator有三种情况,可以是:定位表达式(driver.find_element_by_xpath()),frame的名称,下标index) driver.switch_to.fra ...