题目链接: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. C语言实现Web客户端(转-kungstriving)

    和我的上一篇文章是一起写的,呵呵,大家给提点意见啊.   :-)   /*********filename : Client.cpp****************    该程序通过标准socket实 ...

  2. Git的配置及常用命令

    Git配置 git config --global user.name "<username>" git config --global user.email &quo ...

  3. CenOS 7 安装mysql

    1:安装YUM源 2:利用file zilla 将mysql文件拖放到 /var/opt  目录下 3:在centos当中,已经将mysql的文件放到了 /var/opt  我们只需要安装就可以 安装 ...

  4. I do not want to inherit the child opacity from the parent in CSS(不想让子元素继承父元素的透明度)

    Instead of using opacity, set a background-color with rgba, where 'a' is the level of transparency. ...

  5. 异步等待(ManualResetEvent

    ManualResetEvent实现异步等待,超过时间 不做处理,继续往下执行代码 (ManualResetEvent 涉及一个线程在其他线程进行之前必须完成的任务) ManualResetEvent ...

  6. 动态加载、移除、替换JS和CSS

    //动态加载一个js/css文件 function loadjscssfile(filename, filetype) { if (filetype == "js") { var ...

  7. Lab_1_SysOps_Compute_Linux_v2.5

    System Operations - Lab 1: Creating Elastic Compute Cloud (Amazon EC2) Instances (Linux) - 2.5 ===== ...

  8. Asp.net MVC中 Controller 与 View之间的数据传递

    在ASP.NET MVC中,经常会在Controller与View之间传递数据 1.Controller向View中传递数据 (1)使用ViewData["user"] (2)使用 ...

  9. html 定位问题

    HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...

  10. RabbitMQ、Memcache、Redis(队列、缓存)

    RabbitMQ 一.解释 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消 ...