题解

非常有意思的\(2-SAT\)的题。

听学长讲完之后感觉确实容易想到\(2-SAT\),顺理成章。

显然,对于两个串,对咱们来说有意义的显然是两个串中第一个不同的数字。那么,我们假设两个串分别是\(A,B\),第一个不同的位置是\(pos\),且\(A_{pos}=x,B_{pos}=y\),如果\(x>y\),那么显然我们需要给\(x\)加一个\('\),且\(y\)一定不能加,要不然没有意义。所以我们把\(x+m\)向\(y\)连边,反之就反着来就好了。

然后就可以跑一遍\(2-SAT\),就像模板一样,如果\(color_i==color_{i+m}\),就可以直接输出\(No\)。然后就统计答案即可。

代码

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int tot,head[N],ver[N<<1],nxt[N<<1],a[N],lst[N];
int n,m,lstlen;
int num,dfn[N],low[N];
int color[N],cnt,st[N],top,ans;
int read()
{
int x=0,f=1;
char c=getchar();
while (c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while (c>='0'&&c<='9')
{
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
void add(int x,int y)
{
tot++;
ver[tot]=y;
nxt[tot]=head[x];
head[x]=tot;
}
void Tarjan(int x)
{
low[x]=dfn[x]=++num;
st[++top]=x;
for(int i=head[x];i;i=nxt[i])
{
int y=ver[i];
if(!dfn[y]) Tarjan(y),low[x]=min(low[x],low[y]);
else if(!color[y]) low[x]=min(low[x],dfn[y]);
}
if(dfn[x]==low[x])
{
int y=-1;
cnt++;
while(x!=y)
{
y=st[top--];
color[y]=cnt;//cout<<y<<" "<<cnt<<endl;
}
}
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
{
int len=read();
bool flag=false;
for(int j=1;j<=len;j++) a[j]=read();
for(int j=1;j<=min(lstlen,len);j++)
{
if(a[j]==lst[j]) continue;
flag=true;
if(a[j]>lst[j]) add(a[j]+m,lst[j]+m),add(lst[j],a[j]);
else add(lst[j],lst[j]+m),add(a[j]+m,a[j]);
break;
}
if(!flag && lstlen>len) puts("No"),exit(0);
lstlen=len;
for(int j=1;j<=lstlen;j++) lst[j]=a[j];
} for(int i=1;i<=(m<<1);i++) if(!dfn[i]) Tarjan(i);
for(int i=1;i<=m;i++) if(color[i]==color[i+m]) puts("No"),exit(0);
puts("Yes");
for(int i=1;i<=m;i++) if(color[i+m]<color[i]) ans++;
cout<<ans<<endl;
for(int i=1;i<=m;i++) if(color[i+m]<color[i]) cout<<i<<" ";
return 0;
}

CodeForces CF875C题解的更多相关文章

  1. codeforces#536题解

    CodeForces#536 A. Lunar New Year and Cross Counting Description: Lunar New Year is approaching, and ...

  2. codeforces 1093 题解

    12.18 update:补充了 $ F $ 题的题解 A 题: 题目保证一定有解,就可以考虑用 $ 2 $ 和 $ 3 $ 来凑出这个数 $ n $ 如果 $ n $ 是偶数,我们用 $ n / 2 ...

  3. Codeforces Numbers 题解

    这题只需要会10转P进制就行了. PS:答案需要约分,可以直接用c++自带函数__gcd(x,y). 洛谷网址 Codeforces网址 Code(C++): #include<bits/std ...

  4. Codeforces 691E题解 DP+矩阵快速幂

    题面 传送门:http://codeforces.com/problemset/problem/691/E E. Xor-sequences time limit per test3 seconds ...

  5. Codeforces 833B 题解(DP+线段树)

    题面 传送门:http://codeforces.com/problemset/problem/833/B B. The Bakery time limit per test2.5 seconds m ...

  6. Codeforces 840C 题解(DP+组合数学)

    题面 传送门:http://codeforces.com/problemset/problem/840/C C. On the Bench time limit per test2 seconds m ...

  7. Codeforces 515C 题解(贪心+数论)(思维题)

    题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...

  8. Codeforces 475D 题解(二分查找+ST表)

    题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...

  9. CodeForces CF877D题解(BFS+STL-set)

    解法\(1:\) 正常的\(bfs\)剪枝是\(\Theta(nm4k)\),这个时间复杂度是只加一个\(vis\)记录的剪枝的,只能保证每个点只进队一次,并不能做到其他的减少时间,所以理论上是过不了 ...

随机推荐

  1. auto open Chrome DevTools in the command line

    auto open Chrome DevTools in the command line --auto-open-devtools-for-tabs # macOS $ /Applications/ ...

  2. webassembly & google

    webassembly & google https://developers.google.com/web/updates/2018/08/wasm-av1 https://develope ...

  3. Redis 博文索引

    博文索引 Redis 对象与编码 Redis 持久化 Redis 主从复制 Redis 哨兵 Redis 缓存淘汰 Redis 集合统计 Redis 简介

  4. vue路由理解

    vue路由:就是一个菜单的概念比如说有一个菜单栏,菜单栏上有很多按钮,当你点击一个按钮时会出现不同的页面,这就是vue路由

  5. Vue学习笔记-Django REST framework3后端接口API学习

    一  使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7  (Windows x86- ...

  6. oracle数据库date类型和mysql数据库datetime类型匹配

    oracle数据库有date类型,但是没有datetime类型 mysql数据库既有date类型也有datetime类型. Oracle数据库的date类型和mysql的date类型是不一样的,Ora ...

  7. 按照阿里巴巴规范创建Java线程池

    前言 Executors Executors 是一个Java中的工具类.提供工厂方法来创建不同类型的线程池. 常用方法: 1.newSingleThreadExecutor   介绍:创建一个单线程的 ...

  8. [极客大挑战 2019]Secret File 1

    题目的名字就暗示我们考点文件隐藏进入页面查看源码 得到隐藏的界面点击访问 点击给的"SECRET"按钮页面出现提示"没看清么?回去再仔细看看吧.",说明响应的时 ...

  9. WEB服务-Nginx之十-keepalived

    WEB服务-Nginx之10-keepalived 目录 WEB服务-Nginx之10-keepalived Keepalived和高可用 基本概述 Keepalived安装配置 Keepalived ...

  10. phpMyAdmin Transformation 任意文件包含/远程代码执行漏洞

    漏洞参考 https://yq.aliyun.com/articles/679633 国外提供了一个在线测试的靶场     默认密码  root  toor https://www.vsplate.c ...