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\)记录的剪枝的,只能保证每个点只进队一次,并不能做到其他的减少时间,所以理论上是过不了 ...
随机推荐
- Versatile Python 3.x
Versatile Python 3.x TryPython Python 3.8.0 (default, Nov 14 2019, 22:29:45) [GCC 5.4.0 20160609] on ...
- React Native Apps
React Native Apps https://github.com/ReactNativeNews/React-Native-Apps github app https://github.com ...
- Node.js & LTS
Node.js & LTS 2020 https://nodejs.org/en/about/releases/ https://raw.githubusercontent.com/nodej ...
- NGK全网算力总量增加,SPC前景广阔
日前,根据NGK官方公布的数据显示,NGK全网算力总量达到550.96万,比早前预估的500万算力增加了50.96万,并且随着SPC空投的持续,还有不少生态建设者在持续涌入NGK算力市场.那么,是什么 ...
- Renice INC:法国葡萄酒为什么独占世界鳌头?
提起葡萄酒,许多人首先想到的就是法国.法国有着悠久的酿酒历史和精湛工艺,"82年的拉菲"几乎成了大众认识葡萄酒的代名词.市面上的进口葡萄酒琳琅满目,原产国众多,意大利.西班牙.美国 ...
- 「NGK每日快讯」12.15日NGK公链第42期官方快讯!
- 通过setMouseTracking实现用鼠标拖动控件
1 import sys 2 from PyQt5.Qt import * 3 4 class Mwindow(QWidget): 5 leftclick = False 6 7 def __init ...
- Python3网络爬虫-- 使用代理,轮换使用各种IP访问
# proxy_list 代理列表 run_times = 100000 for i in range(run_times): for item in proxy_list: proxies = { ...
- RabbitMq手动确认时的重试机制
本文转载自RabbitMq手动确认时的重试机制 消息手动确认模式的几点说明 监听的方法内部必须使用channel进行消息确认,包括消费成功或消费失败 如果不手动确认,也不抛出异常,消息不会自动重新推送 ...
- 详解Go语言调度循环源码实现
转载请声明出处哦~,本篇文章发布于luozhiyun的博客: https://www.luozhiyun.com/archives/448 本文使用的go的源码15.7 概述 提到"调度&q ...