HDU 5302(Connect the Graph- 结构)

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


Problem Description
Once there was a special graph. This graph had nHDU 5302(Connect the Graph- 结构) 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 wHDU 5302(Connect the Graph- 结构)0HDU 5302(Connect the Graph- 结构)HDU 5302(Connect the Graph- 结构) vertices which are connected with no white edges, wHDU 5302(Connect the Graph- 结构)1HDU 5302(Connect the Graph- 结构)HDU 5302(Connect the Graph- 结构) vertices which are connected with 1HDU 5302(Connect the Graph- 结构) white edges, wHDU 5302(Connect the Graph- 结构)2HDU 5302(Connect the Graph- 结构)HDU 5302(Connect the Graph- 结构) vertices which are connected with 2HDU 5302(Connect the Graph- 结构) white edges, bHDU 5302(Connect the Graph- 结构)0HDU 5302(Connect the Graph- 结构)HDU 5302(Connect the Graph- 结构) vertices which are connected with no black edges, bHDU 5302(Connect the Graph- 结构)1HDU 5302(Connect the Graph- 结构)HDU 5302(Connect the Graph- 结构) vertices which are connected with 1HDU 5302(Connect the Graph- 结构) black edges and bHDU 5302(Connect the Graph- 结构)2HDU 5302(Connect the Graph- 结构)HDU 5302(Connect the Graph- 结构) vertices which are connected with 2HDU 5302(Connect the Graph- 结构) 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 (T700)HDU 5302(Connect the Graph- 结构), indicating the number of testcases.

Each of the following THDU 5302(Connect the Graph- 结构) lines contains wHDU 5302(Connect the Graph- 结构)0HDU 5302(Connect the Graph- 结构),wHDU 5302(Connect the Graph- 结构)1HDU 5302(Connect the Graph- 结构),wHDU 5302(Connect the Graph- 结构)2HDU 5302(Connect the Graph- 结构),bHDU 5302(Connect the Graph- 结构)0HDU 5302(Connect the Graph- 结构),bHDU 5302(Connect the Graph- 结构)1HDU 5302(Connect the Graph- 结构),bHDU 5302(Connect the Graph- 结构)2HDU 5302(Connect the Graph- 结构)HDU 5302(Connect the Graph- 结构). It is guaranteed that 1wHDU 5302(Connect the Graph- 结构)0HDU 5302(Connect the Graph- 结构),wHDU 5302(Connect the Graph- 结构)1HDU 5302(Connect the Graph- 结构),wHDU 5302(Connect the Graph- 结构)2HDU 5302(Connect the Graph- 结构),bHDU 5302(Connect the Graph- 结构)0HDU 5302(Connect the Graph- 结构),bHDU 5302(Connect the Graph- 结构)1HDU 5302(Connect the Graph- 结构),bHDU 5302(Connect the Graph- 结构)2HDU 5302(Connect the Graph- 结构)2000HDU 5302(Connect the Graph- 结构) and bHDU 5302(Connect the Graph- 结构)0HDU 5302(Connect the Graph- 结构)+bHDU 5302(Connect the Graph- 结构)1HDU 5302(Connect the Graph- 结构)+bHDU 5302(Connect the Graph- 结构)2HDU 5302(Connect the Graph- 结构)=wHDU 5302(Connect the Graph- 结构)0HDU 5302(Connect the Graph- 结构)+wHDU 5302(Connect the Graph- 结构)1HDU 5302(Connect the Graph- 结构)+wHDU 5302(Connect the Graph- 结构)2HDU 5302(Connect the Graph- 结构)HDU 5302(Connect the Graph- 结构).

It is also guaranteed that the sum of all the numbers in the input file is less than 300000HDU 5302(Connect the Graph- 结构).
 

Output
For each testcase, if there is no available solution, print 1HDU 5302(Connect the Graph- 结构). Otherwise, print mHDU 5302(Connect the Graph- 结构) in the first line, indicating the total number of edges. Each of the next mHDU 5302(Connect the Graph- 结构) lines contains three integers x,y,tHDU 5302(Connect the Graph- 结构), which means there is an edge colored tHDU 5302(Connect the Graph- 结构) connecting vertices xHDU 5302(Connect the Graph- 结构) and yHDU 5302(Connect the Graph- 结构). t=0HDU 5302(Connect the Graph- 结构) means this edge white, and t=1HDU 5302(Connect the Graph- 结构) means this edge is black. Please be aware that this graph has no self-loop and no multiple edges. Please make sure that 1x,ybHDU 5302(Connect the Graph- 结构)0HDU 5302(Connect the Graph- 结构)+bHDU 5302(Connect the Graph- 结构)1HDU 5302(Connect the Graph- 结构)+bHDU 5302(Connect the Graph- 结构)2HDU 5302(Connect the Graph- 结构)HDU 5302(Connect the Graph- 结构).
 

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
 

Author
XJZX
 

Source
2015 Multi-University Training Contest 2
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  5395 5394 5393 5392 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;
}





版权声明:本文为博主原创文章,未经博主允许不得转载。