【2-SAT】【并查集】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem
2017-02-24 12:58
627 查看
再来回顾一下2-SAT,把每个点拆点为是和非两个点,如果a能一定推出非b,则a->非b,其他情况同理。
然后跑强连通分量分解,保证a和非a不在同一个分量里面。
这题由于你建完图发现都是双向边,所以用并查集亦可。
然后跑强连通分量分解,保证a和非a不在同一个分量里面。
这题由于你建完图发现都是双向边,所以用并查集亦可。
#include<cstdio> #include<vector> #include<cstring> using namespace std; vector<int>G[200010],rG[200010],vs; bool used[200010],a[100010]; int n,m,cmp[200010],bel[100010][3]; void dfs(int U) { used[U]=1; for(int i=0;i<G[U].size();++i) if(!used[G[U][i]]) dfs(G[U][i]); vs.push_back(U); } void rdfs(int U,int k) { used[U]=1; cmp[U]=k; for(int i=0;i<rG[U].size();++i) if(!used[rG[U][i]]) rdfs(rG[U][i],k); } int main() { // freopen("d.in","r",stdin); int x,y; scanf("%d%d",&n,&m); for(int i=1;i<=n;++i) scanf("%d",&a[i]); for(int i=1;i<=m;++i) { scanf("%d",&x); for(int j=1;j<=x;++j) { scanf("%d",&y); bel[y][++bel[y][0]]=i; } } for(int i=1;i<=n;++i) if(a[i]) { G[bel[i][1]].push_back(bel[i][2]); G[bel[i][2]].push_back(bel[i][1]); G[bel[i][1]+m].push_back(bel[i][2]+m); G[bel[i][2]+m].push_back(bel[i][1]+m); rG[bel[i][2]].push_back(bel[i][1]); rG[bel[i][1]].push_back(bel[i][2]); rG[bel[i][2]+m].push_back(bel[i][1]+m); rG[bel[i][1]+m].push_back(bel[i][2]+m); } else { G[bel[i][1]].push_back(bel[i][2]+n); G[bel[i][2]].push_back(bel[i][1]+n); G[bel[i][1]+m].push_back(bel[i][2]); G[bel[i][2]+m].push_back(bel[i][1]); rG[bel[i][2]+m].push_back(bel[i][1]); rG[bel[i][1]+m].push_back(bel[i][2]); rG[bel[i][2]].push_back(bel[i][1]+m); rG[bel[i][1]].push_back(bel[i][2]+m); } for(int i=1;i<=m;++i) if(!used[i]) dfs(i); memset(used,0,sizeof(used)); int cnt=0; for(int i=vs.size()-1;i>=0;--i) if(!used[vs[i]]) rdfs(vs[i],++cnt); for(int i=1;i<=m;++i) if(cmp[i]==cmp[i+m]) { puts("NO"); return 0; } puts("YES"); return 0; }
相关文章推荐
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 2-SAT
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem(二分染色?/2-sat,好题)
- Codeforces Round #400 (Div. 1 + Div. 2, combined)D - The Door Problem(2-sat)
- Codeforces Round #400 (Div. 1 + Div. 2, combined)D - The Door Problem(2-sat)
- [Updating]ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) 解题报告
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
- 【前缀和 && 思维转换】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)Molly's Chemicals
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)
- 【解题报告】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)
- 【枚举】【前缀和】【map】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) 题解(A-E)
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)A. A Serial Killer(水题)
- Codeforces Round #400 (Div. 1 + Div. 2, combined)D. The Door Problem【2-sat Tarjan+思维建图】
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) E. The Holmes Children
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals