VCOLDWAT - Nước lạnh

Tác giả: happyboy99x

Ngôn ngữ: C++

#include <cstdio>

typedef struct {
	int l, h;
	int a[100005];
	void init() { l = 0; h = 0; }
	bool empty() { return l == h; }
	int size() { return h - l; }
	void push( int x ) { a[h++] = x; }
	int front() { return a[l]; }
	void pop() { ++l; }
} queue;

int g[100005][2];
int f[100005];
queue q;

int main() {
	int n, c;
	scanf( "%d%d", &n, &c );
	while(c--) {
		int u, v1, v2;
		scanf( "%d", &u );
		scanf( "%d%d", &g[u][0], &g[u][1] );
	}
	q.init(); q.push(1);
	for( int cnt = 1; q.empty() == false; ++cnt ) 
		for( int i = 0, _n = q.size(); i < _n; ++i ) {
			f[q.front()] = cnt;
			if ( g[q.front()][0] != 0 ) {
				q.push(g[q.front()][0]);
				q.push(g[q.front()][1]);
			}
			q.pop();
		}
	for( int i = 1; i <= n; ++i ) printf( "%d\n", f[i] );
	return 0;
}

Download