Roronoa__Zoro's blog

By Roronoa__Zoro, history, 3 months ago, In English

There are n towns numbered 1 to n, and m roads connecting them. Arrays a and b are given such that road i connects towns a[i] and b[i]. Your task is to return an array of size m whose i-th element denotes the least number of roads that would be used in traveling from town 1 to town n when the i-th road cannot be used in the traversal. Return -1 as the i-th element if it is impossible to travel from town 1 to town n without using the i-th road. Graph is Undirected and all pairs (a[i] , b[i]) are distinct.

$$$Constraints$$$

$$$2 \le n \le 300$$$

$$$1 \le m \le n*(n-1)$$$

$$$ a[i] != b[i] $$$

$$$ 1 \le a[i] , b[i] \le n$$$

I tried this code but the time complexity will be $$$O(m{^2})$$$.

code

Full text and comments »

  • Vote: I like it
  • +1
  • Vote: I do not like it

By Roronoa__Zoro, history, 3 months ago, In English

You are given a tree consisting of N nodes. You are also given two arrays A and P of size N each, where the value A[i] denotes the value written on the i th node and the value P[i] denotes that there is an edge between the node i and P[i]. We say that an edge is considered good, if after deleting this edge (this will result in formation of 2 trees), the values in each of the nodes of the trees are distinct . Find the total number of good edges present in tree.

Constraints.

$$$1 \le N \le 10^{5}$$$

$$$1 \le A[i] \le 10^{5}$$$

$$$1 \le P[i] \le 10^{5}$$$

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By Roronoa__Zoro, history, 3 months ago, In English

Given $$$N$$$ cities connected by $$$N-1$$$ bidirectional roads i.e they form a tree . Cities with odd numbers are cities of Jack and even numbers cities belong to Bob. Each city has some initial popularity given by array Popularity. Now there are $$$Q$$$ tours taken by some tourists from city u to v taking the simple path.

As they travel from city u to v , the popularity of cities in between increases by x units. Let $$$P1$$$ and $$$P2$$$ be the sum of popularities of the cities of Jack and Bob after $$$Q$$$ tours . You need to print the absolute difference between P1 & P2.

$$$N \le 10^{5}$$$ $$$K \le 10^{6}$$$

Full text and comments »

  • Vote: I like it
  • +4
  • Vote: I do not like it

By Roronoa__Zoro, history, 10 months ago, In English

Below is given the code of following problem : 1907G - Lights. Can anyone explain me how the code for cyclic portion works.

Spoiler

Full text and comments »

  • Vote: I like it
  • -2
  • Vote: I do not like it

By Roronoa__Zoro, history, 10 months ago, In English

So in this problem 1294F - Три пути в дереве. The editorial solution is this :

Spoiler

Suppose our tree has 23 nodes. Let the diameter contains all the node b/w 1 and 20 (take in increasing order for simplicity). Lets take a branch from node 5 which contains the remaining nodes . So according to the editorial we use multi-source bfs and find the node which is at farthest distance . The node that will be farthest will be around node 9 or 10. But if we take this as third node the result will be (1 , 20 , (9 or 10)) but this will not increase the result we will still get 19 edges as our answer. Rather we should take a branch with is at max distance from any node on diameter. I tried this approach but it is giving MLE . Here is the code:

238471077

Full text and comments »

  • Vote: I like it
  • -7
  • Vote: I do not like it