UVALive 4849 String Phone
来源:互联网 发布:java集合框架详解 编辑:程序博客网 时间:2024/06/04 20:14
UVALive 4849 String Phone
图论·2-SAT
https://www.bbsmax.com/A/A2dm7YOJen/
一眼看去以为是4-sat。。。
题目大意:
给n(n<=3000)个黑方块的坐标,保证黑方块没有公共边。对于每个黑方块选一个角作为结点,使得所选结点满足输入的一个无向图。其中距离为曼哈顿距离。输出是否有解。possible或impossible。
题解:
题意:给n(n<=3000)个黑方块的坐标,保证黑方块没有公共边。对于每个黑方块选一个角作为结点,使得所选结点满足输入的一个无向图。其中距离为曼哈顿距离。输出是否有解。possible或impossible。
对于每个黑方块,4个角落必须选且仅选一个。一开始2-sat建模是对于一个pnt[i],有4对点pnt[i][0], pnt[i][0]’, pnt[i][1], pnt[i][1]’, pnt[i][2], pnt[i][2]’, pnt[i][3], pnt[i][3]’。但是这样建模只能保证4个角落至多选一个(pnt[i][j]连pnt[i][k]’ (k!=j)),保证不了至少选一个。
参考别人代码,发现神做法。类似2-sat的嵌套。。。01染色+2sat
把4个角落分组,左上和右下为第0组,右上和左下为第1组。(因为左上和右下的曼哈顿距离为2,有可能共存;右上和左下同理)。而左上和右上不可能共存,左上和左下同理。(没必要也没有意义把不共存的分为一组。)
通过染色,可以确定每个黑方块只有2种选择(黑方块染为第0组,则只能选左上或右下,且必须选且仅选一个;第1组同理)。到这里就可以看得出,显然的2-sat模型。感觉就像是2*2sat==4sat。
Code:
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#define D(x) cout<<#x<<" = "<<x<<" "#define E cout<<endl using namespace std;const int N = 6005;const int M = 300005*8;const int dx[2][2]={{0,1},{0,1}};const int dy[2][2]={{0,1},{1,0}};int n,m;int col[N]; struct Edge{ int to,next,w;};struct G{ Edge e[M*2]; int head[N],ec; void clear(){ memset(head,0,sizeof(head)); ec=0; } void add(int a,int b,int w=0){ // if(w==0){ D(a); D(b); E; } ec++; e[ec].to=b; e[ec].w=w; e[ec].next=head[a]; head[a]=ec; }} g;struct Point{ int x,y; Point(){} Point(int _x,int _y){ x=_x; y=_y; } void input(){ scanf("%d%d",&x,&y); }} p[N], pp[N][2];inline int dis(const Point &a,const Point &b){ return abs(a.x-b.x)+abs(a.y-b.y);}inline int mindis(const Point &a,const Point &b){ return max(abs(a.x-b.x)-1,0) + max(abs(a.y-b.y)-1,0);}inline int maxdis(const Point &a,const Point &b){ return abs(a.x-b.x)+abs(a.y-b.y)+2;}struct SAT{ bool mark[N]; int S[N],c; G sat_g; inline int ID(int x,int y){ return (x-1)*2+y; } void build(){ sat_g.clear(); for(int u=1;u<=n;u++){ if(col[u]==-1) continue; for(int i=g.head[u];i;i=g.e[i].next){ int v=g.e[i].to; int w=g.e[i].w; int dis1=dis(pp[u][0],pp[v][0]); int dis2=dis(pp[u][0],pp[v][1]); if(dis1!=w && dis2!=w){ sat_g.add(ID(u,0),ID(u,1)); } else if(dis1==w && dis2!=w){ sat_g.add(ID(u,0),ID(v,0)); } else if(dis1!=w && dis2==w){ sat_g.add(ID(u,0),ID(v,1)); } dis1=dis(pp[u][1],pp[v][0]); dis2=dis(pp[u][1],pp[v][1]); if(dis1!=w && dis2!=w){ sat_g.add(ID(u,1),ID(u,0)); } else if(dis1==w && dis2!=w){ sat_g.add(ID(u,1),ID(v,0)); } else if(dis1!=w && dis2==w){ sat_g.add(ID(u,1),ID(v,1)); } } } } bool dfs(int u){ if(mark[u^1]) return false; if(mark[u]) return true; mark[u]=true; S[c++]=u; for(int i=sat_g.head[u];i;i=sat_g.e[i].next){ if(!dfs(sat_g.e[i].to)) return false; } return true; } bool solve(){ memset(mark,false,sizeof(mark)); for(int i=0;i<n*2;i+=2){ if(!mark[i] && !mark[i+1]){ c=0; if(!dfs(i)){ while(c) mark[S[--c]]=false; if(!dfs(i+1)) return false; } } } return true; }} sat;struct UNSet{ int pa[N]; void init(){ for(int i=1;i<=n;i++) pa[i]=i; } int find(int x){ if(x!=pa[x]) pa[x]=find(pa[x]); return pa[x]; }} unset;bool color(int u,int bo){ col[u]=bo; for(int i=g.head[u];i;i=g.e[i].next){ int v=g.e[i].to; bool bo2 = (dis(p[u],p[v])&1) ^ (g.e[i].w&1) ^ bo; if(col[v]==-1){ if(color(v,bo2)==false) return false; } else if(col[v]!=bo2) return false; } return true;}bool judge(int u,int bo){ memset(col,-1,sizeof(col)); if(color(u,bo)==false) return false;// for(int i=1;i<=n;i++){// D(i); D(col[i]); E;// } for(int i=1;i<=n;i++){ if(col[i]==-1) continue; pp[i][0]=Point(p[i].x+dx[col[i]][0],p[i].y+dy[col[i]][0]); pp[i][1]=Point(p[i].x+dx[col[i]][1],p[i].y+dy[col[i]][1]); }// for(int i=1;i<=n;i++){// cout<<pp[i][0].x<<" "<<pp[i][0].y<<" "<<pp[i][1].x<<" "<<pp[i][1].y<<endl;// } sat.build(); bool ans=sat.solve(); return ans;}int main(){ freopen("a.in","r",stdin); int T; cin>>T; while(T--){ cin>>n; for(int i=1;i<=n;i++) p[i].input(); cin>>m; g.clear(); unset.init(); int a,b,w; bool flag=true; for(int i=1;i<=m;i++){ scanf("%d%d%d",&a,&b,&w); g.add(a,b,w); g.add(b,a,w);// if(w < mindis(p[a],p[b]) || w > maxdis(p[a],p[b])){// flag=false; break;// } if(unset.find(a)!=unset.find(b)) unset.pa[unset.find(a)]=unset.find(b); } for(int i=1;i<=n && flag;i++){ if(unset.pa[i]==i){// D(i); E; if(!judge(i,0) && !judge(i,1)){ flag=false; } } } if(flag) puts("possible"); else puts("impossible"); }}
- UVALive 4849 String Phone
- UVALive 3363 String Compression
- UVALive 4394 String painter
- UVALive 4394 String painter
- UVAlive-3363 String Compression
- Bit String Reordering UVALive
- Phone String Replacement
- UVA1351 UVALive 3363 String Compression
- UVALive - 3602 DNA Consensus String
- UVALive - 4394 String painter DP
- UVALive 6832Bit String Reordering
- Uvalive 6832 Bit String Reordering
- UVALive 6832Bit String Reordering
- UVALIVE 3954 Phone Cell <离散化 + 圆相交关系〉
- UVA 11243 Texas Trip ------ UVALIVE 3954 Phone Cell
- UVALive 4844 String Popping【记忆化搜索】
- UVALive - 3363 String Compression (区间DP)
- UVALive - 3602 DNA Consensus String 水题
- Spring MVC POJO传参方式
- 再论IBatisNet + Castle进行项目的开发
- 155. Min Stack
- 总结1:从动态绑定到jvm从加载到运行方法的过程.
- 设计模式:单例模式
- UVALive 4849 String Phone
- jsp、css中引入外部资源相对路径的问题
- 漏洞和事件处理简介
- 详解设计模式——抽象工厂模式
- input type为text和button的不对齐问题 以及 之间的小空格
- 树状数组--csu 1933
- F
- Spring MVC 使用Servlet原生API作为参数
- java-并发编程-概念