判断存在一条Hamilton回路的图是否是平面图。
和POJ3207几乎一样,只要把 判断相交的条件改成按回路编号比较就行了。
http://blog.csdn.net/braketbn/article/details/50834302
/* Footprints In The Blood Soaked Snow */
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxm = 10005, maxn = maxm << 2, maxs = maxn, maxd = 205;
int n, m, head[maxn], cnt, dfn[maxn], low[maxn], clo, belong[maxn], tot, id[maxn];
bool ins[maxn];
struct _edge {
int v, next;
} g[maxn];
struct _pos {
int x, y;
} p[maxm];
inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
inline int add(int u, int v) {
g[cnt] = (_edge){v, head[u]};
head[u] = cnt++;
}
int sta[maxs], top;
inline void tarjan(int x) {
dfn[x] = low[x] = clo++;
sta[++top] = x; ins[x] = 1;
for(int i = head[x]; ~i; i = g[i].next) {
int v = g[i].v;
if(!dfn[v]) tarjan(v), low[x] = min(low[x], low[v]);
else if(ins[v]) low[x] = min(low[x], dfn[v]);
}
if(dfn[x] == low[x]) {
tot++;
while(1) {
int u = sta[top--];
belong[u] = tot;
ins[u] = 0;
if(u == x) break;
}
}
}
inline bool check() {
for(int i = (m << 1) - 1; i >= 0; i--)
if(belong[i] == belong[i ^ 1])
return 1;
return 0;
}
int main() {
int T = iread();
while(T--) {
n = iread(); m = iread();
for(int i = 0; i < maxn; i++) ins[i] = dfn[i] = low[i] = 0, head[i] = -1; cnt = 0;
for(int i = 0; i < m; i++) p[i] = (_pos){iread(), iread()};
for(int i = 1; i <= n; i++) id[iread()] = i;
for(int i = 0; i < m; i++) for(int j = i + 1; j < m; j++) {
int x1 = id[p[i].x], y1 = id[p[i].y], x2 = id[p[j].x], y2 = id[p[j].y];
if(x1 > y1) swap(x1, y1);
if(x2 > y2) swap(x2, y2);
if((x1 < x2 && x2 < y1 && y1 < y2) || (x2 < x1 && x1 < y2 && y2 < y1)) {
add(i << 1, j << 1 | 1);
add(i << 1 | 1, j << 1);
add(j << 1, i << 1 | 1);
add(j << 1 | 1, i << 1);
}
}
top = clo = tot = 0;
for(int i = (m << 1) - 1; i >= 0; i--) if(!dfn[i]) tarjan(i);
if(check()) printf("NO\n");
else printf("YES\n");
}
return 0;
}