BZOJ1997: [HNOI2010] PLANAR - Problem Solving

https://www.lydsy.com/JudgeOnline/problem.php?id=1997

https://www.luogu.org/problemnew/show/P3209

If the undirected graph G=(V, E) can be drawn on the plane so that any two edges without coincident vertices do not intersect, then G is said to be a plane graph. The problem of determining whether a graph is a planar graph is an important problem in graph theory. Now suppose that what you want to determine is a special kind of graph, in which there is a cycle containing all vertices, that is, there is a Hamiltonian cycle.

m>3*n+6 is obviously NO.

One idea is to treat the Hamiltonian circuit as a shell, enumerate each edge, and then enumerate the other edge, and it is easy to determine whether the two sides intersect through the Hamiltonian order.

So does the intersection output NO at this time? Not really.

(I was entangled here, and later found that I was playing all the games for nothing. There is a game of untying the rope. After thinking about it, I knew that I could move the edge inside the shell to the outside of the shell to solve the contradiction.)

So it is divided into two areas: inside the shell and outside the shell. Just use and check the maintenance.

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
const int N=210;
const int M=10010;
inline int read(){
    int X=0,w=0;char ch=0;
    while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
    while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
struct node{
    int u,v;
}e[M];
int n,m,pos[N],t[N],fa[M*2];
int find(int x){
    if(fa[x]==x)return x;
    return fa[x]=find(fa[x]);
}
inline bool unionn(int x,int y){
    int x1,y1;
    if(find(x)==find(y))return 0;
    x1=find(x),y1=find(y+m);
    if(x1!=y1)fa[x1]=y1;
    x1=find(x+m),y1=find(y);
    if(x1!=y1)fa[x1]=y1;
    return 1;
}
inline bool pan(int i,int j){
    int xi=pos[e[i].u],yi=pos[e[i].v];
    int xj=pos[e[j].u],yj=pos[e[j].v];
    if(xi>yi)swap(xi,yi);
    if(xj>yj)swap(xj,yj);
    if(xi>xj)swap(xi,xj),swap(yi,yj);
    return xi<xj&&xj<yi&&yi<yj;
}
int main(){
    int T=read();
    while(T--){
        bool ok=1;
        n=read(),m=read();
        for(int i=1;i<=m;i++){
            e[i].u=read(),e[i].v=read();
        }
        for(int i=1;i<=n;i++)t[i]=read(),pos[t[i]]=i;
        if(m>3*n+6){puts("NO");continue;}
        for(int i=1;i<=2*m;i++)fa[i]=i;
        for(int i=1;i<=m&&ok;i++){
            for(int j=i+1;j<=m&&ok;j++){
                if(pan(i,j)){
                    ok=unionn(i,j);
                }
            }
        }
        if(!ok)puts("NO");
        else puts("YES");
    }
    return 0;
}

+++++++++++++++++++++++++++++++++++++++++++

+ Author of this article: luyouqi233. +

+Welcome to my blog: http://www.cnblogs.com/luyouqi233/  +

+++++++++++++++++++++++++++++++++++++++++++

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325387524&siteId=291194637