题解

非常有意思的\(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. js script all in one

    js script all in one 你不知道的 js secret https://html.spec.whatwg.org/multipage/scripting.html https://h ...

  2. OLAP

    OLAP Online Analytical Processing https://en.wikipedia.org/wiki/Online_analytical_processing 在线分析处理 ...

  3. 「NGK每日快讯」12.21日NGK第48期官方快讯!

  4. 【PY从0到1】 一文掌握Pandas量化基础

    # 2[PY从0到1] 一文掌握Pandas量化基础 # Numpy和pandas是什么关系呢? # 在我看来,np偏向于数据细节处理,pd更偏向于表格整体的处理. # 要记住的pd内部的数据结构采用 ...

  5. ASM的基础用法

    本文转载自ASM的基础用法 导语 新闻里使用的热补丁修复方案是基于AspectJ,AspectJ是AOP的一种实现. 无意接触到一种小巧轻便的Java字节码操控框架ASM,它也能方便地生成和改造Jav ...

  6. 三种远程部署war包检测

    简介 远程部署漏洞属于服务器.中间件配置问题,攻击者可通过远程部署漏洞获取系统权限,远程部署漏洞经常出现在Tomcat.Jboss.Weblogic等web容器之上. 0x01 ### tomcat部 ...

  7. winform导出csv

    public void ExportToSvc1(string strFileName) { string strPath = strFileName + ".csv"; Stri ...

  8. @RestController和@Controller

    1.使用@Controller 注解,在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面 若返回json等内容到页面,则需要加@ResponseBody注解 2 ...

  9. vue之下拉菜单Dropdown的使用

    通过组件slot来设置下拉触发的元素以及需要通过具名slot为dropdown 来设置下拉菜单.默认情况下,下拉按钮只要hover即可,无需点击也会显示下拉菜单. <el-dropdown> ...

  10. Django批量插入数据和分页器

    目录 一.ajax结合sweetalert实现删除按钮动态效果 二.bulk_create批量插入数据 1. 一条一条插入 2. 批量插入 三.自定义分页器 一.ajax结合sweetalert实现删 ...