Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D Bear and Two Paths

题目链接:

http://codeforces.com/contest/673/problem/D

题意:

给四个不同点a,b,c,d,求是否能构造出两条哈密顿通路,一条a到b,一条c到d。

题解:

构造法,看例子:

input:

5 6

5 2 4 1

output:

5 4 3 1 2

4 5 3 2 1

Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D  Bear and Two Paths

所以只要满足k>=n+1,就可以构造出来答案。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std; int n, m; const int maxn = ;
int arr[maxn],mp[maxn];
int arr2[maxn]; int main() {
int a, b, c, d;
while (scanf("%d%d", &n, &m) == && n) {
scanf("%d%d%d%d", &a, &b, &c, &d);
if (n == || m < n + ) {
printf("-1\n"); continue;
}
memset(mp, , sizeof(mp));
memset(arr, , sizeof(arr));
mp[a] = mp[b] = mp[c] = mp[d] = ;
arr[] = a; arr[n] = b;
arr[] = c; arr[] = d;
int tot = ;
for (int i = ; i <= n; i++) {
if (mp[i]) continue;
if (arr[] == ) arr[] = i;
else {
arr[tot++]= i;
}
}
for (int i = ; i < n; i++) printf("%d ", arr[i]); printf("%d\n", arr[n]); memset(arr2, , sizeof(arr2));
arr2[] = c; arr2[n] = d; arr2[] = arr[]; arr2[] = arr[];
tot = ;
for (int i = n; i >=; i--) arr2[tot++] = arr[i];
for (int i = ; i < n; i++) printf("%d ", arr2[i]); printf("%d\n", arr2[n]);
}
return ;
}
上一篇:VC++动态链接库(DLL)编程深入浅出(四)


下一篇:简单看看这两个类 String和StringBuilder