CodeForces 643 B.Bear and Two Paths(构造)
来源:互联网 发布:电脑制作漫画软件 编辑:程序博客网 时间:2024/05/29 03:20
Description
给出n个点,要求用至多k条无向边构造出从a到b和从c到d的哈密顿通路,且a和b之间以及c和d之间不能右边,如果存在则输出这两条哈密顿通路,否则输出-1
Input
第一行两个整数n和k表示点数和边数上限,之后四个两两不同的整数a,b,c,d(4<=n<=1000,n-1<=k<=2n-2,1<=a,b,c,d<=n)
Output
如果存在合法方案则输出a->b和c->d的两条哈密顿通路,否则输出-1
Sample Input
7 11
2 4 7 3
Sample Output
2 7 1 3 6 5 4
7 1 5 4 6 2 3
Solution
首先构造从a到b的哈密顿通路需要n-1条边,为节省连边,c到d的哈密顿通路要尽量用已有的边,但是由于从c出发必然要经过a点和b点后再经过d点,所以至少需要两条边来中转,即按如下方案构造:a-c-u-….-v-d-b,其中a和u连,v和b连,那么c到d的哈密顿通路即为c-a-u-…-v-b-d,这种方案用了最少的边(n+1条边),但是需要只要一个中转点(即u和v),故n < 5或者k < n+1时均无解
Code
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue>#include<map>#include<set>#include<ctime>using namespace std;typedef long long ll;#define INF 0x3f3f3f3f#define maxn 1111int n,k,a,b,c,d,ans[maxn];int main(){ while(~scanf("%d%d",&n,&k)) { scanf("%d%d%d%d",&a,&b,&c,&d); if(n<5||k<n+1)printf("-1\n"); else { ans[1]=a,ans[2]=c,ans[n-1]=d,ans[n]=b; for(int i=3,j=1;i<=n-2;i++) { while(j==a||j==b||j==c||j==d)j++; ans[i]=j++; } for(int i=1;i<=n;i++)printf("%d%c",ans[i],i==n?'\n':' '); printf("%d %d %d ",ans[2],ans[1],ans[3]); for(int i=4;i<=n-2;i++)printf("%d ",ans[i]); printf("%d %d\n",ans[n],ans[n-1]); } } return 0;}
0 0
- CodeForces 643 B.Bear and Two Paths(构造)
- CodeForces 673D Bear and Two Paths(构造)
- Codeforces 673D Bear and Two Paths (贪心构造)
- CodeForces 673D - Bear and Two Paths(构造)
- CodeForces 639 B.Bear and Forgotten Tree 3(构造)
- Codeforces 673D Bear and Two Paths【贪心】
- Codeforces 639B Bear and Forgotten Tree 3 【构造】
- Codeforces 771B Bear and Different Names[构造]
- codeforcs 351 D. Bear and Two Paths
- codeforce之Bear and Two Paths
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths
- CodeForces 658C Bear and Forgotten Tree 3(构造)
- Codeforces 385B Bear and Strings(字符串)
- CodeForces 639B Bear and Forgotten Tree 3(水题)
- CodeForces 658B Bear and Displayed Friends(树状数组)
- [CodeForces 573B] Bear and Blocks (脑洞题)
- CodeForces 680B Bear and Finding Criminals(捉小偷)
- Python的Django
- 数段简单趣味的JavaScript代码
- 完美解决glide+StaggeredGridLayoutManager 图片加载大小不正常的问题
- 多线程之多线程的创建
- java.lang.UnsatisfiedLinkError: com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLo
- CodeForces 643 B.Bear and Two Paths(构造)
- mybatis查询,sql语句没问题但是查不出结果
- 二叉树的三种非递归遍历
- 视频码流分析工具
- 图的基本操作【严蔚敏】
- 蓝桥杯入门训练 序列求和 JAVA
- P1067 多项式输出
- java中MD5加密
- 提示框消息