procedure2012
It's not worth it to know you're not worth it!

[关键字]:图论 二分图

[题目大意]:给出一个带有汉密尔顿回路的图,判断它是否是一个平面图。

//==============================================================================

[分析]:汉密尔顿回路回练成一个环,这个图必定被分成两部分,如果两条边相交无论同时在内还是在外都会相交,只有一条在环内一条在外才行——二分图!首先判断出那些边不再回路上然后把有矛盾的边连边利用染色法判断能否构成二分图,二分图的成立决定了平面图的成立。还有一点,平面图内设点数为v边数为e面数为r,v-e+r=2 e<=3*v-6 r<=2*v-4。

[代码]:

View Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

const int MAXE=10010;
const int MAXV=300;

struct node
{
int x,y;
}e2[MAXE];
int test,n,m,sum;
int h[MAXV],rank[MAXV],c[MAXV];
bool map[MAXV][MAXV],flag;
vector<int> e[MAXE],dag[MAXE];
vector<int>::iterator it;

bool Fright(int x1,int y1,int x2,int y2)
{
if (x1==x2 || x1==y2 || y1==x2 || y1==y2) return 0;
x1=rank[x1],y1=rank[y1];
x2=rank[x2],y2=rank[y2];
if (x1>y1) swap(x1,y1);
return (x1<x2 && x2<y1)!=(x1<y2 && y2<y1);
}

void Colour(int v,int x)
{
// printf("%d\n",v);
c[v]=x;
int size=dag[v].size();
for (int i=0;i<size;++i)
{
//printf("%d %d\n",v,dag[v][i]);
if (!c[dag[v][i]]) Colour(dag[v][i],-x); else
if (c[dag[v][i]]==x) flag=0;
if (!flag) return;
}
}

int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
scanf("%d",&test);
while (test--)
{
scanf("%d%d",&n,&m);
flag=1;
for (int i=1;i<=n;++i) e[i].clear();
for (int i=1;i<=m;++i)
{
int x,y;
scanf("%d%d",&x,&y);
e[x].push_back(y);
}
for (int i=1;i<=n;++i)
scanf("%d",&h[i]),rank[h[i]]=i;
memset(map,0,sizeof(map));
for (int i=2;i<=n;++i)
map[h[i-1]][h[i]]=map[h[i]][h[i-1]]=1;
map[h[n]][h[1]]=map[h[1]][h[n]]=1;
if (m>n*3-6)
{
printf("NO\n");
continue;
}
sum=0;
for (int i=1;i<=n;++i)
for (it=e[i].begin();it<e[i].end();++it)
if (!map[i][*it]) e2[++sum].x=i,e2[sum].y=*it;
//printf("%d\n",sum);
//for (int i=1;i<=sum;++i)
//printf("%d %d\n",e2[i].x,e2[i].y);
for (int i=1;i<=sum;++i) dag[i].clear();
for (int i=1;i<=sum;++i)
for (int j=i+1;j<=sum;++j)
if (Fright(e2[i].x,e2[i].y,e2[j].x,e2[j].y)) dag[i].push_back(j),dag[j].push_back(i);
//for (int i=1;i<=sum;++i)
//for (it=dag[i].begin();it<dag[i].end();++it)
//printf("%d %d\n",i,*it);
//printf("//=============================\n");
memset(c,0,sizeof(c));
for (int i=1;i<=sum;++i)
if (!c[i])
{
Colour(i,1);
//printf("//================================\n");
if (!flag) break;
}
if (flag) printf("YES\n"); else printf("NO\n");
}
return 0;
}



posted on 2012-04-07 15:13  procedure2012  阅读(1918)  评论(0编辑  收藏  举报