Problem:
Problem A: Infected Computer

Adam Ivan is working as a system administrator at Soy Group, Inc. He is now facing at a big trouble:
a number of computers under his management have been infected by a computer virus. Unfortunately,
anti-virus system in his company failed to detect this virus because it was very new.

Adam has identified the first computer infected by the virus and collected the records of all data packets
sent within his network. He is now trying to identify which computers have been infected. A computer
is infected when receiving any data packet from any infected computer. The computer is not infected, on
the other hand, just by sending data packets to infected computers.

It seems almost impossible for him to list all infected computers by hand, because the size of the packet
records is fairly large. So he asked you for help: write a program that can identify infected computers.

Input

The input consists of multiple datasets. Each dataset has the following format:

N M

t1 s1 d1

t2 s2 d2

...

tM sM dM

N is the number of computers; M is the number of data packets; ti (1 ≤ i ≤ M) is the time when the i-th
data packet is sent; si and di (1 ≤ i ≤ M) are the source and destination computers of the i-th data packet
respectively. The first infected computer is indicated by the number 1; the other computers are indicated
by unique numbers between 2 and N.

The input meets the following constraints: 0 < N ≤ 20000, 0 ≤ M ≤ 20000, and 0 ≤ ti ≤ 109 for
1 ≤ i ≤ N; all ti 's are different; and the source and destination of each packet are always different.

The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed.

Output

For each dataset, print the number of computers infected by the computer virus.

Sample Input

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

Output for the Sample Input

3
1

Solution in C (judge verdict: Accepted)

// AOJ 2155: Infected Computer
// 2017.10.20 bal4u@uu

#include <stdio.h>
#include <string.h>

typedef struct { int t, s, d; } T;
T tbl[20002];
char f[20002];

//#define getchar_unlocked()  getchar()
int in()
{
	int n = 0;
	int c = getchar_unlocked();
	do n = (n<<3)+(n<<1) + (c & 0xf), c = getchar_unlocked();
	while (c >= '0');
	return n;
}

#define THRESHOLD 10
#define STACKSIZE 35		// 2^32 for int

void quicksort(T *a, int n)
{
	int i, j, l, r, p;
	int lsp[STACKSIZE], rsp[STACKSIZE];
	T t, *m;
	
	l = 0, r = n-1, p = 0;
	while (1) {
		if (r-l <= THRESHOLD) {
			if (!p--) break;
			l = lsp[p], r = rsp[p];
		}
		m = a + ((l+r)>>1);
		i = l, j = r;
		while (1) {
			while (a[i].t < m->t) i++;
			while (m->t < a[j].t) j--;
			if (i >= j) break;
			t = a[i], a[i] = a[j], a[j] = t;
			i++, j--;
		}
		if (i-l > r-j) {
			if (i-l > THRESHOLD) {
				lsp[p] = l;
				rsp[p] = i-1;
				p++;
			}
			l = j+1;
		} else {
			if (r-j > THRESHOLD) {
				lsp[p] = j+1;
				rsp[p] = r;
				p++;
			}
			r = i-1;
		}
	}
	
	// insert sorting
	for (i = 1; i < n; i++) {
		t = a[i];
		for (j = i-1; j >= 0 && a[j].t > t.t; j--) a[j+1] = a[j];
		a[j+1] = t;
	}
}

int main()
{
	int n, m, i, ans;
	register T *t;
	char *q;

	while (n = in()) {
		m = in();
		memset(f, 0, n+1);
		t = tbl, i = m; while (i--) {
			t->t = in(), t->s = in(), t->d = in();
			t++;
		}
		quicksort(tbl, m);
		f[1] = 1, t = tbl, i = m;
		while (i--) f[t->d] |= f[t->s], t++;
		ans = 0, q = f+1, i = n; while (i--) ans += *q++;
		printf("%d\n", ans);
	}
	return 0;
}