Skip to content

Instantly share code, notes, and snippets.

@sourabhxyz
Created December 9, 2018 08:56
Show Gist options
  • Save sourabhxyz/25edb7a7067948832ade9192bd2635ce to your computer and use it in GitHub Desktop.
Save sourabhxyz/25edb7a7067948832ade9192bd2635ce to your computer and use it in GitHub Desktop.
/*This problem asks us to find all possible paths which form the diameter of the given tree.
So first we do a BFS/DFS from any vertex uu, and find the vertex xx which is farthest from ss.
Note xx must be an end of a longest path. Then, we do another BFS/DFS traversal from xx to
find all vertices {s}{s}which are farthest from xx. Every single vertex here, including xx,
is one worst root, and the mid point, or points if the length is even, of every single path
is/are the best roots. After that, we need to do a third traversal from any vertex in {s}
to find any remaining worst roots which are close to xx, and so were not discovered by our
second traversal.*/
#include <bits/stdc++.h>
using namespace std;
void DFS(int node, const vector<vector<int>> &adj_list,
vector<bool> &visited,
int len, int &long_dist, int start, vector<int> &path,
set<int> &best, set<int> &worst) {
path.push_back(node);
visited[node] = true;
if (len > long_dist) {
best.clear();
worst.clear();
long_dist = len;
}
if (len == long_dist) {
worst.insert(node);
worst.insert(start);
if (len % 2 == 0)
best.insert(path[len / 2]);
else {
best.insert(path[len / 2]);
best.insert(path[len / 2 + 1]);
}
}
for (int dest : adj_list[node])
if (!visited[dest]) {
DFS(dest, adj_list, visited, len + 1, long_dist,
start, path, best, worst);
}
visited[node] = false;
path.pop_back();
}
int main() {
int N;
while (scanf("%d", &N) == 1) {
vector<vector<int>> adj_list(N);
for (int i = 0; i < N; i++) {
int K;
scanf("%d", &K);
for (int j = 0; j < K; j++) {
int node;
scanf("%d", &node);
adj_list[i].push_back(node - 1);
}
}
vector<bool> visited(N, false);
queue<pair<int, int>> q;
visited[0] = true;
q.push({0, 0});
int long_dist = 0;
int potential = 0;
while (!q.empty()) {
auto front = q.front();
q.pop();
int u = front.first;
int len = front.second;
if (len > long_dist) {
long_dist = len;
potential = u;
}
for (int v : adj_list[u])
if (!visited[v]) {
visited[v] = true;
q.push({v, len + 1});
}
}
visited.assign(N, false);
vector<int> path;
set<int> best, worst;
DFS(potential, adj_list, visited, 0,
long_dist, potential, path, best, worst);
for (int node : worst)
if (node != potential) {
potential = node;
break;
}
visited.assign(N, false);
path.clear();
DFS(potential, adj_list, visited, 0,
long_dist, potential, path, best, worst);
printf("Best Roots :");
for (int root : best)
printf(" %d", root + 1);
printf("\n");
printf("Worst Roots :");
for (int root : worst)
printf(" %d", root + 1);
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment