Gym 101201.F
来源:互联网 发布:小学安全网络知识竞赛 编辑:程序博客网 时间:2024/04/27 19:51
题意:给你一个n*n的方格,里面有k个方格有灯,每个灯可以选择左右发射长度r格的光或者上下发射,问你是否有一种方案,使得每个格子,不会被同为纵向(横向)的多束光照射。
n,k≤1000 。
思路:2-SAT模板题。枚举每一对灯,如果两者光线有矛盾,加逻辑语句即可。
#include <bits/stdc++.h>using namespace std;const int maxn = 2000 + 5;struct TwoSat{//2i为假 2i+1为真 int n; vector<int>G[maxn * 2]; bool mark[maxn * 2]; int S[maxn * 2], c; void init(int n) { this->n = n; for(int i = 0; i < n * 2; i++) G[i].clear(), mark[i] = 0; } bool dfs(int x) { if(mark[x^1]) return false; if(mark[x]) return true; mark[x] = true; S[c++] = x; for(auto o : G[x]) if(!dfs(o)) return false; return true; } void add_clause(int x, int xval, int y, int yval) { x = x * 2 + xval; y = y * 2 + yval; G[x^1].push_back(y); G[y^1].push_back(x); } bool solve() { for(int i = 0; i < n * 2; i += 2) { if(!mark[i] && !mark[i + 1]) { c = 0; if(!dfs(i)) { while(c > 0) mark[S[--c]] = false; if(!dfs(i + 1)) return false; } } } return true; }}solver;struct node{int x, y;}nodes[maxn];int main(){ int n, r, l; scanf("%d%d%d", &n, &r, &l); for(int i = 0, x, y; i < l; i++) { scanf("%d%d", &x, &y); nodes[i] = {x, y}; } solver.init(l); for(int i = 0; i < l; i++) { for(int j = i + 1; j < l; j++) { if(nodes[i].x == nodes[j].x && abs(nodes[i].y - nodes[j].y) <= r) { solver.add_clause(i, 0, j, 0); } if(nodes[i].y == nodes[j].y && abs(nodes[i].x - nodes[j].x) <= r) { solver.add_clause(i, 1, j, 1); } } } if(solver.solve()) puts("YES"); else puts("NO"); return 0;}
阅读全文
0 0
- Gym 101201.F
- [Gym-101350F] F
- Gym 100685 F Flood
- Gym 100989F STL
- codeforces Gym 101102F
- Codeforces-GYM-100726F
- Codeforces GYM 100803F
- Gym - 100947F
- [Gym-100520F]
- Gym 101142F Folding
- Gym 100553F-Filter
- Gym 101164.F
- Gym 101482.F
- Gym 100513F - Ilya Muromets
- Gym 100712F - Travelling Salesman
- Gym - 100685F Flood BFS
- Gym 100962F Frank Sinatra
- Gym-100712F-Travelling Salesman
- NOIP2011提高组-Car的旅行路线
- 数组去重
- 三顾茅庐:从刘备面试孔明得出的选股经验
- sed,awk多条件匹配
- 【Kalman】卡尔曼滤波Matlab简单实现
- Gym 101201.F
- jvm(三):对象引用与垃圾回收
- static libcurl 在vs中使用方式
- SQL和T-SQL的区别是什么?
- 区块宝周报:区块链技术专利大战已经打响
- 每日区块链:日本银行协会会长——平野信行:加密货币意味着新的数据机遇
- 辗转多次,又回到了起点
- 轻松掌握ARM架构
- ES6 变量作用域与提升:变量的生命周期详解