This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define N	300000
 
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
 
unsigned int X = 12345;
 
int rand_() {
	return (X *= 3) >> 1;
}
 
int *ej[N], eo[N], *fi[N], fo[N];
 
void append(int **ej, int *eo, int i, int j) {
	int o = eo[i]++;
 
	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}
 
int xx[N], yy[N], ll[N], rr[N];
 
void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
 
		while (j < k)
			if (yy[ii[j]] == yy[i_])
				j++;
			else if (yy[ii[j]] > yy[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}
 
char visited[N]; int qu[N], cnt;
 
void dfs1(int j) {
	int o;
 
	if (visited[j])
		return;
	visited[j] = 1;
	for (o = fo[j]; o--; ) {
		int i = fi[j][o];
 
		dfs1(i);
	}
	qu[--cnt] = j;
}
 
int qu_[N], cnt_, l, r;
 
void dfs2(int i) {
	int o;
 
	l = min(l, ll[i]), r = max(r, rr[i]);
	if (visited[i])
		return;
	visited[i] = 1, qu_[cnt_++] = i;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];
 
		dfs2(j);
	}
}
 
int main() {
	static int ii0[N], iia[N], kk[N];
	int n, n0, na, m, a, b, h, i, j, k;
 
	scanf("%d%d%d%d", &n, &m, &a, &b);
	n0 = na = 0;
	for (i = 0; i < n; i++) {
		scanf("%d%d", &xx[i], &yy[i]);
		if (xx[i] == 0)
			ii0[n0++] = i;
		else if (xx[i] == a)
			iia[na++] = i;
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
		fi[i] = (int *) malloc(2 * sizeof *fi[i]);
	}
	for (h = 0; h < m; h++) {
		int t;
 
		scanf("%d%d%d", &i, &j, &t), i--, j--;
		append(ej, eo, i, j), append(fi, fo, j, i);
		if (t == 2)
			append(ej, eo, j, i), append(fi, fo, i, j);
	}
	sort(ii0, 0, n0);
	sort(iia, 0, na);
	for (h = 0; h < n0; h++)
		dfs2(ii0[h]);
	for (h = 0, k = 0; h < na; h++) {
		if (visited[iia[h]])
			k++;
		kk[h] = k;
	}
	memset(visited, 0, n * sizeof *visited);
	for (i = 0; i < n; i++)
		ll[i] = na, rr[i] = -1;
	for (h = 0; h < na; h++)
		ll[iia[h]] = rr[iia[h]] = h;
	cnt = n;
	for (i = 0; i < n; i++)
		dfs1(i);
	for (i = 0; i < n; i++)
		visited[i] = 0;
	memset(visited, 0, n * sizeof *visited);
	for (h = 0; h < n; h++)
		if (!visited[qu[h]]) {
			l = na, r = -1, cnt_ = 0;
			dfs2(qu[h]);
			while (cnt_--)
				ll[qu_[cnt_]] = l, rr[qu_[cnt_]] = r;
		}
	for (h = 0; h < n0; h++) {
		i = ii0[h];
		printf("%d\n", ll[i] > rr[i] ? 0 : kk[rr[i]] - (ll[i] == 0 ? 0 : kk[ll[i] - 1]));
	}
	return 0;
}
Compilation message (stderr)
tra.c: In function 'append':
tra.c:21:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   21 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
tra.c: In function 'main':
tra.c:83:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |  scanf("%d%d%d%d", &n, &m, &a, &b);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tra.c:86:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |   scanf("%d%d", &xx[i], &yy[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tra.c:97:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |   scanf("%d%d%d", &i, &j, &t), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/string.h:495,
                 from tra.c:3:
In function 'memset',
    inlined from 'main' at tra.c:121:2:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:71:10: warning: '__builtin___memset_chk' specified size between 18446744071562067968 and 18446744073709551615 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]
   71 |   return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |