Обратите внимание, что если вам меньше 18 лет, вы не сможете получить доступ к этому сайту.
Проверить
Платный плата
Irma Sommer, 20
популярность: Очень низкий
0
Visitors
0
Нравится
0
друзья
Социальные аккаунты
Около Irma Sommer
BASANTPUR_LANDSCAPE_A0_2000_6-9 Sambalpur Development Authority
**Sambalpur Development Authority**
The Sambalpur Development Authority (SDA) is the principal body resp= toutv.
So we can answer each query in O(1). Complexity: O(n) memory/time for DFS; queries answered O(m).
Alternatively we can mention that we can also use LCA to determine relationship but it's not needed.
Also we need to discuss why this is optimal: can't do better than O(n + m) total time because we need to read all nodes and edges, plus answer each query at least once. The algorithm is linear in input size. Also no algorithm can be faster asymptotically due to reading input alone requiring Omega(n+m). So the algorithm is asymptotically optimal.
Also discuss that if you want to answer queries offline with many updates or modifications, you'd need data structures like dynamic trees but not needed here.
Now we need to produce a thorough explanation and present pseudocode.
Pseudo:
``` procedure processTreeQueries(N, adjacencyList, queries): // adjacencyList: vector> of size N+1 // queries: list of pairs (a,b)
// BFS to compute parent parent = arrayN+1 with zeros queue q visited = arrayN+1 false root = 1 q.push(root) visitedroot = true while not q.empty(): u = q.pop() for v in adjacencyListu: if not visitedv: visitedv = true parentv = u q.push(v)
// answer queries answers = for (a,b) in queries: if parenta == b or parentb == a: answers.append("YES") else: answers.append("NO")
return answers ```
**Complexity:**
- Building `parent` array: O(N + M). - Query answering: each query O(1) → total O(Q). - Memory usage: adjacency list O(N+M), parent array O(N).
Thus the problem is solved efficiently.
We have a c> 8MB for M=10^6. Acceptable.
Also we can use DSU to answer connectivity in O(alpha(n)). But not needed.
We'll provide code snippet in C++:
``` int n,m; cin>>n>>m; vector> g(n+1); for(int i=0;i>u>>v; gu.push_back(v); gv.push_back(u);
... // query int a,b; cin>>a>>b; if(binary_search(ga.begin(),ga.end(),b)) cout
Страна
Algeria
Информация о профиле
основной
Пол
мужчина
предпочтительный язык
английский
Видать
Рост
183cm
Цвет волос
черный
Премиум пользователи
Пожаловаться на пользователя.
Отправить стоимость подарка 50 кредиты
Ваш Supardating Баланс кредитов
0 кредиты
чат
Вы достигли своего дневного лимита, Вы можете общаться с новыми людьми после , не могу ждать? эта услуга стоит вам 30 кредиты.