题目链接:
题意:
给四个不同点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
所以只要满足k>=n+1,就可以构造出来答案。
#include#include #include #include #include using namespace std;int n, m;const int maxn = 1111;int arr[maxn],mp[maxn];int arr2[maxn];int main() { int a, b, c, d; while (scanf("%d%d", &n, &m) == 2 && n) { scanf("%d%d%d%d", &a, &b, &c, &d); if (n ==4 || m < n + 1) { printf("-1\n"); continue; } memset(mp, 0, sizeof(mp)); memset(arr, 0, sizeof(arr)); mp[a] = mp[b] = mp[c] = mp[d] = 1; arr[1] = a; arr[n] = b; arr[2] = c; arr[4] = d; int tot = 5; for (int i = 1; i <= n; i++) { if (mp[i]) continue; if (arr[3] == 0) arr[3] = i; else { arr[tot++]= i; } } for (int i = 1; i < n; i++) printf("%d ", arr[i]); printf("%d\n", arr[n]); memset(arr2, 0, sizeof(arr2)); arr2[1] = c; arr2[n] = d; arr2[2] = arr[1]; arr2[3] = arr[3]; tot = 4; for (int i = n; i >=5; i--) arr2[tot++] = arr[i]; for (int i = 1; i < n; i++) printf("%d ", arr2[i]); printf("%d\n", arr2[n]); } return 0;}