题目大意

2-SAT,其中有\(d\)(\(d\leq 8\))个点是\(3-SAT\)。

题解

枚举\(d\)个点不取三个中(假设三个为\(a,b,c\))的哪一个,然后整体变成做\(2-SAT\)。

注意枚举完不选\(a\)(即选\(b或c\))和不选\(b\)(即选\(a或c\))后,不选\(c\)(即选\(a或b\))已经包含在前两种中,因此搜索部分的时间复杂度是\(\Theta(2^d)\)的。

代码

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<set>
#include<stack>
#include<vector>
#include<queue>
#define LL long long
#define maxn 100007
#define maxm 400007
#define rep(i,x,y) for(int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(int i=(x);i>=(y);--i)
#define view(u,k) for(int k=fir[u];~k;k=nxt[k])
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
void write(int x)
{
int f=0;char ch[20];
if(x==0){putchar('0');putchar(' ');return ;}
if(x<0){putchar('-'),x=-x;}
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);putchar(' ');
}
int n,m,d,fir[maxn],nxt[maxm],v[maxm],cnte,dfn[maxn],low[maxn],ans[maxn],ins[maxn],stk[maxn],tp,tim,col[maxn],num;
void ade(int u1,int v1){v[cnte]=v1,nxt[cnte]=fir[u1],fir[u1]=cnte++;}
void tar(int u)
{
dfn[u]=low[u]=++tim,ins[u]=1,stk[++tp]=u;
view(u,k)
{
if(!dfn[v[k]])tar(v[k]),low[u]=min(low[u],low[v[k]]);
else if(ins[v[k]])low[u]=min(low[u],dfn[v[k]]);
}
if(dfn[u]==low[u])
{
num++;
while(1)
{
col[stk[tp]]=num,ins[stk[tp]]=0;
if(stk[tp--]==u)break;
}
}
}
int gx(int x,int f){return x+f*n;}
int getf(char ci,char ti)
{
if(ti=='a')return ci=='C';
else if(ti=='b')return ci=='A';
else return ci=='B';
}
void reset(){int li=n<<1;rep(i,1,li)fir[i]=-1,dfn[i]=ins[i]=0;tim=cnte=tp=0;}
char s[maxn],t[maxn],c1[maxm],c2[maxm];
int pos[10],cntp,x1[maxm],x2[maxm];
int check()
{
reset();
rep(i,1,m)
{
if(c1[i]-'A'+'a'==t[x1[i]]||(x1[i]==x2[i]&&c1[i]==c2[i]))continue;
//cout<<gx(x1[i],getf(c1[i],t[x1[i]]))<<" "<<gx(x2[i],getf(c2[i],t[x2[i]]))<<endl;
if(x1[i]==x2[i]&&c1[i]!=c2[i])
{
//cout<<"yes"<<endl;
ade(gx(x1[i],getf(c1[i],t[x1[i]])),gx(x1[i],getf(c1[i],t[x1[i]])^1));
}
else if(c2[i]-'A'+'a'==t[x2[i]])
{
ade(gx(x1[i],getf(c1[i],t[x1[i]])),gx(x1[i],getf(c1[i],t[x1[i]])^1));
}
else ade(gx(x1[i],getf(c1[i],t[x1[i]])),gx(x2[i],getf(c2[i],t[x2[i]]))),ade(gx(x2[i],getf(c2[i],t[x2[i]])^1),gx(x1[i],getf(c1[i],t[x1[i]])^1));
}int li=n<<1;
rep(i,1,li)if(!dfn[i])tar(i);
rep(i,1,n)if(col[gx(i,0)]==col[gx(i,1)])return 0;
return 1;
}
int force(int x)
{
if(x==d+1)return check();
t[pos[x]]='a';
if(force(x+1))return 1;
t[pos[x]]='b';
return force(x+1);
}
int main()
{
memset(fir,-1,sizeof(fir));
n=read(),d=read();
scanf("%s",s+1);
rep(i,1,n)if(s[i]=='x')pos[++cntp]=i;
rep(i,1,n)t[i]=s[i];
m=read();
rep(i,1,m)scanf("%d %c %d %c",&x1[i],&c1[i],&x2[i],&c2[i]);
int yes=force(1);
if(!yes)puts("-1");
else
{
rep(i,1,n){if(col[gx(i,0)]>col[gx(i,1)])ans[i]=1;}
rep(i,1,n)
{
if(t[i]=='a')putchar(ans[i]?'C':'B');
if(t[i]=='b')putchar(ans[i]?'A':'C');
if(t[i]=='c')putchar(ans[i]?'B':'A');
}
}
return 0;
}

一些感想

uoj的最后一组hack数据好毒啊

并不对劲的bzoj4945:loj2305:uoj317:p3825[NOI2017]游戏的更多相关文章

  1. P3825 [NOI2017]游戏

    题目 P3825 [NOI2017]游戏 做法 \(x\)地图外的地图好做,模型:\((x,y)\)必须同时选\(x \rightarrow y,y^\prime \rightarrow x^\pri ...

  2. [Luogu P3825] [NOI2017] 游戏 (2-SAT)

    [Luogu P3825] [NOI2017] 游戏 (2-SAT) 题面 题面较长,略 分析 看到这些约束,应该想到这是类似2-SAT的问题.但是x地图很麻烦,因为k-SAT问题在k>2的时候 ...

  3. BZOJ4945 & 洛谷3825 & UOJ317:[NOI2017]游戏——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4945 https://www.luogu.org/problemnew/show/P3825 ht ...

  4. Luogu P3825 [NOI2017]游戏

    这道题看上去NPC啊,超级不可做的样子. 我们先分析一下简单的情形:没有\(x\)地图 此时每个地图由于限制掉一种汽车,那么显然只会有两种选择. 再考虑到限制的情况,那么大致做法就很显然了--2-SA ...

  5. 洛谷P3825 [NOI2017]游戏(2-SAT)

    传送门 果然图论的题永远建图最麻烦……看着题解代码的建图过程真的很珂怕…… 先不考虑地图$x$,那么每一个地图都只能用两种赛车,于是我们可以用2-SAT来搞,用$i$表示这个地图能用的第一辆车,$i' ...

  6. 洛谷 P3825 [NOI2017]游戏 【2-SAT+状压】

    UOJ和洛谷上能A,bzoj 8ms即WA,现在也不是知道为啥--因为我太弱了 先看数据范围发现d非常小,自然想到了状压. 所以先假装都是只能跑两种车的,这显然就是个2-SAT问题了:对于x场没有hx ...

  7. LG3825/BZOJ4945/LOJ2305 「NOI2017」游戏 dfs+2-SAT

    问题描述 LG3825 BZOJ4945 LOJ2305 题解 发现对于每个地图,如果没有\(A,B,C\)地图不可以使用\(a,b,c\),就是一个\(\mathrm{3-SAT}\)问题. 有了这 ...

  8. 【BZOJ4945】[Noi2017]游戏 2-SAT

    [BZOJ4945][Noi2017]游戏 题目描述 题解:2-SAT学艺不精啊! 这题一打眼看上去是个3-SAT?哎?3-SAT不是NPC吗?哎?这题x怎么只有8个?暴力走起! 因为x要么不是A要么 ...

  9. [UOJ317]【NOI2017】游戏 题解

    题意 ​ 小 L 计划进行 \(n\) 场游戏,每场游戏使用一张地图,小 L 会选择一辆车在该地图上完成游戏. ​ 小 L 的赛车有三辆,分别用大写字母 A.B.C 表示.地图一共有四种,分别用小写字 ...

随机推荐

  1. hashcode(),equal()方法经典分析

    首先,想要明白hashCode的作用,必须要先知道Java中的集合. 总的来说,Java中的集合(Collection)有两类,一类是List,再有一类是Set. 前者集合内的元素是有序的,元素可以重 ...

  2. Apache搭建简单的图片访问服务器

    安装apache后,修改httpd.conf文件 将根目录修改为你图片所在目录 DocumentRoot有这么一行,修改成你要指向的路径 DocumentRoot "/yang/pic&qu ...

  3. javax.el.PropertyNotFoundException: Property 'id' not found on type java.lang.String 可长点心吧

    在网上搜了好多帖子都说<c:forEach items="${list }" var="stu">标签list没有加${}: 可我的问题不是这个,而 ...

  4. ./与sh区别

    1   ./需要执行权限,使用脚本文件中第一行#!指定的shell(解释器)来执行命令(譬如常见的/bin/bash),不指定系统会调用默认shell程序 2   sh不需要执行权限,是使用sh这个s ...

  5. arcgis python desc.dataType

    desc = arcpy.Describe(r"C:\Users\dell\Documents\ArcGIS\ddd.shp") 是ShapeFile desc = arcpy.D ...

  6. activemq备忘

    ActiveMQ队列消息积压问题调研 http://blog.51cto.com/winters1224/2049432ActiveMQ的插件开发介绍 https://blog.csdn.net/zh ...

  7. Redis调试 Centos

    https://zhuanlan.zhihu.com/p/67205845 https://www.cxc233.com/blog/e1d54234.html

  8. handler方法

    post(Runnable) postAtTime(Runnable,long) postDelayed(Runnable long) post类方法允许你排列一个Runnable对象到主线程队列中 ...

  9. DPDK 网络加速在 NFV 中的应用

    目录 文章目录 目录 前文列表 传统内核协议栈的数据转发性能瓶颈是什么? DPDK DPDK 基本技术 DPDK 架构 DPDK 核心组件 应用 NUMA 亲和性技术减少跨 NUMA 内存访问 应用 ...

  10. oc 执行shell 脚本

    -(id) InvokingShellScriptAtPath :(NSString*) shellScriptPath { NSTask *shellTask = [[NSTask alloc]in ...