# bzoj 1997: [Hnoi2010]Planar

### solution

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=605,M=605*605*2;
struct node{int x,y;}e[10005];
void Clear(){
memset(col,0,sizeof(col));flag=1;
}
inline void dfs(int x){
int u=to[i];
if(!col[u]){
col[u]=3-col[x];
dfs(u);
}
else if(col[u]==col[x]){flag=0;return ;}
if(!flag)return ;
}
}
void work()
{
Clear();
int x1,y1,x2,y2;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)scanf("%d%d",&e[i].x,&e[i].y);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),id[a[i]]=i;
if(m>3*n-6){puts("NO");return ;}
for(int i=1;i<=m;i++){
for(int j=i+1;j<=m;j++){
x1=id[e[i].x];y1=id[e[i].y];
x2=id[e[j].x];y2=id[e[j].y];
if(x1==x2 || x1==y2 || y1==x2 || y1==y2)continue;
if(x1>y1)swap(x1,y1);
if(x2>y2)swap(x2,y2);
if(x1>x2)swap(x1,x2),swap(y1,y2);
}
}
for(int i=1;i<=m;i++){
if(col[i])continue;
col[i]=1;dfs(i);
if(!flag){puts("NO");return ;}
}
puts("YES");
}

int main()
{
int T;cin>>T;
while(T--)work();
return 0;
}


posted @ 2017-12-03 20:12  PIPIBoss  阅读(175)  评论(0编辑  收藏  举报