Problem:
Problem D:
Futon
The sales department of Japanese Ancient Giant Corp. is visiting a hot spring resort for their
recreational trip. For deepening their friendships, they are staying in one large room of a
Japanese-style hotel called a ryokan.
In the ryokan, people sleep in Japanese-style beds called futons. They all have put their futons
on the floor just as they like. Now they are ready for sleeping but they have one concern: they
don’t like to go into their futons with their legs toward heads — this is regarded as a bad custom
in Japanese tradition. However, it is not obvious whether they can follow a good custom. You
are requested to write a program answering their question, as a talented programmer.
Here let's model the situation. The room is considered to be a grid on an
xy
-plane. As usual,
x
-axis points toward right and
y
-axis points toward up. Each futon occupies two adjacent cells.
People put their pillows on either of the two cells. Their heads come to the pillows; their foots
come to the other cells. If the cell of some person's foot becomes adjacent to the cell of another
person's head, regardless their directions, then it is considered as a bad case. Otherwise people
are all right.
Input
The input is a sequence of datasets. Each dataset is given in the following format:
n
x
1
y
1
dir
1
...
x
n
y
n
dir
n
n
is the number of futons (1 ≤
n
≤ 20,000); (
x
i
,
y
i
) denotes the coordinates of the left-bottom
corner of the
i
-th futon;
dir
i
is either '
x
' or  '
y
' and denotes the direction of the
i
-th futon,
where '
x
' means the futon is put horizontally and '
y
' means vertically. All coordinate values are
non-negative integers not greater than 10
9
.
It is guaranteed that no two futons in the input overlap each other.
The input is terminated by a line with a single zero. This is not part of any dataset and thus
should not be processed.
Output
For each dataset, print "
Yes
" in a line if it is possible to avoid a bad case, or "
No
" otherwise.
Sample Input
4
0 0 x
2 0 x
0 1 x
2 1 x
4
1 0 x
0 1 x
2 1 x
1 2 x
4
0 0 x
2 0 y
0 1 y
1 2 x
0
Output for the Sample Input
Yes
No
Yes

Submitted solutions:

code_snippet: #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;

#define pb push_back
#define fs first
#define sc second
#define openfile {freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);}
#define debug 0

struct cell
{
	int x, y;
	int vt;
};

struct futon
{
	cell a, b;
};

const int MAXN = 20005;
const int mov[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};

int col[MAXN];
vector <int> adj[MAXN];
futon a[MAXN];
cell b[MAXN * 2];
int n, m;
bool ok;

void rset()
{
	m = 0;
	for (int i = 1; i <= n; i++)
		adj[i].clear();
	memset(col, 0, sizeof col);
}

bool mycmp(const cell &a, const cell &b)
{
	return a.x < b.x || (a.x == b.x && a.y < b.y);
}

int bs2(int trai, int phai, int y)
{
	int L = trai, R = phai, mid;
	while (L + 1 < R)
	{
		mid = (L + R) / 2;
		if (b[mid].y < y) L = mid;
		else R = mid;
	}
	if (b[L].y == y) return b[L].vt;
	else if (b[R].y == y) return b[R].vt;
	return -1;
}

int bs1(int x, int y)
{
	int trai, phai;

	int L = 1, R = m, mid;
	while (L + 1 < R)
	{
		mid = (L + R) / 2;
		if (b[mid].x < x) L = mid;
		else R = mid;
	}
	if (b[L].x == x) trai = L;
	else if (b[R].x == x) trai = R;
	else return -1;

	L = 1, R = m;
	while (L + 1 < R)
	{
		mid = (L + R) / 2;
		if (b[mid].x > x) R = mid;
		else L = mid;
	}
	if (b[R].x == x) phai = R;
	else if (b[L].x == x) phai = L;
	else return -1;

	return bs2(trai, phai, y);
}

void init()
{
	sort(b+1, b+m+1, mycmp);
	for (int i = 1; i <= m; i++)
	for (int j = 0; j < 4; j++)
	{
		int x = b[i].x + mov[j][0], y = b[i].y + mov[j][1];
		int k = bs1(x, y);
		if (k == -1) continue;
		if (k != b[i].vt) adj[b[i].vt].pb(k), adj[k].pb(b[i].vt);
	}
}

bool isKe(cell a, cell b)
{
	for (int i = 0; i < 4; i++)
	{
		int x = a.x + mov[i][0], y = a.y + mov[i][1];
		if (x == b.x && y == b.y) return true;
	}
	return false;
}

void dfs(int u)
{
	for (int i = 0; i < (int)adj[u].size() && ok; i++)
	{
		int v = adj[u][i];
		if (!col[v])
		{
			if (isKe(a[v].a, a[u].a))
			{
				if (col[u] == 1) col[v] = 1;
				else col[v] = 2;
			}
			else if (isKe(a[v].a,a[u].b))
			{
				if (col[u] == 1) col[v] = 2;
				else col[v] = 1;
			}
			else if (isKe(a[v].b,a[u].a))
			{
				if (col[u] == 1) col[v] = 2;
				else col[v] = 1;
			}
			else
			{
				if (col[u] == 1) col[v] = 1;
				else col[v] = 2;
			}
			dfs(v);
		}
		else
		{
			if (isKe(a[v].a, a[u].a))
			{
				if (col[u] != col[v]) { ok = false; return; }
			}
			else if (isKe(a[v].a,a[u].b))
			{
				if (col[u] == col[v]) { ok = false; return; }
			}
			else if (isKe(a[v].b,a[u].a))
			{
				if (col[u] == col[v]) { ok = false; return; }
			}
			else
			{
				if (col[u] != col[v]) { ok = false; return; }
			}
		}
	}
}

void solve()
{
	ok = true;
	for (int i = 1; i <= n; i++)
	{
		if (col[i]) continue;

		col[i] = 1;
		dfs(i);
		if (ok) continue;

		col[i] = 2;
		dfs(i);
		if (!ok)
		{
			puts("No");
			return;
		}
	}
	puts("Yes");
}

int main()
{
   if (debug) openfile;

   while (scanf("%d", &n) && n)
	{
		rset();
		for (int i = 1; i <= n; i++)
		{
			scanf("%d%d", &a[i].a.x, &a[i].a.y);
			b[++m] = a[i].a;
			b[m].vt = i;
			char ch;
			scanf("%c%c", &ch, &ch);
			if (ch == 'x')
			{
				a[i].b.x = a[i].a.x + 1;
				a[i].b.y = a[i].a.y;
			}
			else
			{
				a[i].b.x = a[i].a.x;
				a[i].b.y = a[i].a.y + 1;
			}
			b[++m] = a[i].b;
			b[m].vt = i;
			scanf("%c", &ch);
		}
		init();
		solve();
	}

   return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 7344
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
#define X first
#define Y second
signed main(){
  int n;
  while(cin>>n,n){
    P p[n];
    char d[n];
    for(int i=0;i<n;i++) cin>>p[i].X>>p[i].Y>>d[i];
    map<P,int> m;
    for(int i=0;i<n;i++) m[p[i]]=i;
    int used[n],visit[n];
    memset(used,0,sizeof(used));
    bool f=1;

    int ax1[]={-1, 0, 1, 2,-2,-1, 0, 1};
    int ay1[]={-1,-1,-1, 0, 0, 1, 1, 1};
    int ad1[]={ 1, 0, 1, 1, 1, 1, 0, 1};
    
    int ax2[]={-1, 0, 1, 2, 2, 1, 0,-1};
    int ay2[]={ 0, 1, 1, 0,-1,-2,-2,-1};
    int ad2[]={ 0, 0, 1, 1, 0, 0, 1, 1};
    
    int ax3[]={ 0, 1, 1, 1, 0,-1,-1,-1};
    int ay3[]={ 2, 1, 0,-1,-2,-1, 0, 1};
    int ad3[]={ 1, 1, 0, 1, 1, 1, 0, 1};
    
    int ax4[]={-1, 0, 1, 1, 0,-1,-2,-2};
    int ay4[]={-1,-1, 0, 1, 2, 2, 1, 0};
    int ad4[]={ 1, 0, 0, 1, 1, 0, 0, 1};
    
    for(int i=0;i<n;i++){
      if(used[i]) continue;
      bool ff=0;
      for(int k=0;k<2;k++){
	bool fff=1;
	
	memset(visit,-1,sizeof(visit));
	queue<int> q;
	q.push(i);
	visit[i]=k;
	
	while(!q.empty()){
	  int t=q.front();q.pop();
	  //cout<<t<<" "<<visit[t]<<endl;
	  if(d[t]=='x'){
	    for(int j=0;j<8;j++){
	      int nx=p[t].X+ax1[j],ny=p[t].Y+ay1[j],nv=visit[t];
	      if(!m.count(P(nx,ny))) continue;
	      int u=m[P(nx,ny)];
	      if(d[u]=='y') continue;
	      //cout<<"a1:"<<u<<endl;
	      if(ad1[j]) nv=!nv;
	      if(~visit[u]){
		if(visit[u]!=nv){
		  fff=0;
		  goto END;
		}
	      }else{
		visit[u]=nv;
		q.push(u);
	      }
	    }
	    for(int j=0;j<8;j++){
	      int nx=p[t].X+ax2[j],ny=p[t].Y+ay2[j],nv=visit[t];
	      if(!m.count(P(nx,ny))) continue;
	      int u=m[P(nx,ny)];
	      if(d[u]=='x') continue;
	      //cout<<"a2:"<<u<<endl;
	      if(ad2[j]) nv=!nv;
	      if(~visit[u]){
		if(visit[u]!=nv){
		  fff=0;
		  goto END;
		}
	      }else{
		visit[u]=nv;
		q.push(u);
	      }
	    }
	  }
	  
	  if(d[t]=='y'){
	    for(int j=0;j<8;j++){
	      int nx=p[t].X+ax3[j],ny=p[t].Y+ay3[j],nv=visit[t];
	      if(!m.count(P(nx,ny))) continue;
	      int u=m[P(nx,ny)];
	      if(d[u]=='x') continue;
	      //cout<<"a3:"<<u<<endl;
	      if(ad3[j]) nv=!nv;
	      if(~visit[u]){
		if(visit[u]!=nv){
		  fff=0;
		  goto END;
		}
	      }else{
		visit[u]=nv;
		q.push(u);
	      }
	    }
	    for(int j=0;j<8;j++){
	      int nx=p[t].X+ax4[j],ny=p[t].Y+ay4[j],nv=visit[t];
	      if(!m.count(P(nx,ny))) continue;
	      int u=m[P(nx,ny)];
	      if(d[u]=='y') continue;
	      //cout<<"a4:"<<u<<endl;
	      if(ad4[j]) nv=!nv;
	      if(~visit[u]){
		if(visit[u]!=nv){
		  fff=0;
		  goto END;
		}
	      }else{
		visit[u]=nv;
		q.push(u);
	      }
	    }
	  }
	}
	
      END:

	//cout<<"fff:"<<fff<<endl;
	
	if(fff){
	  for(int j=0;j<n;j++) if(~visit[j]) used[j]=1;
	  ff=1;
	  break;
	}
      }
      if(!ff){
	f=0;
	break;
      }
    }
    cout<<(f?"Yes":"No")<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 850
memory_kb: 5076
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,s,t) for(int i=s;i<t;i++)

using vi = vector<int>;
using vvi=vector<vi>;

struct SCC {
  int n;
  vi cmp, order, use;
  vvi g, rg, group;

  SCC(int n, vector<pair<int, int>> edge)
      : n(n), g(n), rg(n), cmp(n, -1), use(n) {
    for(auto [frm, to] : edge) {
      g[frm].push_back(to);
      rg[to].push_back(frm);
    }
    rep(i, n) dfs(i);
    int c = 0;
    rep(i, n) c += rdfs(order[n - 1 - i], c);
    group.resize(c);
    rep(i, n) group[cmp[i]].push_back(i);
  }

  void dfs(int v) {
    if(use[v]) return;
    use[v] = 1;
    for(auto to : g[v]) dfs(to);
    order.push_back(v);
  }

  int rdfs(int v, int c) {
    if(cmp[v] != -1) return 0;
    cmp[v] = c;
    for(auto to : rg[v]) rdfs(to, c);
    return 1;
  }
};

struct two_sat {
  int n;
  vector<pair<int, int>> edge;

  two_sat(int n) : n(n) {}

  void add_clause(int i, int f, int j, int g) {
    edge.push_back({2 * i + (1 - f), 2 * j + g});
    edge.push_back({2 * j + (1 - g), 2 * i + f});
  }

  vi calc() {
    SCC scc(2 * n, edge);
    vvi group = scc.group;
    vi res(2 * n);
    rep(i, group.size()) {
      for(auto j : group[i]) res[j] = i;
    }
    vi ans(n);
    rep(i, n) {
      if(res[2 * i] == res[2 * i + 1]) return {};
      ans[i] = (res[2 * i] < res[2 * i + 1]) ? 1 : 0;
    }
    return ans;
  }
};

vi dx={1,0,-1,0},dy={0,1,0,-1};
void solve(int N){
    two_sat T(N);
    map<array<int,2>,array<int,2>> M;
    rep(i,N){
        int x,y;
        cin>>x>>y;
        M[{x,y}]={i,0};
        char c;
        cin>>c;
        if(c=='x')M[{x+1,y}]={i,1};
        else M[{x,y+1}]={i,1};

        rep(d,4){

            int w=x+dx[d];
            int h=y+dy[d];
            if(M.count({w,h})){
                if(M[{w,h}][0]!=i){
                    T.add_clause(i,0,M[{w,h}][0],1-M[{w,h}][1]);
                    T.add_clause(i,1,M[{w,h}][0],M[{w,h}][1]);
                }
            }
            if(c=='x')w++;
            else h++;
            if(M.count({w,h})){
                if(M[{w,h}][0]==i)continue;
                T.add_clause(i,1,M[{w,h}][0],1-M[{w,h}][1]);
                T.add_clause(i,0,M[{w,h}][0],M[{w,h}][1]);
            }
        }
    }
    auto res=T.calc();
    if(res.size()>0)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N;
    while(cin>>N,N!=0)solve(N);

}
accuracy: 1
time_ms: 110
memory_kb: 13868
final_rank: 16

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

bool Solve(int N) {
    vector<int> X(N*2), Y(N*2);
    map<pair<int,int>,int> mp;
    rep(i,0,N) {
        cin >> X[i*2] >> Y[i*2];
        char C;
        cin >> C;
        if (C == 'x') X[i*2+1] = X[i*2]+1, Y[i*2+1] = Y[i*2];
        else X[i*2+1] = X[i*2], Y[i*2+1] = Y[i*2]+1;
    }
    rep(i,0,N*2) mp[{X[i],Y[i]}] = i;
    vector<int> ANS(N*2,-1);
    int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
    rep(i,0,N*2) {
        if (ANS[i] >= 0) continue;
        queue<int> Q;
        ANS[i] = 0;
        Q.push(i);
        while(!Q.empty()) {
            int ID = Q.front();
            Q.pop();
            int CX = X[ID], CY = Y[ID];
            rep(j,0,4) {
                int NX = CX + dx[j], NY = CY + dy[j];
                if (!mp.count({NX,NY})) continue;
                int NID = mp[{NX,NY}];
                if (ID/2 == NID/2) {
                    if (ANS[NID] == -1) {
                        ANS[NID] = ANS[ID] ^ 1;
                        Q.push(NID);
                    }
                    else {
                        if (ANS[ID] == ANS[NID]) return false;
                    }
                }
                else {
                    if (ANS[NID] == -1) {
                        ANS[NID] = ANS[ID];
                        Q.push(NID);
                    }
                    else {
                        if (ANS[ID] != ANS[NID]) return false;
                    }
                }
            }
        }
    }
    return true;
}

int main() {
while(1) {
    int N;
    cin >> N;
    if (N == 0) return 0;
    cout << (Solve(N) ? "Yes" : "No") << endl;
}
}
accuracy: 1
time_ms: 130
memory_kb: 6100
final_rank: 2

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

struct scc_graph {
    using size_type = int;
    size_type n;
    std::vector<std::vector<size_type>> g;

    size_type group_num;
    std::vector<size_type> ids;
    std::vector<std::vector<size_type>> scc;
    std::vector<std::vector<size_type>> dag;

    scc_graph(const size_type n) : n(n), g(n), group_num(0), ids(n) {}

    void add_edge(const size_type from, const size_type to) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].push_back(to);
    }
    void add_vertex(const size_type k) {
        assert(0 <= k);
        g.resize(n + k);
        ids.resize(n + k);
        n = n + k;
    }

    void build() {
        int now_ord = 0;
        std::vector<size_type> visited, low(n), ord(n, -1);
        visited.reserve(n);
        auto dfs = [&](auto self, size_type v) -> void {
            low[v] = ord[v] = now_ord++;
            visited.push_back(v);
            for(size_type to : g[v]) {
                if(ord[to] == -1) {
                    self(self, to);
                    low[v] = std::min(low[v], low[to]);
                } else {
                    low[v] = std::min(low[v], ord[to]);
                }
            }
            if(low[v] == ord[v]) {
                while(true) {
                    size_type u = visited.back(); visited.pop_back();
                    ord[u] = n;
                    ids[u] = group_num;
                    if(u == v) break;
                }
                group_num++;
            }
        };
        for(size_type i = 0; i < n; i++) if(ord[i] == -1) dfs(dfs, i);
        for(size_type& x : ids) x = group_num - 1 - x;

        scc.resize(group_num);
        for(size_type i = 0; i < n; i++) scc[ids[i]].push_back(i);

        dag.resize(group_num);
        for(size_type from = 0; from < n; from++) {
            for(size_type to : g[from]) {
                const size_type from_id = ids[from];
                const size_type to_id = ids[to];
                if(from_id != to_id) dag[from_id].push_back(to_id);
            }
        }
    }
};

struct two_sat {
    using size_type = int;
    size_type n;
    scc_graph scc;
    std::vector<bool> answer;
    two_sat(const size_type n) : n(n), scc(n + n), answer(n) {}

    void add_clause(const size_type i, const bool f, const size_type j, const bool g) {
        assert(0 <= i and i < n);
        assert(0 <= j and j < n);
        scc.add_edge(2 * i + (f ? 0 : 1), 2 * j + (g ? 1 : 0));
        scc.add_edge(2 * j + (g ? 0 : 1), 2 * i + (f ? 1 : 0));
    }

    // https://atcoder.jp/contests/abc210/tasks/abc210_f
    void add_variable(const size_type k) {
        assert(0 <= k);
        scc.add_vertex(k + k);
        answer.resize(n + k);
        n += k;
    }
    void at_most_one(const std::vector<std::pair<size_type, bool>>& vs) {
        const size_type k = vs.size();
        const size_type o = n;
        add_variable(k);
        const auto y = [&](const size_type i) { return o + i; };
        for(size_type i = 0; i < k; i++) {
            auto [x, f] = vs[i];
            add_clause(x, !f, y(i), true);
            if(0 <= i - 1) {
                add_clause(y(i - 1), false, y(i), true);
                add_clause(y(i - 1), false, x, !f);
            }
        }
    }

    bool satisfiable() {
        scc.build();
        for(size_type i = 0; i < n; i++) {
            if(scc.ids[2 * i] == scc.ids[2 * i + 1]) return false;
            answer[i] = scc.ids[2 * i] < scc.ids[2 * i + 1];
        }
        return true;
    }
};

string solve(int N) {
    vector<int> x(N), y(N);
    vector<char> dir(N);
    map<pair<int,int>, int> mp;
    for(int i : rep(N)) {
        x[i] = in(), y[i] = in(), dir[i] = in();
        mp[{x[i], y[i]}] = i;
        if(dir[i] == 'x') mp[{x[i] + 1, y[i]}] = N + i;
        if(dir[i] == 'y') mp[{x[i], y[i] + 1}] = N + i;
    }

    two_sat ts(N);
    for(int i : rep(N)) {
        for(auto [dx, dy] : dir4) {
            if(dir[i] == 'x' and make_pair(dx, dy) == make_pair(+1, 0)) continue;
            if(dir[i] == 'y' and make_pair(dx, dy) == make_pair(0, +1)) continue;
            int nx = x[i] + dx, ny = y[i] + dy;
            if(mp.count({nx, ny})) {
                int j = mp[{nx, ny}];
                ts.add_clause(i, 0, j % N, j < N);
            }
        }

        for(auto [dx, dy] : dir4) {
            if(dir[i] == 'x' and make_pair(dx, dy) == make_pair(-1, 0)) continue;
            if(dir[i] == 'y' and make_pair(dx, dy) == make_pair(0, -1)) continue;
            int nx = x[i] + (dir[i] == 'x') + dx, ny = y[i] + (dir[i] == 'y') + dy;
            if(mp.count({nx, ny})) {
                int j = mp[{nx, ny}];
                ts.add_clause(i, 1, j % N, j < N);
            }
        }
    }

    return ts.satisfiable() ? "Yes" : "No";
}

int main() {
    while(true) {
        int N = in();
        if(N == 0) return 0;
        print(solve(N));
    }
}
accuracy: 1
time_ms: 80
memory_kb: 13024
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

#ifdef _RUTHEN
#include "debug.hpp"
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

void solve(int N) {
    V<int> x(N), y(N);
    V<char> c(N);
    map<ll, int> mp;
    const ll INF = 1000000000LL;
    rep(i, N) {
        cin >> x[i] >> y[i] >> c[i];
        mp[x[i] * INF + y[i]] = i;
        if (c[i] == 'x') {
            mp[(x[i] + 1) * INF + y[i]] = i;
        } else {
            mp[x[i] * INF + y[i] + 1] = i;
        }
    }
    V<int> seen(N, 0);
    const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
    rep(i, N) {
        if (seen[i]) continue;
        bool found = false;
        rep(_, 2) {
            bool ok = true;
            queue<int> que;
            que.push(i);
            map<ll, int> s;
            int hx = x[i], hy = y[i], fx = hx, fy = hy;
            if (c[i] == 'x') {
                fx++;
            } else {
                fy++;
            }
            s[hx * INF + hy] = _;
            s[fx * INF + fy] = _ ^ 1;
            while (!que.empty()) {
                int id = que.front();
                que.pop();
                array<int, 2> cx, cy;
                cx[0] = cx[1] = x[id];
                cy[0] = cy[1] = y[id];
                if (c[id] == 'x') {
                    cx[1]++;
                } else {
                    cy[1]++;
                }
                rep(j, 2) {
                    rep(k, 4) {
                        int nx = cx[j] + dx[k], ny = cy[j] + dy[k];
                        if (nx == cx[j ^ 1] and ny == cy[j ^ 1]) continue;
                        if (mp.count(nx * INF + ny)) {
                            int id2 = mp[nx * INF + ny];
                            if (s.count(nx * INF + ny) and s[nx * INF + ny] != s[cx[j] * INF + cy[j]]) {
                                ok = false;
                                break;
                            } else {
                                if (s.count(nx * INF + ny) == 0) que.push(id2);
                                s[nx * INF + ny] = s[cx[j] * INF + cy[j]];
                                if (nx == x[id2] and ny == y[id2]) {
                                    if (c[id2] == 'x') {
                                        s[(nx + 1) * INF + ny] = 1 - s[nx * INF + ny];
                                    } else {
                                        s[nx * INF + ny + 1] = 1 - s[nx * INF + ny];
                                    }
                                } else {
                                    s[x[id2] * INF + y[id2]] = 1 - s[nx * INF + ny];
                                }
                            }
                        }
                    }
                }
                if (!ok) break;
            }
            if (ok) {
                for (auto [coo, dir] : s) seen[mp[coo]] = 1;
                found = true;
                break;
            }
        }
        if (!found) {
            cout << "No" << '\n';
            return;
        }
    }
    cout << "Yes" << '\n';
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    while (cin >> N, N != 0) solve(N);
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 8252
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

#ifdef _RUTHEN
#include "debug.hpp"
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

void solve(int N) {
    V<int> x(N), y(N), d(N, 1);
    map<ll, int> mp;
    const ll INF = 1LL << 30;
    rep(i, N) {
        cin >> x[i] >> y[i];
        char c;
        cin >> c;
        mp[x[i] * INF + y[i]] = i;
        if (c == 'x') {
            d[i] = 0;
            mp[(x[i] + 1) * INF + y[i]] = i;
        } else {
            mp[x[i] * INF + y[i] + 1] = i;
        }
    }
    V<int> seen(N, 0);
    const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
    rep(i, N) {
        if (seen[i]) continue;
        bool okc = false;
        rep(_, 2) {
            bool ok = true;
            queue<int> que;
            que.push(i);
            map<int, int> s;
            s[i] = _;
            while (!que.empty()) {
                int id = que.front();
                que.pop();
                int hx, hy, fx, fy;
                if (d[id] == 0) {
                    hx = x[id], hy = y[id];
                    fx = x[id] + 1, fy = y[id];
                } else {
                    hx = x[id], hy = y[id];
                    fx = x[id], fy = y[id] + 1;
                }
                if (s[id] == 1) {
                    swap(hx, fx);
                    swap(hy, fy);
                }
                // h
                rep(k, 4) {
                    int nx = hx + dx[k], ny = hy + dy[k];
                    if (nx == fx and ny == fy) continue;
                    if (mp.count(nx * INF + ny)) {
                        int id2 = mp[nx * INF + ny];
                        if (nx == x[id2] and ny == y[id2]) {
                            if (s.count(id2) and s[id2] == 1) {
                                ok = false;
                                break;
                            } else {
                                if (s.count(id2) == 0) que.push(id2);
                                s[id2] = 0;
                            }
                        } else {
                            if (s.count(id2) and s[id2] == 0) {
                                ok = false;
                                break;
                            } else {
                                if (s.count(id2) == 0) que.push(id2);
                                s[id2] = 1;
                            }
                        }
                    }
                }
                // f
                rep(k, 4) {
                    int nx = fx + dx[k], ny = fy + dy[k];
                    if (nx == hx and ny == hy) continue;
                    if (mp.count(nx * INF + ny)) {
                        int id2 = mp[nx * INF + ny];
                        if (nx == x[id2] and ny == y[id2]) {
                            if (s.count(id2) and s[id2] == 0) {
                                ok = false;
                                break;
                            } else {
                                if (s.count(id2) == 0) que.push(id2);
                                s[id2] = 1;
                            }
                        } else {
                            if (s.count(id2) and s[id2] == 1) {
                                ok = false;
                                break;
                            } else {
                                if (s.count(id2) == 0) que.push(id2);
                                s[id2] = 0;
                            }
                        }
                    }
                }
                if (!ok) break;
            }
            if (ok) {
                for (auto [id, dir] : s) seen[id] = 1;
                okc = true;
                break;
            }
        }
        if (!okc) {
            cout << "No" << '\n';
            return;
        }
    }
    cout << "Yes" << '\n';
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    while (cin >> N, N != 0) solve(N);
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 7136
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

#ifdef _RUTHEN
#include "debug.hpp"
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

void solve(int N) {
    V<int> x(N), y(N), d(N, 1);
    map<ll, int> mp;
    const ll INF = 1LL << 30;
    rep(i, N) {
        cin >> x[i] >> y[i];
        char c;
        cin >> c;
        mp[x[i] * INF + y[i]] = i;
        if (c == 'x') {
            d[i] = 0;
            mp[(x[i] + 1) * INF + y[i]] = i;
        } else {
            mp[x[i] * INF + y[i] + 1] = i;
        }
    }
    V<int> seen(N, 0);
    const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
    bool oktot = true;
    rep(i, N) {
        if (seen[i]) continue;
        bool okc = false;
        rep(_, 2) {
            bool ok = true;
            queue<int> que;
            que.push(i);
            map<int, int> s;
            s[i] = _;
            while (!que.empty()) {
                int id = que.front();
                que.pop();
                int hx, hy, fx, fy;
                if (d[id] == 0) {
                    hx = x[id], hy = y[id];
                    fx = x[id] + 1, fy = y[id];
                } else {
                    hx = x[id], hy = y[id];
                    fx = x[id], fy = y[id] + 1;
                }
                if (s[id] == 1) {
                    swap(hx, fx);
                    swap(hy, fy);
                }
                show(id, make_tuple(hx, hy, fx, fy));
                // h
                rep(k, 4) {
                    int nx = hx + dx[k], ny = hy + dy[k];
                    if (nx == fx and ny == fy) continue;
                    if (mp.count(nx * INF + ny)) {
                        int id2 = mp[nx * INF + ny];
                        if (nx == x[id2] and ny == y[id2]) {
                            if (s.count(id2) and s[id2] == 1) {
                                ok = false;
                                show(id2, s[id2]);

                                break;
                            } else {
                                if (s.count(id2) == 0) que.push(id2);
                                s[id2] = 0;
                            }
                        } else {
                            if (s.count(id2) and s[id2] == 0) {
                                show(id2, s[id2]);
                                ok = false;
                                break;
                            } else {
                                if (s.count(id2) == 0) que.push(id2);
                                s[id2] = 1;
                            }
                        }
                    }
                }
                // f
                rep(k, 4) {
                    int nx = fx + dx[k], ny = fy + dy[k];
                    if (nx == hx and ny == hy) continue;
                    if (mp.count(nx * INF + ny)) {
                        int id2 = mp[nx * INF + ny];
                        if (nx == x[id2] and ny == y[id2]) {
                            if (s.count(id2) and s[id2] == 0) {
                                show(id2, s[id2]);
                                ok = false;
                                break;
                            } else {
                                if (s.count(id2) == 0) que.push(id2);
                                s[id2] = 1;
                            }
                        } else {
                            if (s.count(id2) and s[id2] == 1) {
                                show(id2, s[id2]);
                                ok = false;
                                break;
                            } else {
                                if (s.count(id2) == 0) que.push(id2);
                                s[id2] = 0;
                            }
                        }
                    }
                }
                if (!ok) break;
            }
            if (ok) {
                for (auto [id, dir] : s) seen[id] = 1;
                okc = true;
                break;
            }
        }
        if (!okc) {
            cout << "No" << '\n';
            return;
        }
    }
    cout << "Yes" << '\n';
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    while (cin >> N, N != 0) solve(N);
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 7200
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
// #pragma GCC target("arch=skylake-avx512")
// #include <atcoder/all>
// using namespace atcoder;
// #define NDEBUG

#pragma region template
// Define
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <class T> using pvector = vector<pair<T, T>>;
template <class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
constexpr const ll dx[4] = {1, 0, -1, 0};
constexpr const ll dy[4] = {0, 1, 0, -1};
constexpr const ll MOD = 1e9 + 7;
constexpr const ll mod = 998244353;
constexpr const ll INF = 1LL << 60;
constexpr const ll inf = 1 << 30;
constexpr const char rt = '\n';
constexpr const char sp = ' ';
#define rt(i, n) (i == (ll) (n) -1 ? rt : sp)
#define len(x) ((ll) (x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define ifn(x) if (not(x))
#define elif else if
#define elifn else ifn
#define fi first
#define se second
#define uniq(x) (sort(all(x)), (x).erase(unique(all(x)), (x).end()))
#define bis(x, y) ((ll) (lower_bound(all(x), y) - (x).begin()))

using graph = vector<vector<ll>>;
template <class T> using wgraph = vector<vector<pair<ll, T>>>;
bool __DIRECTED__ = true;
bool __ZERO_INDEXED__ = false;
istream &operator>>(istream &is, graph &g) {
    ll a, b;
    is >> a >> b;
    if (__ZERO_INDEXED__ == false) a--, b--;
    g[a].pb(b);
    if (__DIRECTED__ == false) g[b].pb(a);
    return is;
}
template <class T> istream &operator>>(istream &is, wgraph<T> &g) {
    ll a, b;
    T c;
    is >> a >> b >> c;
    if (__ZERO_INDEXED__ == false) a--, b--;
    g[a].pb({b, c});
    if (__DIRECTED__ == false) g[b].pb({a, c});
    return is;
}

template <class T> bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}

// Debug
#ifdef NDEBUG
#define debug(...)
#define dumpi(a, h, w)
#define vdumpi(a, n)
#define dump(a, h, w)
#define vdump(a, n)
#else
#define debug(...)                                                                                 \
    {                                                                                              \
        cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ";                                         \
        for (auto &&__i : {__VA_ARGS__}) cerr << "[" << __i << "] ";                               \
        cerr << rt;                                                                                \
    }

#define dumpi(a, h, w)                                                                             \
    {                                                                                              \
        cerr << __LINE__ << ": " << #a << " = [" << rt;                                            \
        rep(__i, h) {                                                                              \
            if (__i) cerr << ",\n";                                                                \
            cerr << "[";                                                                           \
            rep(__j, w) {                                                                          \
                if (__j) cerr << ", ";                                                             \
                if (abs(a[__i][__j]) >= INF / 2 and a[__i][__j] <= -INF / 2) cerr << '-';          \
                if (abs(a[__i][__j]) >= INF / 2) cerr << "∞";                                      \
                else                                                                               \
                    cerr << a[__i][__j];                                                           \
            }                                                                                      \
            cerr << "]";                                                                           \
        }                                                                                          \
        cerr << "\n]" << rt;                                                                       \
    }

#define vdumpi(a, n)                                                                               \
    {                                                                                              \
        cerr << __LINE__ << ": " << #a << " = [";                                                  \
        rep(__i, n) {                                                                              \
            if (__i) cerr << ", ";                                                                 \
            if (abs(a[__i]) >= INF / 2 and a[__i] <= -INF / 2) cerr << '-';                        \
            if (abs(a[__i]) >= INF / 2) cerr << "∞";                                               \
            else                                                                                   \
                cerr << a[__i];                                                                    \
        }                                                                                          \
        cerr << "]" << rt;                                                                         \
    }

#define dump(a, h, w)                                                                              \
    {                                                                                              \
        cerr << __LINE__ << ": " << #a << " = [" << rt;                                            \
        rep(__i, h) {                                                                              \
            if (__i) cerr << ",\n";                                                                \
            cerr << "[";                                                                           \
            rep(__j, w) {                                                                          \
                if (__j) cerr << ", ";                                                             \
                cerr << a[__i][__j];                                                               \
            }                                                                                      \
            cerr << "]";                                                                           \
        }                                                                                          \
        cerr << "\n]" << rt;                                                                       \
    }

#define vdump(a, n)                                                                                \
    {                                                                                              \
        cerr << __LINE__ << ": " << #a << " = [";                                                  \
        rep(__i, n) {                                                                              \
            if (__i) cerr << ", ";                                                                 \
            cerr << a[__i];                                                                        \
        }                                                                                          \
        cerr << "]" << rt;                                                                         \
    }
#endif

template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
    is >> p.first >> p.second;
    return is;
}
template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &p) {
    os << p.first << ' ' << p.second;
    return os;
}

// Loop
#define inc(i, a, n) for (ll i = (a), _##i = (n); i <= _##i; ++i)
#define dec(i, a, n) for (ll i = (a), _##i = (n); i >= _##i; --i)
#define rep(i, n) for (ll i = 0, _##i = (n); i < _##i; ++i)
#define each(i, a) for (auto &&i : a)

// Stream
#define fout(n) cout << fixed << setprecision(n)
struct io {
    io() { cin.tie(nullptr), ios::sync_with_stdio(false); }
} io;

// Speed
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("Ofast,unroll-loops")

// Math
inline constexpr ll gcd(const ll a, const ll b) { return b ? gcd(b, a % b) : a; }
inline constexpr ll lcm(const ll a, const ll b) { return a / gcd(a, b) * b; }

inline constexpr ll modulo(const ll n, const ll m = MOD) {
    ll k = n % m;
    return k + m * (k < 0);
}
inline constexpr ll chmod(ll &n, const ll m = MOD) {
    n %= m;
    return n += m * (n < 0);
}
inline constexpr ll mpow(ll a, ll n, const ll m = MOD) {
    ll r = 1;
    rep(i, 64) {
        if (n & (1LL << i)) r *= a;
        chmod(r, m);
        a *= a;
        chmod(a, m);
    }
    return r;
}
inline ll inv(const ll n, const ll m = MOD) {
    ll a = n, b = m, x = 1, y = 0;
    while (b) {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        x -= t * y;
        swap(x, y);
    }
    return modulo(x, m);
}
unsigned long long binary_gcd(unsigned long long x, unsigned long long y) {
    if (!x | !y) return x | y;
    unsigned long long cx = __builtin_ctzll(x), cy = __builtin_ctzll(y);
    x >>= cx, y >>= cy;
    while (x ^ y) {
        if (x > y) {
            x = (x - y) >> __builtin_ctzll(x ^ y);
        } else {
            y = (y - x) >> __builtin_ctzll(x ^ y);
        }
    }
    return x << min(cx, cy);
}

inline long long binary_gcd(long long x, long long y) {
    return binary_gcd((unsigned long long) (abs(x)), (unsigned long long) (abs(y)));
}

#define codeforces                                                                                 \
    ll testcases;                                                                                  \
    cin >> testcases;                                                                              \
    rep(testcase, testcases)
#define gcj(s) cout << s << testcase + 1 << ": "

#pragma endregion

struct UFS {
    vector<ll> data;
    UFS(ll n) : data(n, -1) {}
    ll root(ll x) {
        if (data[x] < 0) return x;
        else
            return data[x] = root(data[x]);
    }
    bool unite(ll x, ll y) {
        ll root_x = root(x), root_y = root(y);
        if (root_x != root_y) {
            if (data[root_x] > data[root_y]) swap(root_x, root_y);
            data[root_x] += data[root_y];
            data[root_y] = root_x;
            return true;
        }
        return false;
    }
    bool same(ll x, ll y) { return root(x) == root(y); }
    bool isroot(ll x) { return x == root(x); }
    ll size(ll x) { return -data[root(x)]; }
    ll size() {
        ll cnt = 0;
        rep(i, data.size()) if (isroot(i)) cnt++;
        return cnt;
    }
};

signed main() {
    ll n, cnt = 0;
    while (cin >> n, n) {
        map<pair<ll, ll>, vector<ll>> m;
        UFS uf(2 * n), u2(4 * n);

        rep(i, n) {
            ll x, y;
            char d;
            cin >> x >> y >> d;
            ll x2 = d == 'x' ? x + 1 : x;
            ll y2 = d == 'y' ? y + 1 : y;
            each(j, m[mp(x, y)]) uf.unite(j, i);
            each(j, m[mp(x2, y2)]) uf.unite(j, i + n);
            rep(k, 4) {
                m[{x + dx[k], y + dy[k]}].push_back(i);
                m[{x2 + dx[k], y2 + dy[k]}].push_back(i + n);
            }
        }

        ll ans = 0;
        rep(i, n) {
            u2.unite(uf.root(i), uf.root(i + n) + 2 * n);
            u2.unite(uf.root(i) + 2 * n, uf.root(i + n));
        }
        rep(i, 2 * n) if (u2.same(i, i + 2 * n)) ans++;

        cout << (ans ? "No" : "Yes") << rt;
    }
}
accuracy: 1
time_ms: 120
memory_kb: 21736
final_rank: 20

code_snippet: #include <stdio.h>
#include <vector>
#include <map>
using namespace std;

int color[40960];
const int dxy[4][2] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} };
vector<pair<int, int>> ed[40960];
map<pair<int, int>, int> mp;

void coloring(int x, int c) {
    color[x] = c;
    for (auto& i : ed[x]) {
        if (color[i.first] < 0) {
            coloring(i.first, c ^ i.second);
        }
    }
}

int main(void) {
    int n, x, y, res;
    char d;
    while (1) {
        scanf("%d", &n);
        if (n == 0) break;
        for (int i = 0; i < n; i++) {
            scanf("%d %d %c", &x, &y, &d);
            mp.insert({ {x, y}, i * 2 });
            for (int j = 0; j < 4; j++) {
                if (mp.find({ x + dxy[j][0], y + dxy[j][1] }) != mp.end()) {
                    ed[i * 2].push_back({ mp[{ x + dxy[j][0], y + dxy[j][1] }], 0 });
                    ed[mp[{ x + dxy[j][0], y + dxy[j][1] }]].push_back({ i * 2, 0 });
                }
            }
            if (d == 'x') x++;
            else y++;
            ed[i * 2].push_back({ i * 2 + 1, 1 });
            ed[i * 2 + 1].push_back({ i * 2, 1 });
            mp.insert({ {x, y}, i * 2 + 1 });
            for (int j = 0; j < 4; j++) {
                if (mp.find({ x + dxy[j][0], y + dxy[j][1] }) != mp.end()) {
                    if (mp[{ x + dxy[j][0], y + dxy[j][1] }] == i * 2) continue;
                    ed[i * 2 + 1].push_back({ mp[{ x + dxy[j][0], y + dxy[j][1] }], 0 });
                    ed[mp[{ x + dxy[j][0], y + dxy[j][1] }]].push_back({ i * 2 + 1, 0 });
                }
            }
        }

        for (int i = 0; i < n * 2; i++) color[i] = -1;
        for (int i = 0; i < n * 2; i++) {
            if (color[i] < 0) {
                coloring(i, 0);
            }
        }
        res = 1;
        for (int i = 0; i < n * 2; i++) {
            for (auto& j : ed[i]) {
                if ((color[i] ^ j.second) != color[j.first]) res = 0;
            }
        }
        printf("%s\n", res ? "Yes" : "No");
        for (int i = 0; i < n * 2; i++) ed[i].clear();
        mp.clear();
    }
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 8908
final_rank: 5

code_snippet: #pragma region Macros
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define REP1(i, n) for(int i = 0; i < (n); i++)
#define REP2(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MOD2 = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

void Case(int i) { cout << "Case #" << i << ": "; }
int popcount(int x) { return __builtin_popcount(x); }
ll popcount(ll x) { return __builtin_popcountll(x); }
#pragma endregion Macros

using P = pair<int, int>;

bool solve() {
    int N;
    cin >> N;
    if(N == 0) return false;

    // seen[x, y] = 0:頭, 1:足, -1:まだ見てない
    map<P, int> seen, ID;
    set<P> rem;
    
    REP(i, N) {
        int x, y; char dir;
        cin >> x >> y >> dir;
        int nx, ny;
        if(dir == 'x') nx = x+1, ny = y;
        else nx = x, ny = y+1;
        ID[{x, y}] = i, seen[{x, y}] = -1;
        rem.emplace(x, y);
        ID[{nx, ny}] = i, seen[{nx, ny}] = -1;
        rem.emplace(nx, ny);
    }
    
    bool ok = true;
    while(rem.size()) {
        queue<tuple<P, int, int>> que;

        que.emplace(*rem.begin(), ID[*rem.begin()], 0);
        while(que.size()) {
            auto [p, id, is_head] = que.front();
            auto [x, y] = p;
            que.pop();
            REP(dir, 4) {
                int nx = x + dx[dir], ny = y + dy[dir];
                if(!seen.count({nx, ny})) continue;
                if(seen[{nx, ny}] == -1) {
                    if(id == ID[{nx, ny}]) {
                        seen[{nx, ny}] = 1 ^ is_head;
                        que.emplace(pair(nx, ny), id, 1 ^ is_head);
                        rem.erase({nx, ny});
                    } else {
                        seen[{nx, ny}] = is_head;
                        que.emplace(pair(nx, ny), ID[{nx, ny}], is_head);
                        rem.erase({nx, ny});
                    }
                } else {
                    if(id == ID[{nx, ny}]) {
                        if(seen[{nx, ny}] == is_head) {
                            ok = false;
                        }
                    } else {
                        if(seen[{nx, ny}] != is_head) {
                            ok = false;
                        }
                    }
                }
            }
            if(!ok) break;
        }
        if(!ok) break;
    }

    cout << (ok ? "Yes" : "No") << '\n';
    return true;
}

int main() {
    while(solve());
}
accuracy: 1
time_ms: 170
memory_kb: 10208
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;
const int INF = 1e9;
const ll inf = 1LL<<60;

int n;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

struct S {
  int x, y, id, head;
  S(int x, int y, int id, int head):x(x), y(y), id(id), head(head) {}
};

int main() {
  while (cin >> n, n) {
    map<pair<int, int>, int> id;
    map<pair<int, int>, int> futon;
    set<pair<int, int>> notused;
    int x, y;
    char dir;
    for (int i=0; i<n; i++) {
      cin >> x >> y >> dir;
      notused.insert({x, y});
      id[{x, y}] = i;
      futon[{x, y}] = -1;
      int d = 3;
      if (dir == 'x') d = 2;
      x += dx[d]; y += dy[d];
      notused.insert({x, y});
      id[{x, y}] = i;
      futon[{x, y}] = -1;
    }
    bool ok = true;
    while (!notused.empty()) {
      queue<S> que;
      pair<int, int> p = *notused.begin();
      que.push(S(p.first, p.second, id[p], 0));
      while (!que.empty()) {
        S s = que.front();
        que.pop();
        for (int i=0; i<4; i++) {
          int nx = s.x + dx[i], ny = s.y + dy[i];
          pair<int, int> np = {nx, ny};
          if (futon.find(np) != futon.end()) {
            if (futon[np] != -1) {
              if (s.id == id[np]) {
                if (futon[np] == s.head) {
                  ok = false;
                  break;
                }
                continue;
              }
              if (futon[np] != s.head) {
                ok = false;
                break;
              }
              continue;
            } else {
              if (s.id == id[np]) {
                futon[np] = (s.head + 1) % 2;
                que.push(S(nx, ny, s.id, futon[np]));
                notused.erase(notused.find(np));
                continue;
              }
              futon[np] = s.head;
              que.push(S(nx, ny, id[np], s.head));
              notused.erase(notused.find(np));
              continue;
            }
          }
        }
        if (!ok) break;
      }
      if (!ok) break;
    }
    cout << (ok ? "Yes" : "No") << endl;
  }
}
accuracy: 1
time_ms: 200
memory_kb: 10188
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Unionfind {
  // tree number
  vector<int> par;
  // constructor
  Unionfind(int n = 1) : par(n, -1) {}
  // search root
  int root(int x) {
    if (par[x] < 0) return x;
    return par[x] = root(par[x]);
  }
  // is same?
  bool issame(int x, int y) { return root(x) == root(y); }

  // add
  // already added, return 0
  bool uni(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return 0;
    if (par[x] > par[y]) swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return 1;
  }
  int size(int x) { return -par[root(x)]; }
};

using P = pair<int, int>;
using T = tuple<int, int, int>;
int n;
vector<T> v;

bool solve();

int main() {
  while (1) {
    cin >> n;
    if (!n) break;
    v.clear();
    for (int i = 0; i < n; ++i) {
      int x, y;
      char c;
      cin >> x >> y >> c;
      v.emplace_back(x, y, c == 'y');
    }
    if (solve())
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

bool solve() {
  map<P, int> mp;
  Unionfind uf(2 * n);
  for (int id = 0; id < n; ++id) {
    auto [x, y, f] = v[id];
    for (int times = 0; times < 2; ++times) {
      for (int i = 0; i < 4; ++i) {
        int d[4] = {0, 1, 0, -1};
        int tx = x + d[i], ty = y + d[1 ^ i];
        if (mp.count(P(tx, ty))) uf.uni(mp[P(tx, ty)], times * n + id);
      }
      f ? ++y : ++x;
    }
    for (int t = 0; t < 2; ++t) f ? --y : --x;
    mp[P(x, y)] = id;
    f ? ++y : ++x;
    mp[P(x, y)] = id + n;
  }
  vector<vector<int>> g;
  for (int i = 0; i < n; ++i) {
    int x = uf.root(i), y = uf.root(i + n);
    g.resize(max({x, y, (int)g.size()}) + 1);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int len = g.size();
  vector<int> dist(len, 0);
  for (int i = 0; i < len; ++i)
    if (!dist[i]) {
      dist[i] = 1;
      queue<int> qu;
      qu.push(i);
      while (qu.size()) {
        int now = qu.front();
        qu.pop();
        for (auto to : g[now])
          if (!dist[to]) {
            dist[to] = 3 - dist[now];
            qu.push(to);
          } else if (dist[to] != 3 - dist[now])
            return 0;
      }
    }
  return 1;
}
accuracy: 1
time_ms: 100
memory_kb: 8440
final_rank: 6

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
    ll modpow(ll x, ll b,ll mod=MOD){
        ll res = 1;
        while(b){
            if(b&1)res = res * x % mod;
            x = x * x % mod;
            b>>=1;
        }
        return res;
    }

    ll modinv(ll x){
        return modpow(x, MOD-2);
    }

    bool was_output = false;
    template<class t>
    void output(t a){
        if(was_output)cout << " ";
        cout << a;
        was_output = true;
    }
    void outendl(){
        was_output = false;
        cout << endl;
    }

    template<class t>
    istream& operator>>(istream&is, vector<t>&x){
        for(auto &i:x)is >> i;
        return is;
    }

    template<class t, class u>
    istream& operator>>(istream&is, pair<t, u>&x){
        is >> x.first >> x.second;
        return is;
    }

    template<class t>
    ostream& operator<<(ostream&os, vector<t> &v){
        os << "{";
        for(t &i:v){
            os << i << ", ";
        }
        os << "}";
        return os;
    }

    template<class t = long long>
    t in(){
        t res; cin >> res; return res;
    }

    template<class t>
    void out(t x){
        cout << x;
    }


    template<class t>
    vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
        sort(line.begin(),line.end(),comp);
        return line;
    }

    template<class t>
    vector<t> reversed(vector<t> line){
        reverse(line.begin(),line.end());
        return line;
    }
    string reversed(string str){
        reverse(str.begin(),str.end());
        return str;
    }

    long long gcd(long long a,long long b){
        while(b){
            a %= b;
            swap(a,b);
        }
        return a;
    }

    long long lcm(long long a,long long b){
        return a / gcd(a,b) * b;
    }
}

using namespace templates;

pii operator+(pii x,pii y){
    return pii(x.first+y.first,x.second+y.second);
}

pii dirs[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

bool func(int n){
    map<pii,int> has;
    map<pii,bool> heads;
    rep(i,n){
        int x = in();
        int y = in();
        pii p(x,y);
        has[p] = i;
        if(in<char>()=='x'){
            has[p+dirs[0]]=i;
        }else{
            has[p+dirs[1]]=i;
        }
    }

    method(rec,bool,pii p,bool head){
        heads[p] = head;
        rep(i,4){
            pii np = p + dirs[i];
            if(!has.count(np))continue;
            bool next_head = (has[p]==has[np])?!head:head;
            if(heads.count(np)){
                if(heads[np]!=next_head)return false;
                continue;
            }
            if(!rec(np,next_head))return false;
        }
        return true;
    };

    foreach(i,has){
        if(heads.count(i.first))continue;
        if(!rec(i.first,false))return false;
    }
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    while(true){
        int n = in();
        if(!n)break;
        cout << (func(n) ? "Yes" : "No") << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 12040
final_rank: 14

code_snippet: #include <iostream>
#include <queue>
#include <map>
using namespace std;
typedef pair<int, int> P;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

template <typename T>
struct Edge
{
    int to;
    T cost;
};

template <typename T>
struct WeightedGraph
{
    int n;
    std::vector<std::vector<Edge<T>>> g;
    
    WeightedGraph(){}
    
    WeightedGraph(int n) : n(n){
        g.resize(n);
    }
    
    void add_edge(int from, int to, T cost){
        g[from].push_back((Edge<T>){to, cost});
    }
};


int main()
{
    while(true){
        int n;
        cin >> n;
        if(n == 0) break;
        map<P, int> mp;
        WeightedGraph<int> g(n * 2);
        for(int i = 0; i < n; i++){
            int x, y;
            char c;
            cin >> x >> y >> c;
            for(int k = 0; k < 4; k++){
                int u = x + dx[k], v = y + dy[k];
                if(mp.count(P(u, v))){
                    g.add_edge(i * 2, mp[P(u, v)], 0);
                    g.add_edge(mp[P(u, v)], i * 2, 0);
                }
            }
            int xx = x, yy = y;
            if(c == 'x') xx++;
            else yy++;
            for(int k = 0; k < 4; k++){
                int u = xx + dx[k], v = yy + dy[k];
                if(mp.count(P(u, v))){
                    g.add_edge(i * 2 + 1, mp[P(u, v)], 0);
                    g.add_edge(mp[P(u, v)], i * 2 + 1, 0);
                }
            }
            mp[P(x, y)] = i * 2;
            mp[P(xx, yy)] = i * 2 + 1;
            g.add_edge(i * 2, i * 2 + 1, 1);
            g.add_edge(i * 2 + 1, i * 2, 1);
        }
        int c[40005];
        for(int i = 0; i < n * 2; i++) c[i] = -1;
        for(int i = 0; i < n * 2; i++){
            if(c[i] >= 0) continue;
            c[i] = 0;
            queue<int> que;
            que.push(i);
            while(que.size()){
                int u = que.front();
                que.pop();
                for(Edge<int> e : g.g[u]){
                    int v = e.to;
                    if(c[v] == (c[u] + (1 - e.cost)) % 2){
                        goto dame;
                    }
                    if(c[v] == -1){
                        c[v] = (c[u] + e.cost) % 2;
                        que.push(v);
                    }
                }
            }
        }
        cout << "Yes" << endl;
        continue;
    dame:
        cout << "No" << endl;
    }
}
accuracy: 1
time_ms: 130
memory_kb: 7900
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct StronglyConnectedComponents{
    vector<vector<int>> G,rG,C,T;
    vector<int> vs,cmp,used;
    StronglyConnectedComponents(int n):G(n),rG(n),cmp(n),used(n){}
    void add_edge(int u,int v){
        G[u].emplace_back(v);
        rG[v].emplace_back(u);
    }
    void dfs(int v){
        used[v]=1;
        for (int u:G[v]) if(!used[u]) dfs(u);
        vs.emplace_back(v);
    }
    void rdfs(int v,int k){
        used[v]=1;
        cmp[v]=k;
        C[k].emplace_back(v);
        for (int u:rG[v]) if (!used[u]) rdfs(u,k);
    }
    int build(){
        int n=G.size();
        for (int i=0;i<n;++i) if (!used[i]) dfs(i);
        fill(used.begin(),used.end(),0);
        int k=0;
        for (int i=n-1;i>=0;--i){
            if (!used[vs[i]]){
                C.emplace_back(),T.emplace_back();
                rdfs(vs[i],k++);
            }
        }
        for (int v=0;v<n;++v){
            for (int u:G[v]){
                if (cmp[v]!=cmp[u]){
                    T[cmp[v]].emplace_back(cmp[u]);
                }
            }
        }
        for (int i=0;i<k;++i){
            sort(T[i].begin(),T[i].end());
            T[i].erase(unique(T[i].begin(),T[i].end()),T[i].end());
        }
        return k;
    }
    int operator[](int i) const{return cmp[i];}
};

struct TwoSatisfiability{
    int n;
    StronglyConnectedComponents SCC;
    TwoSatisfiability(int n):n(n),SCC(n*2){}
    int neg(int v){return (n+v)%(n*2);}
    void add_if(int u,int v){
        SCC.add_edge(u,v);
        SCC.add_edge(neg(v),neg(u));
    }
    void add_or(int u,int v){
        add_if(neg(u),v);
    }
    void add_nand(int u,int v){
        add_if(u,neg(v));
    }
    void set_true(int v){
        SCC.add_edge(neg(v),v);
    }
    void set_false(int v){
        SCC.add_edge(v,neg(v));
    }
    vector<int> build(){
        SCC.build();
        vector<int> res(n);
        for (int i=0;i<n;++i){
            if (SCC[i]==SCC[n+i]) return {};
            res[i]=SCC[i]>SCC[n+i];
        }
        return res;
    }
};

const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

void solve(int n){
    vector<vector<int>> x(n,vector<int>(2)),y(n,vector<int>(2));
    map<pair<int,int>,int> mp;
    for (int i=0;i<n;++i){
        char dir; cin >> x[i][0] >> y[i][0] >> dir;
        x[i][1]=x[i][0]+(dir=='x'); y[i][1]=y[i][0]+(dir=='y');
        for (int j=0;j<2;++j) mp[{x[i][j],y[i][j]}]=i+j*n;
    }

    TwoSatisfiability TS(n);
    for (int i=0;i<n;++i){
        for (int j=0;j<2;++j){
            for (int k=0;k<4;++k){
                int nx=x[i][j^1]+dx[k],ny=y[i][j^1]+dy[k];
                if (!mp.count({nx,ny})) continue;
                int adj=mp[{nx,ny}];
                if (adj%n==i) continue;
                TS.add_or((j?TS.neg(i):i),(adj/n?TS.neg(adj%n):adj));
            }
        }
    }

    cout << (!TS.build().empty()?"Yes":"No") << '\n';
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    while (cin >> n,n){
        solve(n);
    }
}
accuracy: 1
time_ms: 130
memory_kb: 15012
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long MOD=1000000007;
// const long long MOD=998244353;
#define LOCAL
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(),(x).end()
const int INF=1e9;
const long long IINF=1e18;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const char dir[4]={'D','R','U','L'};

template<typename T>
istream &operator>>(istream &is,vector<T> &v){
    for (T &x:v) is >> x;
    return is;
}
template<typename T>
ostream &operator<<(ostream &os,const vector<T> &v){
    for (int i=0;i<v.size();++i){
        os << v[i] << (i+1==v.size()?"": " ");
    }
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const pair<T,U> &p){
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template<typename T,typename U,typename V>
ostream&operator<<(ostream &os,const tuple<T,U,V> &t){
    os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ')';
    return os;
}
template<typename T,typename U,typename V,typename W>
ostream&operator<<(ostream &os,const tuple<T,U,V,W> &t){
    os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ',' << get<3>(t) << ')';
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const map<T,U> &m){
    os << '{';
    for (auto itr=m.begin();itr!=m.end();){
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr!=m.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const unordered_map<T,U> &m){
    os << '{';
    for (auto itr=m.begin();itr!=m.end();){
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr!=m.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const set<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const multiset<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const unordered_set<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const deque<T> &v){
    for (int i=0;i<v.size();++i){
        os << v[i] << (i+1==v.size()?"": " ");
    }
    return os;
}

void debug_out(){cerr << '\n';}
template<class Head,class... Tail>
void debug_out(Head&& head,Tail&&... tail){
    cerr << head;
    if (sizeof...(Tail)>0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...) cerr << " ";\
cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n';\
cerr << " ";\
debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template<typename T> T gcd(T x,T y){return y!=0?gcd(y,x%y):x;}
template<typename T> T lcm(T x,T y){return x/gcd(x,y)*y;}

template<class T1,class T2> inline bool chmin(T1 &a,T2 b){
    if (a>b){a=b; return true;} return false;
}
template<class T1,class T2> inline bool chmax(T1 &a,T2 b){
    if (a<b){a=b; return true;} return false;
}
#pragma endregion

struct StronglyConnectedComponents{
    vector<vector<int>> G,rG,C,T;
    vector<int> vs,cmp,used;
    StronglyConnectedComponents(int n):G(n),rG(n),cmp(n),used(n){}
    void add_edge(int u,int v){
        G[u].emplace_back(v);
        rG[v].emplace_back(u);
    }
    void dfs(int v){
        used[v]=1;
        for (int u:G[v]) if(!used[u]) dfs(u);
        vs.emplace_back(v);
    }
    void rdfs(int v,int k){
        used[v]=1;
        cmp[v]=k;
        C[k].emplace_back(v);
        for (int u:rG[v]) if (!used[u]) rdfs(u,k);
    }
    int build(){
        int n=G.size();
        for (int i=0;i<n;++i) if (!used[i]) dfs(i);
        fill(used.begin(),used.end(),0);
        int k=0;
        for (int i=n-1;i>=0;--i){
            if (!used[vs[i]]){
                C.emplace_back(),T.emplace_back();
                rdfs(vs[i],k++);
            }
        }
        for (int v=0;v<n;++v){
            for (int u:G[v]){
                if (cmp[v]!=cmp[u]){
                    T[cmp[v]].emplace_back(cmp[u]);
                }
            }
        }
        for (int i=0;i<k;++i){
            sort(T[i].begin(),T[i].end());
            T[i].erase(unique(T[i].begin(),T[i].end()),T[i].end());
        }
        return k;
    }
    int operator[](int i) const{return cmp[i];}
};

struct TwoSatisfiability{
    int n;
    StronglyConnectedComponents SCC;
    TwoSatisfiability(int n):n(n),SCC(n*2){}
    int neg(int v){return (n+v)%(n*2);}
    void add_if(int u,int v){
        SCC.add_edge(u,v);
        SCC.add_edge(neg(v),neg(u));
    }
    void add_or(int u,int v){
        add_if(neg(u),v);
    }
    void add_nand(int u,int v){
        add_if(u,neg(v));
    }
    void set_true(int v){
        SCC.add_edge(neg(v),v);
    }
    void set_false(int v){
        SCC.add_edge(v,neg(v));
    }
    vector<int> build(){
        SCC.build();
        vector<int> res(n);
        for (int i=0;i<n;++i){
            if (SCC[i]==SCC[n+i]) return {};
            res[i]=SCC[i]>SCC[n+i];
        }
        return res;
    }
};

void solve(int n){
    vector<vector<int>> x(n,vector<int>(2)),y(n,vector<int>(2));
    map<pair<int,int>,int> mp;
    for (int i=0;i<n;++i){
        char dir; cin >> x[i][0] >> y[i][0] >> dir;
        x[i][1]=x[i][0]+(dir=='x'); y[i][1]=y[i][0]+(dir=='y');
        for (int j=0;j<2;++j) mp[{x[i][j],y[i][j]}]=i+j*n;
    }

    TwoSatisfiability TS(n);
    for (int i=0;i<n;++i){
        for (int j=0;j<2;++j){
            for (int k=0;k<4;++k){
                int nx=x[i][j^1]+dx[k],ny=y[i][j^1]+dy[k];
                if (!mp.count({nx,ny})) continue;
                int adj=mp[{nx,ny}];
                if (adj%n==i) continue;
                TS.add_or((j?TS.neg(i):i),(adj/n?TS.neg(adj%n):adj));
            }
        }
    }

    cout << (!TS.build().empty()?"Yes":"No") << '\n';
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    while (cin >> n,n){
        solve(n);
    }
}
accuracy: 1
time_ms: 130
memory_kb: 14856
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }

const ll INF = 1LL << 60;

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }
} fastiofastio;

map<PII, int> idx;
set<PII> head;
set<PII> foot;
int X1[200001];
int Y1[200001];
int X2[200001];
int Y2[200001];
bool vis[200001];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

bool dfs(int v) {
    int x1 = X1[v];
    int y1 = Y1[v];
    int x2 = X2[v];
    int y2 = Y2[v];
    for (int i = 0; i < 2; i++) {
        bool ok = 1;
        set<int> to;
        for (int j = 0; j < 4; j++) {
            int nx = x1 + dx[j];
            int ny = y1 + dy[j];
            if (nx == x2 && ny == y2)
                continue;
            if (foot.find(PII(nx, ny)) != foot.end()) {
                ok = 0;
                break;
            }
            if (idx.find(PII(nx, ny)) != idx.end()) {
                to.insert(idx[PII(nx, ny)]);
            }
        }
        for (int j = 0; j < 4; j++) {
            int nx = x2 + dx[j];
            int ny = y2 + dy[j];
            if (nx == x1 && ny == y1)
                continue;
            if (head.find(PII(nx, ny)) != head.end()) {
                ok = 0;
                break;
            }
            if (idx.find(PII(nx, ny)) != idx.end()) {
                to.insert(idx[PII(nx, ny)]);
            }
        }
        if (ok) {
            vis[v] = 1;
            head.insert(PII(x1, y1));
            foot.insert(PII(x2, y2));
            bool all = 1;
            for (int x : to) {
                if (!vis[x] && !dfs(x)) {
                    all = 0;
                    break;
                }
            }
            head.erase(PII(x1, y1));
            foot.erase(PII(x2, y2));
            if (all)
                return true;
            vis[v] = 0;
        }
        swap(x1, x2);
        swap(y1, y2);
    }
    return false;
}

int main() {
    while (1) {
        int n;
        cin >> n;
        if (n == 0)
            break;
        idx.clear();
        head.clear();
        foot.clear();
        memset(vis, 0, sizeof(vis));
        for (int i = 1; i <= n; i++) {
            int x, y;
            string dir;
            cin >> x >> y >> dir;
            idx[PII(x, y)] = i;
            X1[i] = x;
            Y1[i] = y;
            if (dir == "x") {
                idx[PII(x + 1, y)] = i;
                X2[i] = x + 1;
                Y2[i] = y;
            } else {
                idx[PII(x, y + 1)] = i;
                X2[i] = x;
                Y2[i] = y + 1;
            }
        }
        bool ok = 1;
        for (int i = 1; i <= n; i++) {
            if (!vis[i] && !dfs(i)) {
                ok = 0;
                break;
            }
        }
        if (ok)
            cout << "Yes\n";
        else
            cout << "No\n";
    }
}
accuracy: 1
time_ms: 120
memory_kb: 13608
final_rank: 15

code_snippet: #include<bits/stdc++.h>


#include <algorithm>
#include <utility>
#include <vector>

namespace atcoder {
namespace internal {

template <class E> struct csr {
    std::vector<int> start;
    std::vector<E> elist;
    csr(int n, const std::vector<std::pair<int, E>>& edges)
        : start(n + 1), elist(edges.size()) {
        for (auto e : edges) {
            start[e.first + 1]++;
        }
        for (int i = 1; i <= n; i++) {
            start[i] += start[i - 1];
        }
        auto counter = start;
        for (auto e : edges) {
            elist[counter[e.first]++] = e.second;
        }
    }
};

// Reference:
// R. Tarjan,
// Depth-First Search and Linear Graph Algorithms
struct scc_graph {
  public:
    scc_graph(int n) : _n(n) {}

    int num_vertices() { return _n; }

    void add_edge(int from, int to) { edges.push_back({from, {to}}); }

    // @return pair of (# of scc, scc id)
    std::pair<int, std::vector<int>> scc_ids() {
        auto g = csr<edge>(_n, edges);
        int now_ord = 0, group_num = 0;
        std::vector<int> visited, low(_n), ord(_n, -1), ids(_n);
        visited.reserve(_n);
        auto dfs = [&](auto self, int v) -> void {
            low[v] = ord[v] = now_ord++;
            visited.push_back(v);
            for (int i = g.start[v]; i < g.start[v + 1]; i++) {
                auto to = g.elist[i].to;
                if (ord[to] == -1) {
                    self(self, to);
                    low[v] = std::min(low[v], low[to]);
                } else {
                    low[v] = std::min(low[v], ord[to]);
                }
            }
            if (low[v] == ord[v]) {
                while (true) {
                    int u = visited.back();
                    visited.pop_back();
                    ord[u] = _n;
                    ids[u] = group_num;
                    if (u == v) break;
                }
                group_num++;
            }
        };
        for (int i = 0; i < _n; i++) {
            if (ord[i] == -1) dfs(dfs, i);
        }
        for (auto& x : ids) {
            x = group_num - 1 - x;
        }
        return {group_num, ids};
    }

    std::vector<std::vector<int>> scc() {
        auto ids = scc_ids();
        int group_num = ids.first;
        std::vector<int> counts(group_num);
        for (auto x : ids.second) counts[x]++;
        std::vector<std::vector<int>> groups(ids.first);
        for (int i = 0; i < group_num; i++) {
            groups[i].reserve(counts[i]);
        }
        for (int i = 0; i < _n; i++) {
            groups[ids.second[i]].push_back(i);
        }
        return groups;
    }

  private:
    int _n;
    struct edge {
        int to;
    };
    std::vector<std::pair<int, edge>> edges;
};

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <vector>

namespace atcoder {

// Reference:
// B. Aspvall, M. Plass, and R. Tarjan,
// A Linear-Time Algorithm for Testing the Truth of Certain Quantified Boolean
// Formulas
struct two_sat {
  public:
    two_sat() : _n(0), scc(0) {}
    two_sat(int n) : _n(n), _answer(n), scc(2 * n) {}

    void add_clause(int i, bool f, int j, bool g) {
        assert(0 <= i && i < _n);
        assert(0 <= j && j < _n);
        scc.add_edge(2 * i + (f ? 0 : 1), 2 * j + (g ? 1 : 0));
        scc.add_edge(2 * j + (g ? 0 : 1), 2 * i + (f ? 1 : 0));
    }
    bool satisfiable() {
        auto id = scc.scc_ids().second;
        for (int i = 0; i < _n; i++) {
            if (id[2 * i] == id[2 * i + 1]) return false;
            _answer[i] = id[2 * i] < id[2 * i + 1];
        }
        return true;
    }
    std::vector<bool> answer() { return _answer; }

  private:
    int _n;
    std::vector<bool> _answer;
    internal::scc_graph scc;
};

}  // namespace atcoder

using namespace std;
using namespace atcoder;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const ll mod=998244353;
const ll LINF=1ll<<60;
const int INF=1<<28;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};



int main(){
    int n;
    while(cin >> n, n){
        vector<int> x(n), y(n);
        vector<bool> f(n);
        map<P, pair<int, bool>> ma;
        for (int i = 0; i < n; i++) {
            char c;
            cin >> x[i] >> y[i] >> c;
            if(c == 'x') f[i] = true;
            else f[i] = false;
            ma[{x[i], y[i]}] = mp(i, 0);
            if(f[i]){
                ma[{x[i] + 1, y[i]}] = mp(i, 1);
            }
            else{
                ma[{x[i], y[i] + 1}] = mp(i, 1);
            }
        }
        two_sat ts(n);
        for (int i = 0; i < n; i++) {
            for (int fi = 0; fi < 2; fi++) {
                int xi = x[i], yi = y[i];
                if(f[i]){
                    xi += fi;
                }
                else{
                    yi += fi;
                }
                for (int j = 0; j < 4; j++) {
                    int nx = xi + dx[j], ny = yi + dy[j];
                    if(ma.count({nx, ny})){
                        auto p = ma[{nx, ny}];
                        if(p.fs == i) continue;
                        ts.add_clause(i, fi, p.fs, not p.sc);
                        ts.add_clause(i, not fi, p.fs, p.sc);
                    }
                }
            }
        }
        if(ts.satisfiable()) puts("Yes");
        else puts("No");
            }
            return 0;
            }
accuracy: 1
time_ms: 140
memory_kb: 11536
final_rank: 12

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h> 
#include <functional>
#include <cassert>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1e18;
constexpr ll LLMAX = 9223372036854775807;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ---------------------------------------------------------------------------


bool solve(){
  int N;
  cin >> N;
  if(N == 0) return false;
  map<P,int> mp;
  map<P,int> color;
  for(int i=0; i<N; i++){
    int x,y;
    char c;
    cin >> x >> y >> c;
    if(c == 'y'){
      color[P(x,y)] = -1;
      mp[P(x,y)] = i;
      color[P(x,y+1)] = -1;
      mp[P(x,y+1)] = i;
    }else{
      color[P(x,y)] = -1;
      mp[P(x,y)] = i;
      color[P(x+1,y)] = -1;
      mp[P(x+1,y)] = i;
    }
  }
  for(auto m: color){
    if(m.second != -1) continue;
    queue<pair<P,int>> que;
    color[m.first] = 0;
    que.emplace(m.first,0);
    while(que.size()){
      int x,y;
      tie(x,y) = que.front().first;
      int now = que.front().second;
      que.pop();
      for(int k=0; k<4; k++){
        int nx = x + dx[k];
        int ny = y + dy[k];
        if(color.find(P(nx,ny)) == color.end()) continue;
        if(mp[P(nx,ny)] == mp[P(x,y)]){
          if(color[P(nx,ny)] != -1){
            if(color[P(nx,ny)] == now){
              cout << "No\n";
              return true;
            }
          }else{
            color[P(nx,ny)] = (now^1);
            que.emplace(P(nx,ny),(now^1));
          }
        }else{
          if(color[P(nx,ny)] != -1){
            if(color[P(nx,ny)] != now){
              cout << "No\n";
              return true;
            }
          }else{
            color[P(nx,ny)] = now;
            que.emplace(P(nx,ny),now);
          }
        }
      }
    }
  }
  cout << "Yes\n";
  return true;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  while(solve());
  return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 8300
final_rank: 9