2-sat模版(转自acm再见)
2017-10-18 21:25
267 查看
//2-sat模版 const int maxn=10005*3; int n,m; int a[maxn],b[maxn]; struct note { int to; int nxt; }edge[maxn*2]; int head[maxn]; int ip; int dfn[maxn],low[maxn],sccno[maxn],cnt,scc,instack[maxn]; stack<int> stk; void init() { memset(head,-1,sizeof(head)); ip=1; } void addedge(int u,int v) { edge[ip].to=v,edge[ip].nxt=head[u],head[u]=ip++; } // x = xval or y = yval //void add_cluse(int x,int xval,int y,int yval) //{ // x=x*2+xval; // y=y*2+yval; // addedge(x,y^1); // addedge(y,x^1); //} void dfs(int u) { dfn[u]=low[u]=++scc; stk.push(u); instack[u]=1; for (int i=head[u]; i!=-1; i=edge[i].nxt) { int v=edge[i].to; if (!dfn[v]) { dfs(v); low[u]=min(low[u],low[v]); } else if (instack[v]) low[u]=min(low[u],dfn[v]); } if (low[u]==dfn[u]) { cnt++; int x; do { x=stk.top(); stk.pop(); sccno[x]=cnt; instack[x]=0; }while (x!=u); } } bool solve() { scc=cnt=0; memset(sccno,0,sizeof(sccno)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(instack,0,sizeof(instack)); while (!stk.empty()) stk.pop(); for (int i=0; i<2*n; i++) if (!dfn[i]) dfs(i); for (int i=0; i<2*n; i+=2) { if (sccno[i]==sccno[i^1]) return false; } return true; }
相关文章推荐
- 再见了acm
- ACM模版库制作V3
- poj 3683 Priest John's Busiest Day (2-sat 模版)
- 2012年ACM长春现场赛B题 2-sat
- 再见了,ACM
- ACM模版-f_zyj v 2.0——更新通知
- 2-sat模版
- 再见了acm
- [杂/退役了]ACM生涯再见
- ACM->2-sat ZOJ 3656 Bit Magic
- HDU 3062 Party(2-SAT模版题)
- [ACM]数论模版
- ACM大数模版
- 再见了我热爱的ACM赛场
- ZOJ 3656 Bit Magic 第37届ACM/ICPC长春赛区现场赛B题 (2-SAT)
- ACM->2-sat
- acm在线模版(链接)
- 个人acm模版
- 【总结】2013年,再见ACM/ICPC
- TwoSAT模版(LRJ)