Algorithm: POJ 2296 Map Labeler (2-sat+ two points)

Source: Internet
Author: User

"The main effect of the topic"

There are n dots on the axis, to paste a square at each point, the square's bottom and bottom sides are parallel to the x,y axis, and the point is either the midpoint of the upper edge of the square, or the midpoint of the next edge, and the squares of any two points do not overlap (can be repeated). Q What is the maximum side length of a square?

Ideas

It is easy to see that the square is either above the point, or below, so it is judged by the 2-sat, the key is the edge of the judgment, to involve two square position overlapping relationship is more troublesome.

Then the sides of the second square can be long.

Code

#include <iostream> #include <queue> #include <cstdio> #include <cstring> using namespace std  
    
;  
const int MAXN = 110;  
const int VN = MAXN*2;  
const int EN = VN*VN*2*10;  
    
const int INF = 0X3F3F3F3F;  
    
int n;  
int Abs (int n) {return n<0?-n:n;  
struct node{int x, y;  
    
}ARR[MAXN];  
    struct graph{int size, HEAD[VN]; Struct{int V, next;  
    E[en];  
    void Init () {size=0; memset (Head,-1, sizeof (head));  
        void Addedge (int u, int v) {e[size].v = v;  
        E[size].next = Head[u];  
    Head[u] = size++;  
    
}}g;  
        Class two_sat{Public:bool Check (const graph& g, const int n) {SCC (g, 2*n);  
        for (int i=0; i<n; ++i) if (belong[i] = = Belong[i+n]) return false;  
    return true;  
        } private:void Tarjan (const graph& g, const int u) {int V;  Dfn[u] = low[u] = ++idx;
        sta[top++] = u;  
    
        Instack[u] = true;  
            for (int e=g.head[u]; E!=-1 e=g.e[e].next) {v = g.e[e].v;  
                if (dfn[v] = =-1) {Tarjan (g, v);  
            Low[u] = min (Low[u], low[v]);     
            }else if (Instack[v]) {Low[u] = min (Low[u], dfn[v]);  
            } if (dfn[u] = = Low[u]) {++bcnt;  
                do{v = sta[--top];  
                INSTACK[V] = false;  
            BELONG[V] = bcnt;  
        }while (U!= v);  
        } void SCC (const graph& g, const int n) {idx = top = bcnt = 0;  
        memset (DFN,-1, sizeof (DFN));  
        memset (instack, 0, sizeof (instack));  
        for (int i=0; i<n; ++i) {if (dfn[i] = = 1) Tarjan (g, I);  
    } private:int idx, top, bcnt;  
    int DFN[VN], low[vn], STA[VN], BELONG[VN]; BooL INSTACK[VN];  
    
}sat;  
    void buildgraph (int r) {g.init (); for (int i=0; i<n; ++i) {for (int j=i+1; j<n; ++j) {if (Abs (arr[i].x-arr[j].x) >= R) continu  
            E  
            if (Abs (ARR[I].Y-ARR[J].Y) >= 2*r) continue; if (Abs (ARR[I].Y-ARR[J].Y) < R) {if (Arr[i].y > Arr[j].y) {//I on top G.addedge (i  
                    , j+n);  
                    G.addedge (I+n, i);  
                    G.addedge (J, J+n);  
                G.addedge (J+n, i);  
                    }else if (Arr[i].y < arr[j].y) {G.addedge (J, I+n);  
                    G.addedge (J+n, J);  
                    G.addedge (i, i+n);  
                G.addedge (I+n, J);  
                    }else{G.addedge (i, j+n);  
                    G.addedge (I+n, J);  
                    G.addedge (J, I+n);  
                G.addedge (J+n, i);  
  }}else{              if (Arr[i].y > Arr[j].y) {g.addedge (i+n, j+n);  
                G.addedge (J, I);  
                    }else{G.addedge (J+n, i+n);  
                G.addedge (i, j);  
    int main () {int ncase;  
    
    scanf ("%d", &ncase);  
    
        while (ncase--) {scanf ("%d", &n);  
    
        for (int i=0; i<n; ++i) scanf ("%d%d", &arr[i].x, &ARR[I].Y);    
        int l=0;  
        int r=20000, M;   
        int ans=0;  
            while (L < R) {//two-side long M = (l+r) >>1;  
            Buildgraph (m);  
                if (Sat.check (g, N)) {ans = m;  
            l=m+1;  
        else r=m;  
    printf ("%d\n", ans);  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.