HDU 1824 Let's go home(2-SAT)
2017-02-26 19:40
344 查看
2-sat问题
思路来自:http://blog.csdn.net/u013480600/article/details/36397729
思路来自:http://blog.csdn.net/u013480600/article/details/36397729
#include<iostream> #include<cstring> #include<vector> #include<stack> #define Min(a,b) a<b?a:b using namespace std; const int maxn = 6001*2; vector<int> g[maxn]; stack<int> st; int dfn[maxn],instack[maxn],low[maxn],scc[maxn]; int time,sccn; void tarjin(int u) { dfn[u]=low[u]=++time; instack[u]=1; st.push(u); for(int i=0;i<g[u].size();i++) { int v = g[u][i]; if(!dfn[v]) { tarjin(v); low[u] = Min(low[u],low[v]); } else if(instack[v]) low[u] = Min(low[u],low[v]); } if(low[u]==dfn[u]) { sccn++; int x; do { x=st.top();st.pop(); instack[x]=0; scc[x]=sccn; }while(x!=u); } } void init() { memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(instack,0,sizeof(instack)); memset(scc,0,sizeof(scc)); } int main() { int t,m,a,b,c,flag; while(scanf("%d%d",&t,&m)!=EOF) { for(int i=0;i<6*t;i++)g[i].clear(); init(); while(!st.empty())st.pop(); time=sccn=flag=0; for(int i=0;i<t;i++) { scanf("%d%d%d",&a,&b,&c); int u=2*a; // 队长 留 int u1=2*a+1;// 队长 回 int v=2*b; int v1=2*b+1; int x=2*c; int x1=2*c+1; g[u].push_back(v1);//留下 g[u].push_back(x1); g[v].push_back(u1); g[v].push_back(x); g[x].push_back(v); g[x].push_back(u1); g[u1].push_back(v);//离开 g[u1].push_back(x); g[v1].push_back(x1); g[v1].push_back(u); g[x1].push_back(v1); g[x1].push_back(u); } for(int i=0;i<m;i++) { scanf("%d%d",&a,&b); int u=2*a; int u1=2*a+1; int v=2*b; int v1=2*b+1; g[u].push_back(v1); g[v].push_back(u1); // g[v1].push_back(u); a和b可以都不留下 // g[u1].push_back(v); 上面两个或下面两个都行 } for(int i=0;i<6*t;i++) if(!dfn[i]) tarjin(i); for(int i=0;i<3*t;i++) { if(scc[2*i]==scc[2*i+1]) { flag=1; break; } } if(flag)printf("no\n"); else printf("yes\n"); } }
相关文章推荐
- Hdu 1824 Let's go home (2-sat 可行性判定)
- hdu 1824 Let's go home(2-sat入门)
- HDU 1824 —— Let's go home(2-SAT)
- HDU 1824 Let's go home(2-sat)
- HDU 1824 Let's go home (2-SAT)
- HDU 1824 Let's go home (2-SAT)
- hdu 1824 Let's go home【2-SAT------强连通Tarjan】
- hdu 1824 Let's go home (2-sat)
- HDU_1824_Let's go home(2-SAT)
- hdu 1824 Let's go home(s-sat)
- HDU 1824 Let's go home(2-SAT)
- HDU 1824 Let's go home (2-SAT判定)
- HDU 1824 Let's go home 2-sat判断可行解
- HDU 1824 Let's go home 2-sat基础
- HDU - 1824 Let's go home(2-SAT)
- HDU 1824 Let's go home(2-SAT)
- HDU-1824-Let's go home(2-SAT)
- hdu 1824 Let's go home (2-sate)
- hdoj--1824--Let's go home(2-sat)
- HDU 1824 Let's go home (2-SAT判定)