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 JAVA (judge verdict: Accepted)

import java.util.*;
import static java.lang.System.*;

public class Main {

	Scanner sc = new Scanner(in);
	
	void quickSort_ascend(int[] a, int[] b, int[] c, int l, int r) {
		if (l < r) {
			int w = a[(l+r)/2];
			int i = l, j = r, temp;
			while (i < j) {
				while (a[i] < w) i++;
				while (a[j] > w) j--;
				if (i <= j) {
					temp = a[i];
					a[i] = a[j]; a[j] = temp;
					temp = b[i];
					b[i] = b[j]; b[j] = temp;
					temp = c[i];
					c[i] = c[j]; c[j] = temp;
					i++; j--;
				}
			}
			quickSort_ascend(a, b, c, l, j);
			quickSort_ascend(a, b, c, i, r);
		}
	}
	
	void run() {
		int n, m;
		while (true) {
			n = sc.nextInt(); m = sc.nextInt();
			if (n == 0 && m == 0) break;
			int[] t = new int[m];
			int[] s = new int[m];
			int[] d = new int[m];
			for (int i = 0; i < m; i++) {
				t[i] = sc.nextInt();
				s[i] = sc.nextInt();
				d[i] = sc.nextInt();
			}
			quickSort_ascend(t,s,d,0,m-1);
			boolean[] comp = new boolean[n+1];
			comp[1] = true;
			if (m > 0) {
				int time = t[0];
				List<Integer> list = new ArrayList<Integer>();
				for (int i = 0; i < m; i++) {
					if (t[i] == time) {
						if (comp[s[i]]) list.add(d[i]);
					} else {
						for (int k : list)
							comp[k] = true;
						list.clear();
						time = t[i];
						if (comp[s[i]]) list.add(d[i]);
					}
				}
				
				for (int k : list)
					comp[k] = true;
			}
			
			int ans = 0;
			for (int i = 1; i <= n; i++)
				if (comp[i])
					ans++;
			
			out.println(ans);
		}
	}
	
	public static void main(String[] args) {
		new Main().run();
	}

}