Untitled

 avatar
unknown
plain_text
2 years ago
1.1 kB
2
Indexable
#include<iostream>
using namespace std;

int front =-1, rear = -1;
int connect[101][101];
int x,y;
int Qx[100000], Qd[100000];
int index[101] = {0};
void push(int x, int d)
{
	rear ++;
	Qx[rear] = x;
	Qd[rear] = d;
}

void pop(int &x, int &d)
{
	front ++;
	x = Qx[front];
	d = Qd[front];
}

int Bfs(int x, int d)
{

	push(x,d);
	int id=0, max=0;
	while (front !=rear)
	{
		pop(x,d);
		for (int y1=0; y1<100; y1++)
		{
			if (connect[x][y1] == 1 && index[y1] == 0) 
			{
				push(y1, d+1);
				index[y1] = d+1;
			}
		}

		if (id < d)
		{
			id = d;
		}
	}
	for (int i=0; i<=100; i++)
	{
		if(index[i]  == id)
		{
			if (max < i) max = i;
		}
	}
	return max;
}


int main()
{
	for (int tc=1; tc<=10; tc++)
	{
		int n, st;
		cin >> n >> st;
		for (int i=0; i<100; i++) index[i] = 0;

		for (int i=0; i<100; i++)
			for (int j=0; j<100; j++)
				connect[i][j] = -1;

		for (int i=0; i<n/2; i++)
		{
			cin >> x >> y;
			connect[x][y] = 1;
		}
		front = rear = -1;
		
		cout << "#" << tc << " " << Bfs(st,1) << endl;
	}


	return 0;
}
Editor is loading...