Connect the Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 108    Accepted Submission(s): 36
Special Judge

Problem Description
Once there was a special graph. This graph had n vertices
and some edges. Each edge was either white or black. There was no edge connecting one vertex and the vertex itself. There was no two edges connecting the same pair of vertices. It is special because the each vertex is connected to at most two black edges and
at most two white edges.

One day, the demon broke this graph by copying all the vertices and in one copy of the graph, the demon only keeps all the black edges, and in the other copy of the graph, the demon keeps all the white edges. Now people only knows there are w0 vertices
which are connected with no white edges, w1 vertices
which are connected with 1 white
edges, w2 vertices
which are connected with 2 white
edges, b0 vertices
which are connected with no black edges, b1 vertices
which are connected with 1 black
edges and b2 vertices
which are connected with 2 black
edges.

The precious graph should be fixed to guide people, so some people started to fix it. If multiple initial states satisfy the restriction described above, print any of them.

 
Input
The first line of the input is a single integer T (T≤700),
indicating the number of testcases.

Each of the following T lines
contains w0,w1,w2,b0,b1,b2.
It is guaranteed that 1≤w0,w1,w2,b0,b1,b2≤2000 and b0+b1+b2=w0+w1+w2.

It is also guaranteed that the sum of all the numbers in the input file is less than 300000.

 
Output
For each testcase, if there is no available solution, print −1.
Otherwise, print m in
the first line, indicating the total number of edges. Each of the next m lines
contains three integers x,y,t,
which means there is an edge colored t connecting
vertices x and y. t=0 means
this edge white, and t=1 means
this edge is black. Please be aware that this graph has no self-loop and no multiple edges. Please make sure that 1≤x,y≤b0+b1+b2.
 
Sample Input
2
1 1 1 1 1 1
1 2 2 1 2 2
 
Sample Output
-1
6
1 5 0
4 5 0
2 4 0
1 4 1
1 3 1
2 3 1
 
Source
 

题意:构造一个图使其满足:

白边:入度为0的数目为a[0],入度为1的数目为a[1],入度为2的数目为a[2],黑边同理。

思路:

入度为1的点必定为偶数,否则输出-1

点的总数:a[0] + a[1] + a[2]

边的总数 = 入度总数 /2  = (a[1]+b[1])/2 + a[2] + b[2]

入度为二的:(1,2)(2,3)(3,4).... 所以 a[2] >= 0

入度为一的: (1,2)(3,4).....    而且构造入度为二的链会有2个入度1,所以吧a[1] > 2

黑色边的处理需要间隔2的跳变(图中两点之间只有一条边)

//学习的别人的代码,把感觉能做题的都写一遍吧,当然那种代码长而且没法理解了,咳咳。。。  以后再说

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
int MAX=0x3f3f3f3f;
using namespace std;
const int INF = 0x7f7f7f;
const int MAXM = 12e4+5; int p[1000005];
int a[3],b[3]; int main()
{ int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d %d %d %d %d",&a[0],&a[1],&a[2],&b[0],&b[1],&b[2]);
int sum = 0;
for(int i = 0; i < 3; i++)
sum+=a[i]; if((a[1] & 1) || (b[1] & 1))
{
printf("-1\n");
continue;
} int n = (a[1]/2 + a[2]+b[1]/2 + b[2]); if(sum==4)
{
printf("4\n1 2 0\n1 3 0\n2 3 1\n3 4 1\n");
continue;
} printf("%d\n",n);
int t = 1;
while(a[2] >=0)
{
printf("%d %d 0\n",t,t+1);
t++;
a[2]--;
}
t++;
while(a[1] > 2)
{
printf("%d %d 0\n",t,t+1);
t+=2;
a[1]-=2;
}
int tmp = 0;
for(int i = 1;i <= sum ;i+=2) p[tmp++] = i;
for(int i = 2;i <= sum;i+=2) p[tmp++] = i; tmp = 0;
while(b[2] >= 0)
{
printf("%d %d 1\n",min(p[tmp],p[tmp+1]),max(p[tmp],p[tmp+1]));
tmp++;
b[2]--;
}
tmp ++;
while(b[1] > 2)
{
printf("%d %d 1\n",min(p[tmp],p[tmp+1]),max(p[tmp],p[tmp+1]));
tmp+=2;
b[1]-=2;
} }
return 0;
}

  

2015 多校联赛 ——HDU5302(构造)的更多相关文章

  1. 2015 多校联赛 ——HDU5302(矩阵快速幂)

    The Goddess Of The Moon Sample Input 2 10 50 12 1213 1212 1313231 12312413 12312 4123 1231 3 131 5 5 ...

  2. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  3. 2015 多校联赛 ——HDU5353(构造)

    Each soda has some candies in their hand. And they want to make the number of candies the same by do ...

  4. 2015 多校联赛 ——HDU5294(最短路,最小切割)

    Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  5. 2015 多校联赛 ——HDU5325(DFS)

    Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  6. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  7. 2015 多校联赛 ——HDU5323(搜索)

    Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. 2015 多校联赛 ——HDU5319(模拟)

    Painter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  9. 2015 多校联赛 ——HDU5301(技巧)

    Your current task is to make a ground plan for a residential building located in HZXJHS. So you must ...

随机推荐

  1. 201621123031 《Java程序设计》第7周学习总结

    作业07-Java GUI编程 1.本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 事件监听器: Java事件监听器是由事件类和监听接口组成,自定义 ...

  2. 2017 国庆湖南 Day6

    期望得分:100+100+60=260 实际得分:100+85+0=185 二分最后一条相交线段的位置 #include<cstdio> #include<iostream> ...

  3. MobileNet_v2

    研究动机: 神经网络彻底改变了机器智能的许多领域,实现了超人的准确性.然而,提高准确性的驱动力往往需要付出代价:现代先进网络需要高度计算资源,超出许多移动和嵌入式应用的能力. 主要贡献: 发明了一个新 ...

  4. MSSQL 2000 错误823恢复

    一.故障描述 MSSQL Server 2000 附加数据库错误823,附加数据库失败.数据库没有备份,不能通过备份恢复数据库,急需恢复数据库中的数据. 二.故障分析SQL Server数据库 823 ...

  5. JAVA_SE基础——72.自定义线程

    进程 :  正在执行的程序称作为一个进程.  进程负责了内存空间的划分.   问题: windows号称是多任务的操作系统,那么windows是同时运行多个应用程序吗?从宏观的角度: windows确 ...

  6. php的控制器链

    控制器之间协同工作就形成了控制器链· 比如在一个控制器的方法中,创建另外一个·控制器,创建对象,然后调用第二个控制器方法,那么在第一个控制器分配给视图的变量,在 第二个控制器的方法中对应的视图也是可以 ...

  7. 阿里云API网关(1)服务网关的产品概述

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  8. maven入门(1-2)settings.xml

    一.简介 settings.xml对于maven来说相当于全局性的配置,用于所有的项目, 当Maven运行过程中的各种配置,例如pom.xml,不想绑定到一个固定的project或者要分配给用户时, ...

  9. spring mvc跨域(post)--filter方案

    import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Http ...

  10. 相同域名不同端口的两个应用,cookie名字、路径都相同的情况下,会覆盖吗

    首先答案是: 会的. 本地测试流程: 两个相同的应用,代码完全相同:只是部署在两个不同的tomcat:域名都是localhost 应用A:部署在http://localhost:8087/ 应用B:部 ...