constructive algorithms implementation trees *1900

Please click on ads to support us..

Python Code:

t = int(input())
for _ in range(t):
    arr = input().strip().split()
    n, x, y, z = [int(i) for i in arr]
    v1, v2, v3 = 1, 2, 3
    if x > y:
        v1, v3 = v3, v1
        x, y = y, x
    if y > z:
        v1, v2 = v2, v1
        y, z = z, y
    if x > y:
        v1, v3 = v3, v1
        x, y = y, x
    if x + y < z:
        print("NO")
    elif (x + y - z) % 2 != 0:
        print("NO")
    else:
                                        
        lists = []
        t = (y + z - x) // 2
        k = (x + y - z) // 2

        if  x + t + 1 > n:
            print("NO")
        else:
            print("YES")
            last_used_point = 3

            if k == 0:
                pred_point = v2
                for i in range(x):
                    if i != x - 1:
                        last_used_point += 1
                        lists.append([pred_point, last_used_point])
                        pred_point = last_used_point
                    else:
                        lists.append([pred_point, v1])
                pred_point = v2
                for i in range(y):
                    if i != y - 1:
                        last_used_point += 1
                        lists.append([pred_point, last_used_point])
                        pred_point = last_used_point
                    else:
                        lists.append([pred_point, v3])
            else:
                traffic_point = 4
                last_used_point = 4

                pred_point = v2
                for i in range(k):
                    if i != k - 1:
                        last_used_point += 1
                        lists.append([pred_point, last_used_point])
                        pred_point = last_used_point
                    else:
                        lists.append([pred_point, traffic_point])

                pred_point = traffic_point
                for i in range(x - k):
                    if i != x - k - 1:
                        last_used_point += 1
                        lists.append([pred_point, last_used_point])
                        pred_point = last_used_point
                    else:
                        lists.append([pred_point, v1])

                pred_point = traffic_point
                for i in range(t):
                    if i != t - 1:
                        last_used_point += 1
                        lists.append([pred_point, last_used_point])
                        pred_point = last_used_point
                    else:
                        lists.append([pred_point, v3])

            for i in range(last_used_point+1, n+1):
                lists.append([1, i])

            for edge in lists:
                print(edge[0], edge[1])


C++ Code:

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
int T,n,now,d[5],a[5];
void print(int u,int v,int dis){
	if(u==v)return;
	vector<int> V;V.pb(u);
	--dis;
	while(dis--)V.pb(++now);
	V.pb(v);
	for(int i=0;i<V.size()-1;i++)printf("%d %d\n",V[i],V[i+1]);
	return;
}
int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d%d",&n,&d[1],&d[2],&d[3]);
		int s=d[1]+d[2]+d[3];
		if(s&1){puts("NO");continue;}
		s>>=1;
		a[1]=s-d[2],a[2]=s-d[3],a[3]=s-d[1];
		if(a[1]<0||a[2]<0||a[3]<0||a[1]+a[2]+a[3]+1>n){puts("NO");continue;}
		puts("YES");
		now=3;int rt=0;
		for(int i=1;i<=3;i++)
			if(!a[i]){rt=i;break;}
		if(!rt)rt=++now;
		for(int i=1;i<=3;i++)print(rt,i,a[i]);
		for(++now;now<=n;now++)printf("1 %d\n",now);
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable