社区讨论
求助 2-SAT 模板,80pts
P4782【模板】2-SAT参与者 3已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @lo7zfuz7
- 此快照首次捕获于
- 2023/10/27 10:16 2 年前
- 此快照最后确认于
- 2023/10/27 10:16 2 年前
调不出错QAQ。
2-SAT 评测记录(WA #8,RE #10)
然后我下载了第八个样例点,本地跑的是
POSSIBLE,而洛谷 IDE 不能支持这么大的输入,所以我不能在洛谷 IDE 测。于是我去写了满汉全席,直接在此题代码上更改的,A了。满汉全席评测记录
代码:
CPP#include<bits/stdc++.h>
using namespace std;
const int N=2e6+5;
int n,m;
int head[N],tot;
int dfn[N],dn;
int low[N];
int stk[N],top;
int col[N],cn;//和scc数组同理
int ansi[N];
bool inst[N];
struct node{
int nex,to;
}edge[N];
void add(int x,int y){
edge[++tot].nex=head[x];
edge[tot].to=y;
head[x]=tot;
}
void tarjan(int x){
dfn[x]=low[x]=++dn;
stk[++top]=x;
inst[x]=true;
for(int i=head[x];i;i=edge[i].nex){
int v=edge[i].to;
if(dfn[v]==0){
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if(inst[v]==true)
low[x]=min(low[x],dfn[v]);
}
if(dfn[x]==low[x]){
cn++;
while(stk[top]!=x){
int now=stk[top--];
col[now]=cn;
inst[now]=false;
}
int now=stk[top--];
col[now]=cn;
inst[now]=false;
}
}
int main(){
// freopen("P4782_8.in","r",stdin);
// freopen("my.out","w",stdout);
ios::sync_with_stdio(false);
cin>>n>>m;
//值为0则编号为1~n
//值为1则编号为n+1~2n
for(int i=1;i<=m;i++){
int x,tx,y,ty;
cin>>x>>tx>>y>>ty;
int fx=x,fy=y;
if(tx==0) fx+=n;
else x+=n;
if(ty==0) fy+=n;
else y+=n;
// if(x==y) add(x,x);
add(fx,y);add(fy,x);
}
for(int i=1;i<=n*2;i++)
if(dfn[i]==0) tarjan(i);
for(int i=1;i<=n;i++){
if(col[i]==col[i+n]){
cout<<"IMPOSSIBLE";
return 0;
}
if(col[i]<col[i+n]) ansi[i]=0;
else ansi[i]=1;
}
cout<<"POSSIBLE\n";
for(int i=1;i<=n;i++) cout<<ansi[i]<<" ";
return 0;
}
回复
共 5 条回复,欢迎继续交流。
正在加载回复...