Problem:
Score : 1200 points

Problem Statement

We have N irregular jigsaw pieces. Each piece is composed of three rectangular parts of width 1 and various heights joined together. More specifically:

The i-th piece is a part of height H, with another part of height A_i joined to the left, and yet another part of height B_i joined to the right, as shown below. Here, the bottom sides of the left and right parts are respectively at C_i and D_i units length above the bottom side of the center part.

Snuke is arranging these pieces on a square table of side 10^{100}. Here, the following conditions must be held:

All pieces must be put on the table.

The entire bottom side of the center part of each piece must touch the front side of the table.

The entire bottom side of the non-center parts of each piece must either touch the front side of the table, or touch the top side of a part of some other piece.

The pieces must not be rotated or flipped.

Determine whether such an arrangement is possible.

Constraints

1 \leq N \leq 100000

1 \leq H \leq 200

1 \leq A_i \leq H

1 \leq B_i \leq H

0 \leq C_i \leq H - A_i

0 \leq D_i \leq H - B_i

All input values are integers.

Input

Input is given from Standard Input in the following format:

N H
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_N B_N C_N D_N

Output

If it is possible to arrange the pieces under the conditions, print YES; if it is impossible, print NO.

Sample Input 1

3 4
1 1 0 0
2 2 0 1
3 3 1 0

Sample Output 1

YES

The figure below shows a possible arrangement.

Sample Input 2

4 2
1 1 0 1
1 1 0 1
1 1 0 1
1 1 0 1

Sample Output 2

NO

Sample Input 3

10 4
1 1 0 3
2 3 2 0
1 2 3 0
2 1 0 0
3 2 0 2
1 1 3 0
3 2 0 0
1 3 2 0
1 1 1 3
2 3 0 0

Sample Output 3

YES

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;

typedef pair <int, int> pii;

vector <pii> I[101010], O[101010];
bool chk[222];
int n, h;

int dfs(int p)
{
	int ret = O[p].size();
	
	chk[p] = 1;
	
	for(pii t: O[p]){
		if(!chk[t.first]){
			ret += dfs(t.first);
		}
	}
	
	return ret;
}

int main()
{
	int i, a, b, c, d, s1, s2, t1, t2, f, f1, f2;
	scanf("%d%d", &n, &h);
	
	for(i=1;i<=n;i++){
		scanf("%d%d%d%d", &a, &b, &c, &d);
		
		f = 0;
		if(c) a = c;
		else f |= 1;
		if(d) b = d;
		else f |= 2;
		
		I[b].push_back(pii(a, f));
		O[a].push_back(pii(b, f));
	}
	
	if(n == 1){
		if(f == 3) printf("YES\n");
		else printf("NO\n");
		
		return 0;
	}
	
	s1 = s2 = 0;
	
	for(i=1;i<h;i++){
		if(I[i].size() + 1 == O[i].size()) s1 ++, t1 = i;
		else if(I[i].size() == O[i].size() + 1) s2 ++, t2 = i;
		else if(I[i].size() != O[i].size()){
			printf("NO\n");
			return 0;
		}
	}
	
	for(i=1;i<h;i++) chk[i] = 0;
	
	if(s1 == 0 && s2 == 0 && dfs(1) == n){
		for(i=1;i<h;i++){
			f1 = f2 = 0;
			for(pii j: O[i]) if(j.second & 1) f1 ++;
			for(pii j: I[i]) if(j.second & 2) f2 ++;
			if(f1 && f2){
				if(f1 > 1 || f2 > 1){
					printf("YES\n");
					return 0;
				}
				else if(f1 == 1 && f2 == 1){
					for(pii j: O[i]){
						if(j.second == 3 && j.first == i){
							printf("NO\n");
							return 0;
						}
					}
					
					printf("YES\n");
					return 0;
				}
			}
		}
	}
	
	for(i=1;i<h;i++) chk[i] = 0;
	
	if(s1 == 1 && s2 == 1 && dfs(t1) == n){
		f1 = f2 = 0;
		for(pii j: O[t1]) if(j.second & 1) f1 ++;
		for(pii j: I[t2]) if(j.second & 2) f2 ++;
		if(f1 && f2){
			if(f1 > 1 || f2 > 1){
				printf("YES\n");
				return 0;
			}
			else if(f1 == 1 && f2 == 1){
				for(pii j: O[t1]){
					if(j.second == 3 && j.first == t2){
						printf("NO\n");
						return 0;
					}
				}
				
				printf("YES\n");
				return 0;
			}
		}
	}
	
	printf("NO\n");
	
	return 0;
}