Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) E. National Property(2-sat)
1 second
512 megabytes
standard input
standard output
You all know that the Library of Bookland is the largest library in the world. There are dozens of thousands of books in the library.
Some long and uninteresting story was removed...
The alphabet of Bookland is so large that its letters are denoted by positive integers. Each letter can be small or large, the large version of a letter x is denoted by x'. BSCII encoding, which is used everywhere in Bookland, is made in that way so that large letters are presented in the order of the numbers they are denoted by, and small letters are presented in the order of the numbers they are denoted by, but all large letters are before all small letters. For example, the following conditions hold: 2 < 3, 2' < 3', 3' < 2.
A word x1, x2, ..., xa is not lexicographically greater than y1, y2, ..., yb if one of the two following conditions holds:
- a ≤ b and x1 = y1, ..., xa = ya, i.e. the first word is the prefix of the second word;
 - there is a position 1 ≤ j ≤ min(a, b), such that x1 = y1, ..., xj - 1 = yj - 1 and xj < yj, i.e. at the first position where the words differ the first word has a smaller letter than the second word has.
 
For example, the word "3' 7 5" is before the word "2 4' 6" in lexicographical order. It is said that sequence of words is in lexicographical order if each word is not lexicographically greater than the next word in the sequence.
Denis has a sequence of words consisting of small letters only. He wants to change some letters to large (let's call this process a capitalization) in such a way that the sequence of words is in lexicographical order. However, he soon realized that for some reason he can't change a single letter in a single word. He only can choose a letter and change all of its occurrences in all words to large letters. He can perform this operation any number of times with arbitrary letters of Bookland's alphabet.
Help Denis to choose which letters he needs to capitalize (make large) in order to make the sequence of words lexicographically ordered, or determine that it is impossible.
Note that some words can be equal.
The first line contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of words and the number of letters in Bookland's alphabet, respectively. The letters of Bookland's alphabet are denoted by integers from 1 to m.
Each of the next n lines contains a description of one word in format li, si, 1, si, 2, ..., si, li (1 ≤ li ≤ 100 000, 1 ≤ si, j ≤ m), where li is the length of the word, and si, j is the sequence of letters in the word. The words are given in the order Denis has them in the sequence.
It is guaranteed that the total length of all words is not greater than 100 000.
In the first line print "Yes" (without quotes), if it is possible to capitalize some set of letters in such a way that the sequence of words becomes lexicographically ordered. Otherwise, print "No" (without quotes).
If the required is possible, in the second line print k — the number of letters Denis has to capitalize (make large), and in the third line print kdistinct integers — these letters. Note that you don't need to minimize the value k.
You can print the letters in any order. If there are multiple answers, print any of them.
4 3
1 2
1 1
3 1 3 2
2 1 1
Yes
2
2 3
6 5
2 1 2
2 1 2
3 1 2 3
2 1 5
2 4 4
2 4 4
Yes
0
4 3
4 3 2 2 1
3 1 1 3
3 2 3 3
2 3 1
No
In the first example after Denis makes letters 2 and 3 large, the sequence looks like the following:
- 2'
 - 1
 - 1 3' 2'
 - 1 1
 
The condition 2' < 1 holds, so the first word is not lexicographically larger than the second word. The second word is the prefix of the third word, so the are in lexicographical order. As the first letters of the third and the fourth words are the same, and 3' < 1, then the third word is not lexicographically larger than the fourth word.
In the second example the words are in lexicographical order from the beginning, so Denis can do nothing.
【题意】对于一个数字如3,那么3'<3,且加了'的数均小于没有加'的数,对于两个加了'的数,按照没有'时的规则比较大小,问存不存在一种修改方案将给定的一系列打次化为字典序递增的。
【分析】2-sat,详细见代码。建图请看这里。
以下为要求输出字典序最小的模板。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 2e5+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
typedef pair<ll,int>P;
vector<int>vec[N],ans;
struct Edge {
int to,next;
} edge[M];
int head[M],tot;
void init() {
tot = ;
memset(head,-,sizeof(head));
}
void addedge(int u,int v) {
//printf("!!!%d %d\n",u,v);
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
bool vis[N];//染色标记,为true表示选择
int S[N],top;//栈
bool dfs(int u) {
if(vis[u^])return false;
if(vis[u])return true;
vis[u] = true;
S[top++] = u;
for(int i = head[u]; i != -; i = edge[i].next)
if(!dfs(edge[i].to))
return false;
return true;
}
bool Twosat(int n) {
memset(vis,false,sizeof(vis));
for(int i = ; i < n; i += ) {
if(vis[i] || vis[i^])continue;
top = ;
if(!dfs(i)) {
while(top)vis[S[--top]] = false;
if(!dfs(i^)) return false;
}
}
return true;
}
int main() {
int n,m;
int u,v;
scanf("%d%d",&n,&m);
init();
for(int i=,s,x;i<=n;i++){
scanf("%d",&s);
while(s--){
scanf("%d",&x);
vec[i].pb(x);
}
if(i==)continue;
int len=min(vec[i].size(),vec[i-].size());
int pos=-;
for(int j=;j<len;j++){
if(vec[i][j]!=vec[i-][j]){
pos=j;break;
}
}
if(pos==-){
if(vec[i-].size()>vec[i].size()){
return puts("No")*;
}
}
else {
int a=vec[i-][pos],b=vec[i][pos];
if(a>b){
addedge(*a-,*a-);
addedge(*b-,*b-);
}
else {
addedge(*a-,*b-);
addedge(*b-,*a-);
}
}
}
if(Twosat(*m)) {
puts("Yes");
for(int i = ; i < *m; i+=)
if(vis[i])
ans.pb((i)/+);
printf("%d\n",ans.size());
for(int x : ans)printf("%d ",x);
} else printf("No\n");
return ;
}
输出任意一组解。
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn=;
const int maxm=; struct qlt
{
int n,num1;
int b[maxn],next[maxm],a[maxm];
int dfn[maxn],low[maxn];
int stack[maxn],belong[maxn];
bool instack[maxn];
int num[maxn],topo[maxn];
int sum2;
int scc,top,index;
vector<int> v[maxn];
void init(int nn)
{
n=nn;num1=;
memset(b,,sizeof(b));
}
void addedge(int x,int y)
{
++num1;a[num1]=y;next[num1]=b[x];b[x]=num1;
}
void tarjan(int x)
{
int y;
low[x]=dfn[x]=++index;
stack[top++]=x;
instack[x]=true;
for (int i=b[x];i>;i=next[i])
{
y=a[i];
if (!dfn[y])
{
tarjan(y);
if (low[x]>low[y]) low[x]=low[y];
}
else if (instack[y]&&low[x]>dfn[y])
low[x]=dfn[y];
}
if (low[x]==dfn[x])
{
scc++;
do
{
y=stack[--top];
instack[y]=false;
belong[y]=scc;
v[scc].push_back(y);
sum2++;
topo[y]=sum2;
num[scc]++;
}
while (y!=x);
}
}
void solve()
{
memset(dfn,,sizeof(dfn));
memset(instack,false,sizeof(instack));
memset(num,,sizeof(num));
index=scc=top=sum2=;
for (int i=;i<n;++i)
if (!dfn[i]) tarjan(i);
}
}mp; int n,m,nn,x;
vector<int> v[maxn],vv; int main()
{
scanf("%d%d",&n,&m);
mp.init(*m);
for (int i=;i<=n;++i)
{
scanf("%d",&nn);
while (nn--)
{
scanf("%d",&x);
v[i].push_back(x);
}
if (i==) continue;
int len=min(v[i-].size(),v[i].size());
int pos=-;
for (int j=;j<len;++j)
{
if (v[i][j]!=v[i-][j])
{
pos=j;
break;
}
}
if (pos==-)
{
if (v[i].size()<v[i-].size())
{
printf("No\n");
return ;
}
}
else
{
int a=v[i-][pos],b=v[i][pos];
//if (a==b) continue;
a--;b--;
if (a>b)
{
mp.addedge(*a+,*a);
mp.addedge(*b,*b+);
}
else
{
mp.addedge(*a+,*b+);
mp.addedge(*b,*a);
}
}
}
mp.solve();
for (int i=;i<m;++i)
{
if (mp.belong[*i]==mp.belong[*i+])
{
printf("No\n");
return ;
}
}
vv.clear();
for (int i=;i<m;++i) if (mp.topo[*i]<mp.topo[*i+]) vv.push_back(i+);
printf("Yes\n");
printf("%d\n",(int)vv.size());
for (int i=;i<vv.size();++i) printf("%d ",vv[i]);
return ;
}
Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) E. National Property(2-sat)的更多相关文章
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) F. High Cry(思维 统计)
		
F. High Cry time limit per test 1 second memory limit per test 512 megabytes input standard input ou ...
 - Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)  D. Sorting the Coins
		
http://codeforces.com/contest/876/problem/D 题意: 最开始有一串全部由"O"组成的字符串,现在给出n个数字,指的是每次把位置n上的&qu ...
 - Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)  C. Classroom Watch
		
http://codeforces.com/contest/876/problem/C 题意: 现在有一个数n,它是由一个数x加上x每一位的数字得到的,现在给出n,要求找出符合条件的每一个x. 思路: ...
 - Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) B. Divisiblity of Differences
		
http://codeforces.com/contest/876/problem/B 题意: 给出n个数,要求从里面选出k个数使得这k个数中任意两个的差能够被m整除,若不能则输出no. 思路: 差能 ...
 - Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) A. Trip For Meal
		
http://codeforces.com/contest/876/problem/A 题意: 一个人一天要吃n次蜂蜜,他有3个朋友,他第一次总是在一个固定的朋友家吃蜂蜜,如果说没有吃到n次,那么他就 ...
 - Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)
		
A. Trip For Meal 题目链接:http://codeforces.com/contest/876/problem/A 题目意思:现在三个点1,2,3,1-2的路程是a,1-3的路程是b, ...
 - ACM-ICPC (10/16) Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)
		
A. Trip For Meal Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. ...
 - Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth(重识搜索)
		
https://codeforces.com/contest/1064/problem/D 题意 给你一个有障碍的图,限制你向左向右走的次数,问你可以到达格子的个数 思路 可以定义状态为vi[x][y ...
 - Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) D. Labyrinth
		
http://codeforces.com/contest/1064/problem/D 向上/向下加0,向左/右加1, step = 0,1,…… 求的是最少的步数,所以使用bfs. step=k ...
 
随机推荐
- Python json转字符串的一些细节
			
要调PHP那边的接口,php那边一直都校验不过,很是郁闷.没办法,只能让人把发送成功的代码拿过来看,不过是php写的,步骤都是一样: php端: 1. json对象转json字符串. 2. 对json ...
 - 【leetcode 简单】 第八十二题 反转字符串
			
编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man, a p ...
 - 图文解说视频直播原理-zz
			
本文主要介绍rtmp&hls视频直播原理,文章最早发表在我们的微信公众号上,详见这里,欢迎关注微信公众号blackerteam,更多详见www.blackerteam.com 现在视频直播很火 ...
 - JavaScript 金额、数字、千分位、千分位、保留几位小数、舍入舍去、支持负数
			
JavaScript 金额.数字 千分位格式化.保留指定位数小数.支持四舍五入.进一法.去尾法 字段说明: number:需要处理的数字: decimals:保留几位小数,默认两位,可不传: dec_ ...
 - Pytorch自定义数据库
			
1)前言 虽然torchvision.datasets中已经封装了好多通用的数据集,但是我们在使用Pytorch做深度学习任务的时候,会面临着自定义数据库来满足自己的任务需要.如我们要训练一个人脸关键 ...
 - RabbitMQ--Publish/Subscribe(三)
			
前言 在先前例子中我们创建了一个work queue,封装一个task到一个message中并发送到queue.一个worker(consumer)取出任务并执行. 像这种producer把messa ...
 - Python模块:Random(未完待续)
			
本文基于Python 3.6.5的官文random编写. random模块简介 random为各种数学分布算法(distributions)实现了伪随机数生成器. 对于整数,是从一个范围中均匀选择(u ...
 - MyEclipse中点击Deploy MyEclipse J2EE Project to Server无响应解决方法
			
问题: MyEclipse中点击Deploy MyEclipse J2EE Project to Server无响应 解决方法: 如果工作空间的问题,那么需要删除你工作空间的一个文件就可以解决了.这个 ...
 - 洛谷P1342请柬
			
传送门啦 核心思想:两遍最短路. 1号点去各地的时间直接套最短路模板,各地到1号点时间用逆向思维,视为求1号点沿反边到各地的时间即可. #include <iostream> #inclu ...
 - nio复习总结
			
观察者: 多个对象依赖一个对象的状态, 当这个对象状态发生改变时,依次通知多个对象. 消息的分发和处理 事件驱动 / IO多路复用 借助select epoll等 reactor: io事件触发时, ...