题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5302

题意:给你一个无向图,它的边要么是黑色要么是白色,且图上的每个点最多与两个黑边两个白边相连。现在,Demon将图分成两部分,一部分包含所有的黑边,另一部分包括所有的白边,给你白边图中度为0的点的数量w0,度为1的点数w1,度为2的点数w2,与黑边图中度为0的点数b1,度为1的点数b1,度为2的点数b2,要你输出任意一个符合条件的原图,如果不能,输出-1

(注1:无论是黑边图,还是白边图,给出的度为0、1、2三种点的数量均>=1;

 注2:w0+w1+w2==b0+b1+b2,输出图的点数最多为w0+w1+w2个;

 注3:图中无重边,无自环;

解:构造题

考虑每条边对应两度,那么w1或b1为奇数一定无解

排除以上情况,有w1>=2 && b1>=2,将二度的点排成一排,顺次链接起来,然后将首尾分别和一个一度点相连,剩下的一度点两两互相连接即可

这样我们就可以解决黑图或白图,考虑上述构造方法每个点只和相邻的点连接,我们在构造完一图之后,另外一图只需要稍微打乱下点的顺序即可

for example:

1 2 3 4 5 -> 1 4 2 5 3

另外n==4的时候需要特判一下。。。

 /*
* Problem: hdu5302 Connect the Graph
* Author: SHJWUDP
* Created Time: 2015/8/10 星期一 19:24:32
* File Name: 1006.cpp
* State: Accepted
* Memo: 构造
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm> using namespace std; int main() {
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif
int T;
scanf("%d", &T);
while(T--) {
vector<int> w(), b();
for(int i=; i<; i++) scanf("%d", &w[i]);
for(int i=; i<; i++) scanf("%d", &b[i]);
if((w[] & ) || (b[] & )) {
puts("-1"); continue;
}
int n=w[]+w[]+w[];
if(n<=) {
puts("4\n1 2 0\n1 3 0\n2 3 1\n3 4 1"); continue;
}
int m1=w[]+w[]/;
int m2=b[]+b[]/;
printf("%d\n", m1+m2);
int nw=;
w[]++;
while(w[]--) {
printf("%d %d 0\n", nw, nw+); nw++;
}
w[]-=; nw++;
while(w[]) {
printf("%d %d 0\n", nw, nw+); nw+=;
w[]-=;
}
vector<int> table(n+);
int pos=;
for(int i=; i<=n; i+=) table[pos++]=i;
for(int i=; i<=n; i+=) table[pos++]=i;
int nb=;
b[]++;
while(b[]--) {
printf("%d %d 1\n", table[nb], table[nb+]); nb++;
}
b[]-=; nb++;
while(b[]) {
printf("%d %d 1\n", table[nb], table[nb+]); nb+=;
b[]-=;
}
}
return ;
}

hdu5302

[2015hdu多校联赛补题]hdu5302 Connect the Graph的更多相关文章

  1. [2015hdu多校联赛补题]hdu5384 Danganronpa

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:函数f(A, B)定义:A.B为字符串,f(A, B)为A中有多少个不同的B(ex:f(& ...

  2. [2015hdu多校联赛补题]hdu5301 Buildings

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5301 题目大意:给你一块由1x1方格组成的矩形区域,其中有且仅有一个坏块,现在你要在上面建矩形的房子, ...

  3. [2015hdu多校联赛补题]hdu5378 Leader in Tree Land

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树 ...

  4. [2015hdu多校联赛补题]hdu5372 Segment Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5372 题意:进行n次操作,操作分两种,0和1,每一个0操作按出现顺序有一个编号(从1开始 0操作 0 ...

  5. [2015hdu多校联赛补题]hdu5371 Hotaru's problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...

  6. [2015hdu多校联赛补题]hdu5303 Delicious Apples

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...

  7. [2015hdu多校联赛补题]hdu5299 Circles Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5299 题意: 在欧几里得平面上有n个圆,圆之间不会相交也不会相切,现在Alice和Bob玩游戏,两人轮 ...

  8. [2015hdu多校联赛补题]hdu5348 MZL's endless loop

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1, ...

  9. [2015hdu多校联赛补题]hdu5324 Boring Class

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5324 题意:给你一个二维的序列,让你找出最长的第一维升第二维降的子序列(如果多个答案,输出字典序最小) ...

随机推荐

  1. ADC 电源监测

    我能为别人做点什么?这是我最近在思考的问题. 看了 ADC 电源监测代码,觉得对 ADC 的理解不到位,代码中有很多部分都不懂.如: 1. 为什么初始化的时候管脚设置为输出? 2. ADC 采集到的值 ...

  2. c++代码美化

    int main() if else return 0; int main() if else return 0; int main() if else return 0; int main() if ...

  3. linux ssh服务器

    默认配置文件在: /etc/ssh/下 有两个需要配置的文件: ssh_config    ssh_client配置文件 sshd_config  ssh服务器的配置文件 两个文件的详细介绍和配置方法 ...

  4. apache 配置多个虚拟主机,不同的端口

    1.在httpd.conf添加多个端口,如下 Listen 80Listen 8080 2.开启Include conf/extra/httpd-vhosts.conf 3.具体代码如下 <Vi ...

  5. python 实现二分法查找

    二分查找图 二叉树: 代码 #!/usr/bin/python #-*-coding:utf-8-*- #----------------------------------------------- ...

  6. python标准库xml.etree.ElementTree的bug

    使用python生成或者解析xml的方法用的最多的可能就数python标准库xml.etree.ElementTree和lxml了,在某些环境下使用xml.etree.ElementTree更方便一些 ...

  7. JAVA 新手问题: Request 编码编译出错,Unhandled exception type UnsupportedEncodingException

    新手: 编写如下代码 private void Exec(HttpServletRequest Req,HttpServletResponse Response) //throws ServletEx ...

  8. [VB.NET]调用系统的文件夹选择对话框

    以下示例代码展示如何调用系统的文件夹选择对话框: Private Function SelectFolder(ByVal Describe As String, Optional ByVal Show ...

  9. math汇总

    **** 1/(1^2) + 1/(2^2) + … + 1/(n^2)会收敛到pi^2/6,当n的数位大于6位数字时,最后的结果就是pi^2/6 ****** &的大作用 1.先看看这个n= ...

  10. 解决activity加上Theme.Translucent.NoTitleBar 页面跳转显示桌面

    自定义style 继承Theme.Translucent.NoTitleBar       <style name="My.Translucent" parent=" ...