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;
int inadj[210][2], outadj[210][2], n, h;
int inadj2[210][2], outadj2[210][2];
pair<int, int> thing(int a, int b)
{
	pair<int, int> ans;
	if (b) ans = { b, 0 };
	else ans = { a, 1 };
	return ans;
}
vector<pair<int, int> > adj[210][2];
int seen[210][2], c;
vector<pair<int, int> > components[500];
void dfs(int a, int b, int d)
{
	if (seen[a][b]) return;
	seen[a][b] = 1;
	components[d].push_back({ a, b });
	for (auto c : adj[a][b]) dfs(c.first, c.second, d);
}

int main()
{
	scanf("%d%d", &n, &h);
	for (int i = 0; i < n; i++)
	{
		int a, b, c, d;
		scanf("%d%d%d%d", &a, &b, &c, &d);
		pair<int, int> in, out;
		in = thing(a, c);
		out = thing(b, d);
		out.second = !out.second;
//		printf("%d %d -> %d %d\n", in.first, in.second, out.first, out.second);
		outadj[in.first][in.second]++;
		inadj[out.first][out.second]++;
		adj[in.first][in.second].push_back(out);
		adj[out.first][out.second].push_back(in);
	}
	for (int i = 1; i <= h; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			if (adj[i][j].size() && !seen[i][j]) dfs(i, j, c++);
		//	printf("%d:%d - in %d, out %d\n", i, j, inadj[i][j], outadj[i][j]);
		}
	}
	bool works = 1;
	for (int i = 0; i < c; i++)
	{
		for (int i = 0; i <= h; i++) inadj2[i][0] = inadj2[i][1] = outadj2[i][0] = outadj2[i][1] = 0;
		for (auto b : components[i])
		{
			inadj2[b.first][b.second] += inadj[b.first][b.second];
			outadj2[b.first][b.second] += outadj[b.first][b.second];
		}
		for (int i = 0; i <= h; i++)
		{
			//printf("%d %d - %d %d\n", inadj2[i][0], outadj2[i][0], inadj2[i][1], outadj2[i][1]);
			if (inadj2[i][0] - outadj2[i][0] > 0 && inadj2[i][1] - outadj2[i][1] < 0) 
			{
			//	printf("%d\n", i);
				inadj2[i][0]--; 
				outadj2[i][1]--;
			//	printf(" %d %d - %d %d\n", inadj2[i][0], outadj2[i][0], inadj2[i][1], outadj2[i][1]);
			}
		}
		int extra = 0;
		for (int i = 0; i <= h; i++)
		{
			for (int j = 0; j < 2; j++)
			{
		//		printf("  %d\n", abs(inadj2[i][j] - outadj2[i][j]));
				if (abs(inadj2[i][j] - outadj2[i][j]) == 1) extra++;
				else if (abs(inadj2[i][j] - outadj2[i][j])) extra += 10;
			}
		}
		if (extra != 2) works = 0;
	}
//	printf("Components %d\n", c);
	if (works) printf("YES\n");
	else printf("NO\n");
}