1997: [Hnoi2010]Planar

1997: [Hnoi2010]Planar

Time Limit: 10 Sec   Memory Limit: 64 MB
Submit: 1454   Solved: 569
[ Submit][ Status][ Discuss]

Description

Input

Output

Sample Input

2
6 9
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6
1 4 2 5 3 6
5 5
1 2
2 3
3 4
4 5
5 1
1 2 3 4 5

Sample Output

NO
YES

HINT

Source

[ Submit][ Status][ Discuss]




首先,平面图边数 <= 点数*3 - 6

然后。。每条边要么在里面要么在外面,搜索一下即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn = 21000;

int n,m,tail,bo[maxn],R[maxn],po[maxn],a[maxn],b[maxn],q[maxn];

vector <int> v[maxn*3]; 

bool dfs(int x)
{
	for (int i = 0; i < v[x].size(); i++) {
		int to = v[x][i];
		if (bo[to] == -1) {
			bo[to] = bo[x]^1;
			q[++tail] = i;
			if (!dfs(to)) return 0;
		}
		else if (bo[to] == bo[x]) return 0;
	}
	return 1;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	int t; cin >> t;
	while (t--) {
		scanf("%d%d",&n,&m);
		for (int i = 1; i <= m; i++) scanf("%d%d",&a[i],&b[i]);
		for (int i = 1; i <= n; i++) scanf("%d",&R[i]),po[R[i]] = i;
		if (m > 3*n-6) {printf("NO\n"); continue;}
		for (int i = 1; i <= m; i++) bo[i] = -1,v[i].clear();
		for (int i = 1; i <= m; i++)
			if (po[a[i]] > po[b[i]])
				swap(a[i],b[i]);
		for (int i = 1; i < m; i++)
			for (int j = i + 1; j <= m; j++) {
				if (po[a[i]] <= po[a[j]] && po[b[j]] <= po[b[i]]) continue;
				if (po[a[j]] <= po[a[i]] && po[b[i]] <= po[b[j]]) continue;
				if (po[b[i]] <= po[a[j]] || po[b[j]] <= po[a[i]]) continue;
				v[i].push_back(j); v[j].push_back(i);
			}
		bool flag = 1;
		for (int i = 1; i <= m; i++) 
			if (bo[i] == -1) {
				tail = 0;
				bo[i] = 0; q[++tail] = i; if (dfs(i)) continue;
				for (int j = 1; j <= tail; j++) bo[q[j]] = -1;
				tail = 0;
				bo[i] = 1; q[++tail] = i; if (dfs(i)) continue;
				flag = 0; break;
			}
		if (flag) printf("YES\n"); else printf("NO\n");
	} 
	return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值