CodeForces CF875C题解
题解
非常有意思的\(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题解的更多相关文章
- codeforces#536题解
CodeForces#536 A. Lunar New Year and Cross Counting Description: Lunar New Year is approaching, and ...
- codeforces 1093 题解
12.18 update:补充了 $ F $ 题的题解 A 题: 题目保证一定有解,就可以考虑用 $ 2 $ 和 $ 3 $ 来凑出这个数 $ n $ 如果 $ n $ 是偶数,我们用 $ n / 2 ...
- Codeforces Numbers 题解
这题只需要会10转P进制就行了. PS:答案需要约分,可以直接用c++自带函数__gcd(x,y). 洛谷网址 Codeforces网址 Code(C++): #include<bits/std ...
- Codeforces 691E题解 DP+矩阵快速幂
题面 传送门:http://codeforces.com/problemset/problem/691/E E. Xor-sequences time limit per test3 seconds ...
- Codeforces 833B 题解(DP+线段树)
题面 传送门:http://codeforces.com/problemset/problem/833/B B. The Bakery time limit per test2.5 seconds m ...
- Codeforces 840C 题解(DP+组合数学)
题面 传送门:http://codeforces.com/problemset/problem/840/C C. On the Bench time limit per test2 seconds m ...
- Codeforces 515C 题解(贪心+数论)(思维题)
题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...
- Codeforces 475D 题解(二分查找+ST表)
题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...
- CodeForces CF877D题解(BFS+STL-set)
解法\(1:\) 正常的\(bfs\)剪枝是\(\Theta(nm4k)\),这个时间复杂度是只加一个\(vis\)记录的剪枝的,只能保证每个点只进队一次,并不能做到其他的减少时间,所以理论上是过不了 ...
随机推荐
- Axios 取消 Ajax 请求
Axios 取消 Ajax 请求 Axios XMLHttpRequest https://caniuse.com/?search=XMLHttpRequest https://developer.m ...
- Micro Frontends & microservices
Micro Frontends & microservices https://micro-frontends.org/ https://github.com/neuland/micro-fr ...
- Flutter: IntrinsicWidth类,将孩子的宽度调整为孩子的最大内在宽度
文档 原文 class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePage ...
- [转]ROS订阅激光数据
https://github.com/robopeak/rplidar_ros/blob/master/src/client.cpp /* * Copyright (c) 2014, RoboPe ...
- 单例模式有效解决过多的if-else
策略模式 引例:假如我们要分享一个篇文章.有微信分享.微博分享.QQ分享等......我们是先判断类型是哪个,然后再调用各自得API去做分享操作 一般来说,大多数人都会根据类型判断是哪个渠道吧,如下代 ...
- 通过golang小案例,了解golang程序常见机制
目录 代码理解及纠错 1.defer和panic执行先后顺序 2.for循环元素副本问题 3.slice追加元素问题 4.返回值命名问题 5.用new初始化内置类型问题 6.切片append另外一个切 ...
- 剑指 Offer 31. 栈的压入、弹出序列 + 入栈顺序和出栈顺序的匹配问题
剑指 Offer 31. 栈的压入.弹出序列 Offer_31 题目详情: 解析: 这里需要使用一个栈来模仿入栈操作. package com.walegarrett.offer; /** * @Au ...
- javascript处理HTML的Encode(转码)和解码(Decode)
HTML的Encode(转码)和解码(Decode)在平时的开发中也是经常要处理的,在这里总结了使用javascript处理HTML的Encode(转码)和解码(Decode)的常用方式 一.用浏览器 ...
- Java 集合框架 04
集合框架·Map 和 Collections集合工具类 Map集合的概述和特点 * A:Map接口概述 * 查看API可知: * 将键映射到值的对象 * 一个映射不能包含重复的键 * 每个键最多只能映 ...
- C#类中的字段、属性和方法
C#类中的字段.属性和方法 刚开始学C#,对于类中的字段.属性和方法很难分清,写下这份笔记,帮助理解 字段:与类相关的变量 声明方法与声明变量类似,可在前面添加访问修饰符.static关键字等: 属性 ...