HDU 5302(Connect the Graph- 构造)
Connect the Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 456 Accepted Submission(s): 144
Special Judge
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
w
vertices which are connected with no white edges,
w
vertices which are connected with 1
white edges, w
vertices which are connected with 2
white edges, b
vertices which are connected with no black edges,
b
vertices which are connected with 1
black edges and b
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.
T (T≤700)
indicating the number of testcases.
Each of the following T
lines contains w
It is guaranteed that 1≤w
and b
It is also guaranteed that the sum of all the numbers in the input file is less than
300000
−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≤b
2
1 1 1 1 1 1
1 2 2 1 2 2
-1
6
1 5 0
4 5 0
2 4 0
1 4 1
1 3 1
2 3 1
pid=5395" target="_blank">5395
5394pid=5393" target="_blank">5393
pid=5392" target="_blank">5392
pid=5391" target="_blank">5391
构造法:
首先保证度数之和为偶数,即w1=b1=1 ,否则无解
又w0,w1,w2,b0,b1,b2均为正数 故
当n=4时,仅仅有1种情况 1 2 1 不是无解
当n≥4时,先构造2个环分别为白环,黑环
对于奇数n:
白环 1 2 3 ... n
黑环 1 3 5 ... n 2 4 6 ... n-1
对于偶数n:
白环 1 2 3 ... n
黑环 1 3 5 ... n-1 2 n n-2 n-4 ... 4
此时,对于每一个环而言,构造答案
1-2-2-...-2-2-1
1-1 1-1 .. 1-1
1-1 0 .. 0
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXD (2000+10)
#define MAXN (6000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int a2[MAXN],a1[MAXN],n;
void calc(int *a,int n0,int n1,int n2,int p)
{
int i=1;
if (n1==0&&n2==0) return;
For(i,n2+1)
{
printf("%d %d %d\n",a[i],a[i+1],p);
}
n1-=2;
for(int i=n2+3,j=1;j<=n1;i+=2,j+=2) printf("%d %d %d\n",a[i],a[i+1],p); }
int main()
{
// freopen("C.in","r",stdin);
// freopen(".out","w",stdout); int T; cin>>T;
while(T--) {
int w0,w1,w2,b0,b1,b2;
scanf("%d%d%d%d%d%d",&w0,&w1,&w2,&b0,&b1,&b2);
n=w0+w1+w2; //特判
if ((w1&1)||(b1&1)) { printf("-1\n");continue;} int m=(w1+2*w2+b1+2*b2)/2; if (n==4)
{
puts("4\n1 2 0\n1 3 0\n2 3 1\n3 4 1");
continue;
}
else if (n>4) {
For(i,n) a1[i]=i;
if (n%2==0)
{
for(int i=1,j=1;i<=n/2;i++,j+=2) a2[i]=j;
for(int i=n/2+1,j=2;i<=n;i++,j+=2) a2[i]=j;
a2[n+1]=1;
}
else {
for(int i=1,j=1;i<=n/2+1;i++,j+=2) a2[i]=j;
a2[n/2+2]=2;
for(int i=n/2+3,j=n-1;i<=n;i++,j-=2) a2[i]=j;
a2[n+1]=1;
}
cout<<m<<endl;
calc(a1,w0,w1,w2,0);
calc(a2,b0,b1,b2,1);
} } return 0;
}
HDU 5302(Connect the Graph- 构造)的更多相关文章
- [2015hdu多校联赛补题]hdu5302 Connect the Graph
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5302 题意:给你一个无向图,它的边要么是黑色要么是白色,且图上的每个点最多与两个黑边两个白边相连.现在 ...
- HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)
6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...
- HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- HDU - 6313 Hack It(构造)
http://acm.hdu.edu.cn/showproblem.php?pid=6313 题意 让你构造一个矩阵使得里面不存在四个顶点都为1的矩形,并且矩阵里面1的个数要>=85000 分析 ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- HDU 6343 - Problem L. Graph Theory Homework - [(伪装成图论题的)简单数学题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
随机推荐
- MySQL本地登录及数据库导入导出
注意:本地MySQL服务要开启 更新整个数据库 1.将正式服务器上的数据库做备份 登录到正式服务器,执行如下命令:(注意空格) mysqldump -uroot –p密码 数据库名 -P 接口 --d ...
- JavaScript中函数的调用
JavaScript中函数的调用 制作人:全心全意 在JavaScript中,函数定义后并不会自动执行,要执行一个函数需要在特定的位置调用该函数,调用函数需要创建调用语句,调用语句包含函数名称和参数. ...
- 高可用技术之keepalived原理简单了解
Keepalived 工作原理 keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议. 虚拟路由冗余协 ...
- 剑指Offer(书):二维数组中的查找
题目:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. ...
- POJ 3259 Wormholes(负权环路)
题意: 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地,M条路连接着两块地,W个虫洞,连接两块地的路是双向的,而虫洞是单 ...
- 【C#】【数据结构】006-栈:链栈
C#数据结构:链栈 1.自定义链栈结构: 链栈节点类 using System.Collections; using System.Collections.Generic; using UnityEn ...
- javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean (蛋疼死我了)
1为抛出异常原因,2为异常解决方法. 原因: 进入spring:bind标签源码你可以看到 Object target = requestContext.getModelObject(beanNa ...
- XV6锁
锁 xv6 运行在多处理器上,即计算机上有多个单独执行代码的 CPU.这些 CPU 操作同一片地址空间并分享其中的数据结构:xv6 必须建立一种合作机制防止它们互相干扰.即使是在单个处理器上,xv6 ...
- javascript、jquery 、C#、sqlserveer、mysql、oracle中字符串截取的区别和用法
下标从0开始 ,并且包括起始位 javascript 中字符串截取 : substring(Number start,Number end) var substr = "liuguangfa ...
- BigTable
Bigtable发布于2006年,启发了无数的NoSQL数据库,比如:Cassandra.HBase等等. Cassandra架构中有一半是模仿Bigtable,包括了数据模型.SSTables以及提 ...