hdu 5302 Connect the Graph(构造)

题意:对于一幅无向图,有黑白边连接,连接i条白边的点数为a[0],a[1],a[2],连接i条黑边的点数为b[0],b[1],b[2],

        a[0]+a[1]+a[2]=b[0]+b[1]+b[2],求该无向图的边数及各边情况;

思路:构造法;若连接边数为1的点为奇数,则该图不存在;

        图的总点数为a[0]+a[1]+a[2];总边数为a[1]/2+a[2]+b[1]/2+b[2];

        构造两条边的点时,连续枚举,如(1,2),(2,3);

        构造一条边的点时,间隔枚举,如(1,2),(3,4);

        另一种颜色的边按相同的方法,在已经枚举的点中构造;

        总点数为4时,由于间隔枚举会产生问题,所以需要特判;

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int t,n,m,sum;
int a[4],b[4];
int shu[500100];
int main()
{
    int i,j,k,temp,temp2,num,cnt;
    scanf("%d",&t);
    while(t--){
       scanf("%d%d%d%d%d%d",&a[0],&a[1],&a[2],&b[0],&b[1],&b[2]);
       sum=0;cnt=0;
       if(a[1]%2==1||b[1]%2==1){
           printf("-1\n");
           continue;
       }
       sum=a[0]+a[1]+a[2];
       num=a[1]/2+a[2]+b[1]/2+b[2];
       printf("%d\n",num);
       if(num==4){
           printf("1 2 0\n1 3 0\n2 3 1\n3 4 1\n");
           continue;
       }
       temp=1;
       while(a[2]!=-1) {printf("%d %d 0\n",temp,temp+1);a[2]--;temp++;}temp++;
       while(a[1]!=2) {printf("%d %d 0\n",temp,temp+1);a[1]-=2;temp+=2;}
       for(i=1;i<=sum;i+=2) shu[cnt++]=i;
       for(i=2;i<=sum;i+=2) shu[cnt++]=i;
       temp2=0;
       while(b[2]!=-1) {printf("%d %d 1\n",min(shu[temp2],shu[temp2+1]),max(shu[temp2],shu[temp2+1]));b[2]--;temp2++;}temp2++;
       while(b[1]!=2) {printf("%d %d 1\n",min(shu[temp2],shu[temp2+1]),max(shu[temp2],shu[temp2+1]));b[1]-=2;temp2+=2;}
    }
    return 0;
}

 

posted @ 2015-07-25 17:14  dominating  Views(221)  Comments(0)    收藏  举报