void DFS(const TGraph& g, int v, TLine* result) { result->resize(g.size()); fill(result->begin(), result->end(), -1); TQueue q; (*result)[v] = 0; q.push(v); while (!q.empty()) { int now = q.front(); q.pop(); for (int i = 0; i < g[now].size(); ++i) { int next = g[now][i]; if (-1 == (*result)[next]) { (*result)[next] = (*result)[now] + 1; q.push(next); } } } }