2*i 表示连接s2
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<vector> #include<sstream> #include<string> #include<climits> #include<stack> #include<set> #include<bitset> #include<cmath> #include<deque> #include<map> #include<queue> #define iinf 2000000000 #define linf 1000000000000000000LL #define dinf 1e200 #define eps 1e-11 #define all(v) (v).begin(),(v).end() #define sz(x) x.size() #define pb push_back #define mp make_pair #define lng long long #define sqr(a) ((a)*(a)) #define pii pair<int,int> #define pll pair<lng,lng> #define pss pair<string,string> #define pdd pair<double,double> #define X first #define Y second #define pi 3.14159265359 #define ff(i,xi,n) for(int i=xi;i<=(int)(n);++i) #define ffd(i,xi,n) for(int i=xi;i>=(int)(n);--i) #define ffl(i,r) for(int i=head[r];i!=-1;i=edge[i].next) #define cc(i,j) memset(i,j,sizeof(i)) #define two(x) ((lng)1<<(x)) #define N 2000 #define M 1000000 #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1 | 1 #define Mod n #define Pmod(x) (x%Mod+Mod)%Mod using namespace std; typedef vector<int> vi; typedef vector<string> vs; typedef unsigned int uint; typedef unsigned lng ulng; template<class T> inline void checkmax(T &x,T y){if(x<y) x=y;} template<class T> inline void checkmin(T &x,T y){if(x>y) x=y;} template<class T> inline T Min(T x,T y){return (x>y?y:x);} template<class T> inline T Max(T x,T y){return (x<y?y:x);} template<class T> T gcd(T a,T b){return (a%b)==0?b:gcd(b,a%b);} template<class T> T lcm(T a,T b){return a*b/gcd(a,b);} template<class T> T Abs(T a){return a>0?a:(-a);} template<class T> inline T lowbit(T n){return (n^(n-1))&n;} template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));} template<class T> inline bool isPrimeNumber(T n) {if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;} template<class T> inline T Minmin(T a,T b,T c,T d){return Min(Min(a,b),Min(c,d));} struct tarjan_seg { int head[N],tot,id[N],pre[N],low[N],stack[N],top,n,Index,nn,newid,newhead[N],*cur,contain[N]; struct pp { int v,next; }edge[N*N]; void add(int u,int v) { edge[tot].v=v; edge[tot].next=cur[u]; cur[u]=tot++; } void init() { cc(head,-1); cc(newhead,-1); tot=0; newid=0; cur=head; cc(pre,-1); cc(low,-1); top=0; cc(id,-1); Index=0; } void tarjan(int r,int p) { bool xx=false; pre[r]=low[r]=++Index; stack[++top]=r; ffl(i,r) { int v=edge[i].v; if(v==p&&xx==0) { xx=1; continue; } if(pre[v]==-1) { tarjan(v,r); checkmin(low[r],low[v]); } else if(id[v]==-1) checkmin(low[r],pre[v]); } if(pre[r]==low[r]) { int v; ++newid; do { v=stack[top--]; id[v]=newid; }while(v!=r); } } void solve() { ff(i,1,n) if(pre[i]==-1) tarjan(i,-1); } void build_map() { int hash[N]={}; cur=newhead; ff(i,1,n) { ffl(j,i) { int v=edge[j].v; if(id[i]!=id[v]&&hash[id[i]]!=id[v]) add(id[v],id[i]),hash[id[i]]=id[v]; } } } void solve_contain() { cc(contain,-1); cur=contain; ff(i,1,n) add(id[i],i); } bool two_sat() { solve(); for(int i=1;i<=n;i+=2) if(id[i]==id[i+1]) return false; return true; } void dfs(int r) { pre[r]=++Index; for(int i=newhead[r];i!=-1;i=edge[i].next) if(pre[edge[i].v]==-1) dfs(edge[i].v); low[++newid]=r; } void color_dfs(int r) { pre[r]=2; for(int i=newhead[r];i!=-1;i=edge[i].next) if(pre[edge[i].v]==-1) color_dfs(edge[i].v); } void Color() { cc(pre,-1); for(int i=nn;i>0;--i) if(pre[low[i]]==-1) { int a=edge[contain[low[i]]].v; int b; b=((a+1)/2)*4-1-a; pre[low[i]]=1; if(pre[id[b]]==-1) color_dfs(id[b]); } } void topo_sort() { nn=newid; Index=0; newid=0; cc(pre,-1); cc(low,-1); ff(i,1,nn) if(pre[i]==-1) dfs(i); } void taganswer() { cc(low,-1); ff(i,1,nn) if(pre[i]==1) { for(int j=contain[i];j!=-1;j=edge[j].next) low[edge[j].v]=1; } } void print_answer() { if(!two_sat()) {printf("NIE\n"); return ; } // printf("YES\n"); solve_contain(); build_map(); topo_sort(); Color(); taganswer(); ff(i,1,n) { if(low[i]==1) { printf("%d\n",i); } } } }; tarjan_seg G; int na,nb,nm; struct pps { int x,y; }s1,s2,a[N],ad1[N],ad2[N]; int dist[N]; int l,r,mid; int kdis; bool check() { G.init(); G.n=2*nm; ff(i,1,na) { int u,v; u= ad1[i].x,v=ad1[i].y; G.add(2*u-1,2*v); G.add(2*v-1,2*u); G.add(2*u,2*v-1); G.add(2*v,2*u-1); } ff(i,1,nb) { int u,v; u=ad2[i].x,v=ad2[i].y; G.add(2*u-1,2*v-1); G.add(2*v-1,2*u-1); G.add(2*u,2*v); G.add(2*v,2*u); } ff(i,1,2*nm) ff(j,i+1,2*nm) { if((i+1)/2==(j+1)/2) continue; int udis=dist[i]+dist[j]; if(i%2+j%2==1) udis+=kdis; if(udis>mid) G.add(i,(j+1)/2*4-1-j),G.add(j,(i+1)/2*4-1-i); } return G.two_sat(); } int main() { while(scanf("%d%d%d",&nm,&na,&nb)==3) { G.init(); G.n=2*nm; scanf("%d%d%d%d",&s1.x,&s1.y,&s2.x,&s2.y); ff(i,1,nm) scanf("%d%d",&a[i].x,&a[i].y); ff(i,1,nm) dist[2*i-1]=Abs(a[i].x-s1.x)+Abs(a[i].y-s1.y),dist[2*i]=Abs(a[i].x-s2.x)+Abs(a[i].y-s2.y); kdis=Abs(s1.x-s2.x)+Abs(s1.y-s2.y); ff(i,1,na) { int u,v; scanf("%d%d",&u,&v); ad1[i].x=u,ad1[i].y=v; G.add(2*u-1,2*v); G.add(2*v-1,2*u); G.add(2*u,2*v-1); G.add(2*v,2*u-1); } ff(i,1,nb) { int u,v; scanf("%d%d",&u,&v); ad2[i].x=u,ad2[i].y=v; G.add(2*u-1,2*v-1); G.add(2*v-1,2*u-1); G.add(2*u,2*v); G.add(2*v,2*u); } if(G.two_sat()); else { printf("-1\n"); continue; } l=0,r=100000000; while(l<=r) { mid=(l+r)>>1; if(check()) r=mid-1; else l=mid+1; } printf("%d\n",l); } return 0; }