Contact - Answer

mail@pastecode.io avatar
unknown
plain_text
a year ago
888 B
1
Indexable
Never
#include <iostream>
using namespace std;

int in[101][101];
int visit[101] = {0};
int Q[100000];
int r = -1, f = -1;

void push(int x){
	r++;
	Q[r] = x;
}
int pop(){
	f++;
	return Q[f];
}
int BFS(int st){
	push(st);
	int max = 0;
	while(r != f){
		int max1 = 0;
		int r1 = r;
		int f1 = f;
		for(int i  = 0; i < r1 -f1 ; i++){
			int a = pop();
			if (a > max1) {
				max1 = a;
				max = max1;
			}
			visit[a] = 1;
			for(int j = 0; j < 101; j++){
				if(in[a][j] == 1 && visit[j] == 0){
					push(j);
				}
			}
			
		}
	}
	return max;

}
int main(){
	freopen ("input.txt","r", stdin);
	for(int tc = 1; tc <= 10; tc++){
		r = -1;
		f = -1;
		int n, st;
		cin >> n >> st;
		for(int i = 0; i < n/2; i ++){
			int from, to;
			cin >> from >> to;
			in[from][to] = 1;
		}
		cout << "#" << tc << " " << BFS(st) << endl;
	}
	return 0;
}