[2015hdu多校联赛补题]hdu5302 Connect the Graph
题目链接: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的更多相关文章
- [2015hdu多校联赛补题]hdu5384 Danganronpa
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:函数f(A, B)定义:A.B为字符串,f(A, B)为A中有多少个不同的B(ex:f(& ...
- [2015hdu多校联赛补题]hdu5301 Buildings
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5301 题目大意:给你一块由1x1方格组成的矩形区域,其中有且仅有一个坏块,现在你要在上面建矩形的房子, ...
- [2015hdu多校联赛补题]hdu5378 Leader in Tree Land
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树 ...
- [2015hdu多校联赛补题]hdu5372 Segment Game
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5372 题意:进行n次操作,操作分两种,0和1,每一个0操作按出现顺序有一个编号(从1开始 0操作 0 ...
- [2015hdu多校联赛补题]hdu5371 Hotaru's problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...
- [2015hdu多校联赛补题]hdu5303 Delicious Apples
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...
- [2015hdu多校联赛补题]hdu5299 Circles Game
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5299 题意: 在欧几里得平面上有n个圆,圆之间不会相交也不会相切,现在Alice和Bob玩游戏,两人轮 ...
- [2015hdu多校联赛补题]hdu5348 MZL's endless loop
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1, ...
- [2015hdu多校联赛补题]hdu5324 Boring Class
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5324 题意:给你一个二维的序列,让你找出最长的第一维升第二维降的子序列(如果多个答案,输出字典序最小) ...
随机推荐
- valgind使用错误——检测不同位目标程序
当64位的valgrind工具测试32位的程序时,会报如下错误: ==22235== Memcheck, a memory error detector ==22235== Copyright (C) ...
- 安装初始化mysql后,默认几个库介绍
背景介绍: 当我们安装初始化mysql后,默认建了几个数据库,那么这些数据库有什么作用呢?mysql> show databases;+--------------------+| Datab ...
- Postgres 9.4 feature highlight: REPLICA IDENTITY and logical replication
Among the many things to say about logical replication features added in PostgreSQL 9.4, REPLICA IDE ...
- java之OOP
类中属性的默认值 1.数字类型(int,short,byte,long,float,double)的初始化默认值是0 2.boolean的初始化默认值是false 3.引用类型的初始化默认值是null ...
- Office web app server2013详细的安装和部署
转自:http://blog.csdn.net/u011355311/article/details/9360293 SharePoint 2013集成Office web apps server20 ...
- JS Math对象中一些小技巧
JS中快速获取数组中最大/最小值 var a=[1,2,3,5]; alert(Math.max.apply(Math, a));//最大值 alert(Math.min.apply(Math, a) ...
- java io流(字符流) 文件打开、读取文件、关闭文件
java io流(字符流) 文件打开 读取文件 关闭文件 //打开文件 //读取文件内容 //关闭文件 import java.io.*; public class Index{ public sta ...
- jquery 文本域光标操作(选、添、删、取)
一.JQuery扩展 ; (function ($) { /* * 文本域光标操作(选.添.删.取)的jQuery扩展 http://www.cnblogs.com/phpyangbo/p/55286 ...
- 用wamp配置的环境,想用CMD连接mysql怎么连
签:用wamp配置的环境 想用cmd连接mysql怎么连 进到d盘该目录 (cd切不了盘,就输入盘符加冒号回车,再cd到目录) WAMP装好后,mysql数据库运行时没有 mysql 和 ...
- Java为什么会引入及如何使用Unsafe
综述 sun.misc.Unsafe至少从2004年Java1.4开始就存在于Java中了.在Java9中,为了提高JVM的可维护性,Unsafe和许多其他的东西一起都被作为内部使用类隐藏起来了.但是 ...