MAXimal | |
���������: 10 Jun 2008 19:19 ���������� [������] ����� � ���������� ���� �� �������� ���������� �� ������. � ���������� ������ � ������� ��������� ����������������� ������ ���� � �����. �������� �������� �� O (N+M). ���������� ���������
����������vector < vector<int> > g; // ����
int n; // ����� ������
vector<int> color; // ���� ������� (0, 1, ��� 2)
vector<int> time_in, time_out; // "�������" ������ � ������ �� �������
int dfs_timer = 0; // "������" ��� ����������� �����
void dfs (int v) {
time_in[v] = dfs_timer++;
color[v] = 1;
for (vector<int>::iterator i=g[v].begin(); i!=g[v].end(); ++i)
if (color[*i] == 0)
dfs (*i);
color[v] = 2;
time_out[v] = dfs_timer++;
}
��� �������� ����� ���. �� ������ ������� ������� ������ � ������ �� ������� �� �����, ��� �� ��� � �� ����� ����� ������ (�� ����� ���� ����� ������ ����������� �� ������ ��������� ������ used). ��� �������� ������� ����������: vector < vector<int> > g; // ����
int n; // ����� ������
vector<char> used;
void dfs (int v) {
used[v] = true;
for (vector<int>::iterator i=g[v].begin(); i!=g[v].end(); ++i)
if (!used[*i])
dfs (*i);
}
| |