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\)记录的剪枝的,只能保证每个点只进队一次,并不能做到其他的减少时间,所以理论上是过不了 ...
随机推荐
- CSS margin collapsing All In One
CSS margin collapsing All In One margin collapsing margin 塌陷 / margin 合并 会出现 margin collapsing 的 3 种 ...
- how to make one you own free online tutorials in minutes
how to make one you own free online tutorials in minutes educative.io https://www.educative.io/colle ...
- Frameworkless Movement
Frameworkless Movement 无框架运动 https://www.frameworklessmovement.org/ vanilla javascript https://githu ...
- Flutter 可选择的Text
Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, ...
- NGK公链有发展前景吗?
最近网络中经常能看到一个新公链项目NGK的消息,很多朋友也都私下表示过,非常看好今年的NGK.对此,小编对NGK做了一些功课,发觉到NGK未来在商业Dapp应用的发展前景,下面就给大家分享一下我的理解 ...
- uniapp 自定义弹窗组件
先上效果: 组件源码:slot-modal.vue <template> <view class="modal-container" v-if="sho ...
- Egg.js 是什么?
Egg.js 是什么? 阿里巴巴出 Egg.js 为企业级框架和应用而生,我们希望由 Egg.js 孕育出更多上层框架,帮助开发团队和开发人员降低开发和维护成本. 注:Egg.js 缩写为 Egg 设 ...
- 深入理解Linux TCP backlog
本文转载自深入理解Linux TCP backlog 当应用程序调用listen系统调用让一个socket进入LISTEN状态时,需要指定一个参数:backlog.这个参数经常被描述为,新连接队列的长 ...
- MapReduce原理及简单实现
MapReduce是Google在2004年发表的论文<MapReduce: Simplified Data Processing on Large Clusters>中提出的一个用于分布 ...
- Oracle RMAN scripts to delete archivelog
vi del_arch.shexport ORACLE_SID=pdcsdbrman target / cmdfile=/home/oracle/scripts/del_arch.sql log=/h ...