HDU
来源:互联网 发布:天猫好还是淘宝好 编辑:程序博客网 时间:2024/06/05 22:51
HDU - 1814
题意:直白的2-sat,但是这个是要输出最小的字典序,那么我们只能选择暴力。
思路:我的暴力的想法是,枚举每对元素,先优先取靠前的那个,把它所有连了边的都取了然后打tag,如果它不会产生矛盾,那么继续,如果有,那么先把所有的tag取消,再判断a+1有没有矛盾,如果a+1也有矛盾,那这组数据就已经gg了。我已经是个蠢到取消tag也要用dfs的人了,看了别人的暴力...基本都是用栈保留然后取消tag,我试了一下,我的两波dfs要跑2s.....而用栈处理.只需要1s不到。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 8e3 + 50, maxe = 2e5;struct node{ int to,next; node(int a,int b){to = a; next = b;} node(){}}edge[maxe];int h[maxn<<1];int n,m,edgenum,snum=0;int vis[maxn],col[maxn],ans[maxn],S[maxn];void add(int f,int t){ edge[edgenum] = node(t,h[f]); h[f] = edgenum++;}int flag = 0;bool dfs(int u,int root){ if(col[u^1] != -1) return 0; if(col[u] != -1) return 1; col[u] = root; for(int i = h[u]; ~i; i = edge[i].next) { int v = edge[i].to; if(dfs(v,root) == 0) return 0; } return true;}void clean(int u){ col[u] = -1; for(int i = h[u]; ~i; i = edge[i].next) { int v = edge[i].to; if(col[v] != -1) clean(v); }}int main(){ int a,b; while(~scanf("%d%d",&n,&m)) { edgenum = snum = 0; for(int i = 0; i < n*2 ; i++) h[i] = -1,col[i] = -1; for(int i = 0; i < m ; i++) { scanf("%d%d",&a,&b); a--,b--; add(a,b^1); add(b,a^1); } int mark = 0; int num = 0; for(int i = 0; i < n*2 ; i += 2) { if(col[i] == -1 && col[i+1] == -1) { if(dfs(i,i)) continue; else { clean(i); if(dfs(i+1,i+1)) continue; else {mark = 1;break;} } } } if(mark) {printf("NIE\n");} else { for(int i = 0; i < n*2; i++) if(col[i]!=-1)printf("%d\n",i+1); } } return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- Git Submodule简单使用
- Java解析XML
- 二.BeautifulSoup多线程下载百思不得姐图片
- MySQL触发器使用详解
- 大前端之路
- HDU
- 查看jvm内存使用情况
- 利用navicat创建存储过程、触发器和使用游标的简单实例
- LeetCode-447. Number of Boomerangs (Java)
- 【备忘】2017北京黑马17期JavaEE基础+就业班全套含解压密码
- PAT (Advanced Level) Practise 1105 Spiral Matrix (25)
- "沐事店员工具"App隐私政策
- 可用的redmine邮箱配置
- iOS 10新的通知机制中 展示图片、Gif、Audio和Video