标签:
题意:对于一幅无向图,有黑白边连接,连接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];
#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; }
hdu 5302 Connect the Graph(构造)
标签:
原文地址:http://www.cnblogs.com/dominating/p/4676239.html