By aakarshmadhavan, history, 18 months ago, ,

I just need some very basic hints.

I am thinking of topological sort + BFS to start. What do you say? (No spoilers plz)

• -8

 » 18 months ago, # |   +11 Lets say you know the sum of distances from a node. Think about what happens to the sum if you go to a child or to the parent.
•  » » 18 months ago, # ^ |   0 Hi, I am not sure I understand your hint.If I know the sum of distances node X to node Y I am not sure how this helps? Thanks!
•  » » » 18 months ago, # ^ |   0 He meant that, if for some node u, you know ans[u] then what happens to ans[v] if v is a child/parent of u. There isn't too much difference between ans[u] and ans[v], can you figure that out?
 » 18 months ago, # |   0 Just use LCA.
•  » » 18 months ago, # ^ |   +3 Simple dfs would be much easier and probably faster.
•  » » » 18 months ago, # ^ |   0 Hmm. That works if he is looking for distance from a single node to the rest of the nodes. What if i is arbitrary?
•  » » » » 18 months ago, # ^ |   0 Just do dfs for all vertices, n^2 should pass for given constraints.
•  » » » » » 18 months ago, # ^ |   0 Oh yeah. You are right. I didn't note the small constraints.
•  » » » » » 18 months ago, # ^ |   0 but some judges have insanely tight time limits, maybe it will not be fast enough still
•  » » » 18 months ago, # ^ | ← Rev. 2 →   0 Can you help me with DFS approach in O(N)? I am very confused how to do it. Thank you so much
 » 18 months ago, # |   -8 yes, topological sort + bfs can be done, but it's easier and faster to just use a DFS traversal. You can remember, in the recursive function, the depth that you are currently at, and just add that to the answer. When you go to your children, increase the depth argument. This is O(n), so when you do a new traversal for every node, it will be O(n^2).
•  » » 18 months ago, # ^ |   0 Can you help me out with this DFS approach? I am trying to understand it properly. Say I do a DFS from Node X to all of its children, then if denote:DP[x][some child] = DP[some child][x] = distance from X to some Child, which ever child we are looking at.Are you saying I should then use DP[x][some child] for parents of node X?Thanks!
•  » » » 18 months ago, # ^ | ← Rev. 3 →   0 I'm saying you don't have to store it in memory. int sum = 0; bool visited[MAXN]; void dfs(int u, int depth) { visited[u] = true; sum += depth; for(int v : neighbours[u]) { if(!visited[v]) { dfs(v, depth + 1); } } } So, when you do dfs(x, 0); the variable sum will hold the solution for that node, calculated in O(n). Cheers.
 » 3 months ago, # |   0 I thought about advancement in this question. What if instead of a tree it is a connected graph with cycles allowed? 1<=N<=200000Any help would be helpful?
 » 3 months ago, # |   +8 The sum of depth of node is equal to the sum of subtree size of node.so you maybe just need to use change root DP to solve it in $O(n)$. the code#include const int maxn = 100100; typedef long long ll; std::vector to[maxn]; int size[maxn],n; ll ans[maxn],now; inline void dfs(int x,int f = 0){ size[x] = 1; for(int i : to[x]) if(i != f) dfs(i,x),size[x] += size[i]; now += size[x]; } inline void dfs2(int x,int f = 0){ ans[x] = now - n; for(int i : to[x]) if(i != f){ now -= size[x] + size[i]; size[x] -= size[i]; size[i] += size[x]; now += size[x] + size[i]; dfs2(i,x); now -= size[x] + size[i]; size[i] -= size[x]; size[x] += size[i]; now += size[x] + size[i]; } } int main(){ std::ios::sync_with_stdio(false),std::cin.tie(0); std::cin >> n; for(int i=1,x,y;i> x >> y; to[x].push_back(y); to[y].push_back(x); } dfs(0), dfs2(0); for(int i=0;i