1997: [Hnoi2010]Planar
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 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
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
YES
HINT
Source
首先,平面图边数 <= 点数*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;
}