2-SAT[模板]
发布日期:2021-06-30 10:20:38
浏览次数:3
分类:技术文章
本文共 2159 字,大约阅读时间需要 7 分钟。
互 斥 的 点 加 一 条 边 互斥的点加一条边 互斥的点加一条边
缩 点 后 , 保 证 互 的 点 不 在 一 个 强 连 通 内 缩点后,保证互的点不在一个强连通内 缩点后,保证互的点不在一个强连通内
就 可 以 拓 扑 排 序 就可以拓扑排序 就可以拓扑排序
s c c 编 号 小 的 拓 扑 序 号 反 而 大 ( 也 就 是 先 进 行 拓 扑 ) scc编号小的拓扑序号反而大(也就是先进行拓扑) scc编号小的拓扑序号反而大(也就是先进行拓扑)
我 们 需 要 的 就 是 拓 扑 号 大 的 , 这 样 对 后 续 影 响 小 我们需要的就是拓扑号大的,这样对后续影响小 我们需要的就是拓扑号大的,这样对后续影响小
#includeusing namespace std;const int maxn=4e6+10;int n,m;struct edge{ int to,nxt;}d[maxn]; int head[maxn],cnt;void add(int u,int v){ d[++cnt]=(edge){v,head[u]},head[u]=cnt;}int low[maxn],dfn[maxn],vis[maxn],scc[maxn],stac[maxn],id,top,scc_num;/*void tarjan(int u,int fa){ dfn[u]=low[u]=++id; stac[++top]=u,vis[u]=1; for(int i=head[u];i;i=d[i].nxt ) { int v=d[i].to; if( v==fa ) continue; if( !dfn[v] ) tarjan(v,u), low[u]=min( low[u],low[v] ); else if( vis[v] ) low[u]=min( low[u],dfn[v] ); } if( dfn[u]==low[u] ) { scc_num++; while( stac[top]!=u ) { scc[ stac[top] ]=scc_num; vis[ stac[top] ]=0; top--; } scc[ stac[top] ]=scc_num; vis[ stac[top] ]=0; top--; }}*/void tarjan(int u,int fa){ dfn[u]=low[u]=++id; stac[++top]=u; vis[u]=1; for(int i=head[u];i;i=d[i].nxt) { int v=d[i].to; if(v==fa) continue; if(!dfn[v]) { tarjan(v,u); low[u]=min(low[u],low[v]); } else if(vis[v]) low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]) { scc_num++; while(stac[top]!=u) { scc[stac[top]]=scc_num; vis[stac[top]]=0; top--; } scc[stac[top]]=scc_num; vis[stac[top]]=0; top--; }}int main(){ ios::sync_with_stdio(false); cin >> n >> m; for(int i=1;i<=m;i++) { int a,x,b,y;//a+n表示1 cin >> a >> x >> b >> y; if( x==0&&y==0 ) add(a+n,b),add(b+n,a); if( x==0&&y==1 ) add(a+n,b+n),add(b,a); if( x==1&&y==0 ) add(a,b),add(b+n,a+n); if( x==1&&y==1 ) add(a,b+n),add(b,a+n); } for(int i=1;i<=2*n;i++) if( !dfn[i] ) tarjan(i,0); for(int i=1;i<=n;i++) if( scc[i]==scc[i+n] ) { cout << "IMPOSSIBLE\n"; return 0; } cout << "POSSIBLE\n"; for(int i=1;i<=n;i++) { if( scc[i]>scc[i+n] ) cout << 1 << " ";//强连通编号小->拓扑序越大(比较容易拓扑出来) else cout << 0 << " "; }}
转载地址:https://issue-is-vegetable.blog.csdn.net/article/details/108217945 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月10日 23时28分35秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
CodeForces 248B - Chilly Willy - 找规律
2019-04-30
POJ-2418 Hardwood Species(Trie树)(map)
2019-04-30
HDU-4300 Clairewd’s message + 4333(扩展KMP)
2019-04-30
HDU 1592 Half of and a Half(高精度)
2019-04-30
POJ-3304 Segments(计算几何)
2019-04-30
UVA-11538 Chess Queen(数学)
2019-04-30
UVA-11401 Triangle Counting(数学优化)
2019-04-30
Codeforces Round #369 (Div. 2)
2019-04-30
UVA 11426 GCD - Extreme (II)(欧拉函数)
2019-04-30
HDU-2838 Cow Sorting(树状数组)
2019-04-30
POJ-2299 Ultra-QuickSort(树状数组)(离散化)
2019-04-30
基于SSM的兼职论坛系统的设计与实现
2019-04-30
基于java的图书管理系统的设计与实现
2019-04-30
基于java的SSM框架理财管理系统的设计与实现
2019-04-30
基于java的ssm框架就业信息管理系统的设计
2019-04-30
基于java的ssm框架的旅游网站设计与实现
2019-04-30
基于java的SSM框架的流浪猫救助网站的设计与实现
2019-04-30
基于java的SSM框架的教务关系系统的设计与实现
2019-04-30