Блог пользователя salyg1n

Автор salyg1n, 8 месяцев назад, По-русски

Всем привет! Просим прощения за задержку >_<

Надеемся, что вам понравились наши задачи! Извините за то, что лимиты по времени и памяти в некоторых задачах были слишком жесткими и решения на Java не проходили С (кстати, у одного из авторов бывшый ник — "java." :D). Постараемся не допустить таких ошибок вновь.

Оставляйте впечатления и пожелания в комментариях! Спасибо за участие!

1867A — green_gold_dog, массив и перестановка

Автор: green_gold_dog

Разбор
Решение

1867B — Палиндромный XOR

Автор: ace5

Разбор
Решение

1867C-salyg1n и игра с MEX

Автор: salyg1n

Разбор
Решение

1867D-Циклические операции

Автор: ace5

Разбор
Решение

1867E1-salyg1n и массив (простая версия)

Автор: salyg1n

Разбор
Решение

1867E2-salyg1n и массив (сложная версия)

Автор: salyg1n

Разбор
Решение

1867F-Максимально не похожее дерево

Автор: green_gold_dog

Разбор
Решение
Разбор задач Codeforces Round 897 (Div. 2)
  • Проголосовать: нравится
  • +107
  • Проголосовать: не нравится

»
8 месяцев назад, # |
  Проголосовать: нравится +71 Проголосовать: не нравится

If you were wondering why am I an author but didn't make any problems: a day before the contest we decided to replace my task F (because I hate it) and to give "Most Different Tree" instead, hopefully it fits better (I guess we'll never know) :]

»
8 месяцев назад, # |
Rev. 2   Проголосовать: нравится +11 Проголосовать: не нравится

Video Editorial for A-E2 ...I hope it will helpful to Newbies and Pupils..! YOUTUBE EDITORIAL LINK

»
8 месяцев назад, # |
Rev. 2   Проголосовать: нравится +10 Проголосовать: не нравится

In 1867E2 - Salyg1n and Array (hard version) it is impossible to achieve $$$2k < x$$$ if $$$n \leq 2k$$$. However, in fact we do not need this condition. The second part of the solution still works in the case $$$k < x \leq 2k$$$.

»
8 месяцев назад, # |
  Проголосовать: нравится +21 Проголосовать: не нравится

E2 can be actually done in 51 operations instead of 57.

When k divides n then it only takes n/k queries, i.e 50 at max.

Otherwise, we can separate the first n*[n/k] elements and the remaining n%k elements. Since, n%k is even then we can divide those remaining elements in 2 parts of (n%k)/2 size. First, we ask a query where the i+k-1=n-(n%k)/2. Then after the subarray gets reversed, we again ask another query where i+k-1=n. Taking XOR of their outputs, the common elements gets nullified and we get our requried value.

This takes [n/k]+2=49+2=51 queries.

Submission E2

»
8 месяцев назад, # |
  Проголосовать: нравится +73 Проголосовать: не нравится

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Disclaimer: I'm a stupid newbie code Note: Yet plz continue reading Many accepted solutions, including this official editorial solution, are giving answers other than the correct answer (1) for the following test case: t=1 n=9 k=4 array = [1, 2, 3, 4, 5, 6, 7, 8, 9]

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

The necessity to learn C++ to literally write the same code for Problem C pisses me off. TLEs in Java :(

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Thank you for editorial <3

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

runtime error @testcase3 for D, grateful if someone can find the error ! fk this shit mann!


for _ in range(int(input())): n,k=map(int,input().split()) arr = [x - 1 for x in map(int, input().split())] if k==1: if arr==list(range(n)): print('YES') else: print('NO') else: memo={} def dfs(node,c): if node in memo: return memo[node] count[node]=c ch=arr[node] if count[ch] is None: local=dfs(ch,c+1) else: if abs(count[node]-count[ch])+1==k: local=True else: local= False memo[node]=local return local final='YES' for i in range(n): count=[None for _ in range(n)] ans=dfs(i,0) if ans is False: final='NO' break print(final)
  • »
    »
    8 месяцев назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Python has a default maximum recursion depth of 1000. Since n is of the order 2e5, it is not possible to do it in a recursive fashion. Try writing your dfs using stacks or just use while loops.

    Note that you may also try increasing the recursion depth limit ( sys.setrecursionlimit(N) ) but that would just lead to MLE.

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

E1 and E2 was possible to make more interesting by pushing a condition when (k+n)%2==0 ,worst case it would take more two moves than optimal solution.

»
8 месяцев назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

It was possible to make problem E1 and E2 more interesting by pushing extra condition (n+k)%2==0.

»
8 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

In the problem 1867E2 — Salyg1n and Array (hard version) for a case where n = 3, k = 2 and a = [4, 2, 5] the answer should be 3. But the given solution in the editorial is providing the answer 6. The interaction is given below,

1
3 2
? 1
6
? 1
6
? 1
6
! 6

Can someone explain the case?

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please explain why was this submission skipped https://codeforces.com/contest/1867/submission/222952890 ? It have gotten AC on pretests and i also sent almost identical one (added pragmas) later and it got AC.

  • »
    »
    8 месяцев назад, # ^ |
      Проголосовать: нравится +8 Проголосовать: не нравится

    Because later you submitted another submission and it also passed pretests. Only your last AC submission is tested on sistests

    • »
      »
      »
      8 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      OK, thanks. It wasn’t the case for previous contests as far as i remember though…

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
  • »
    »
    2 месяца назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    bhaiya i watched the video but not able to get the B what should i do

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

223167780

Can anyone give me an edge case which should give wrong answer for this code?

»
8 месяцев назад, # |
Rev. 4   Проголосовать: нравится +18 Проголосовать: не нравится

I was too lazy to code some general tree enumeration for F, so I got a slightly different solution.

Expand a little on the jury's observations. The answer always (except for $$$n=2$$$) looks like that: some tree that is not in $$$P(G)$$$, all its children are in $$$P(G)$$$ and there's a long bamboo attached to its root. The value of the answer is the sum of the sizes of the children.

Now, recall the hashing part of the isomorphism check procedure. We take the isomorphism classes of all children of a vertex, sort them and get the class of the whole tree based on that list. Imagine building the answer with that procedure. The resulting list should not appear among the existing classes. However, during the procedure, all prefixes of the list have to appear among them. Otherwise, we could've taken that prefix as the answer.

Thus, the answer looks like that: an existing subtree with another existing subtree attached to it as the last child. Additionally, the class of the last child should be not less than the class of the last child of the initial subtree.

So the solution could look like that. Iterate over the existing classes as the initial tree, then iterate over them again as the last child. If the last child is smaller than the last element of the initial list, skip it. If the list with the new element appended appears among the existing classes, skip it. Find the smallest tree among everything that's left.

Unfortunately, it's too slow as is. Let's optimize.

The second condition is easy to circumvent. Find all existing lists that are the current one with one element appended. You can think of it as building a trie of these lists, then traversing it. Now, you can remove them from some structure of options, then find the best option, then add them back. That would be $$$O(n)$$$ removals and additions in total.

The first condition is harder. I chose to abuse the fact the answer is really small. For each size, store the indices of the classes of all existing trees of that size in the increasing order. Now, we can iterate over the size of the option tree, then lower_bound for the index that is at least as large as the last element of the current list. If the iterator isn't at the end, update the answer and break. If you use a set for that, you can easily support the removals and additions as well.

I believe, that is $$$O(n \cdot \mathit{ans} \cdot \log n)$$$. The submission is 222999827 if anyone is curious.

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please explain why in the A problem I cannot use a multimap?

  • »
    »
    8 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I think you mixed up the position of the first element of the sorted array in the original array with the position of the first element of the original array in the sorted array.

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Do anyone know 42th subtest of test 2 of problem D. I can not pass this anyway

»
8 месяцев назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

In the editorial of F it is said that "One can see that if we generate all trees of size up to 15 we will definitely find T there."

How do you prove that we only need trees with a size no more than 15?

  • »
    »
    8 месяцев назад, # ^ |
    Rev. 3   Проголосовать: нравится +10 Проголосовать: не нравится

    Because the number of rooted trees of size 15 is more than $$$87000$$$. $$$87000 \cdot 15 > 10^6$$$, so the initial tree can't have all these trees as subtrees. Here's the sequence

»
8 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

ace5, can you please tell me for the problem 1867D - Cyclic Operations what is the motivation for converting this problem into graph . Its just something outside of my thought process that this problem can also be thought like this. So how we actually know of converting it. Is it by just applying other concepts and then checking if they are not working then try another some new concept.

Because i have been thinking like that for the answer to be YES , all the elements in the array must have been permutation of the positions and then are overlapped in a unique fashion and if this condition does not hold somewhere in between then the answer will be NO. But i do not know how we will implement it and just went blank after that.

Can you please explain it will be so kind of you as i have spent a significant amount of time but still have no idea.

»
8 месяцев назад, # |
Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

A much more sane way to implement F is to recognize that you can enumerate all RBS of length 28 to cover the euler tour of every tree of size 15 (with repetitions); there are 2.6e6 of them which barely fits in the TL. However, you can also use this method for sizes up to 14 and just use random to solve size 15 (less than 80% of size 15 trees can be subtrees), which should reduce the runtime by a factor of 3.6 and comfortably pass.

https://codeforces.com/contest/1867/submission/223380152

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I want to ask In problem E: how come this sample is accepted n=3 k=2 and the array is 1 2 4 the judges code is answering 6 when in fact it should be 7 but there is no definite way to know the answer.

  • »
    »
    8 месяцев назад, # ^ |
      Проголосовать: нравится +15 Проголосовать: не нравится

    Notice that problem statement states n and k are even numbers. It can be shown that this ensures uniqueness of the answer.

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

In E1/E2, if anyone is wondering if solution for any cases where n & k are not both even is possible or not. I tried some examples and found out that I can construct a solution if k is odd (doesn't matter whether n is odd or even). But when k is even we can prove that solution is not possible if n is odd.

The proof is as follows:

Note: Can someone please confirm my claim or prove me wrong?

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

My Solution to D: Direct an edge between i -> b_i . This edge implies that the position i in a has to have the value of b_i. Then, it's obvious that for arrays we only care about choosing paths of length k. Either the path is a cycle or it's not. If it's not a cycle, then every position i will be satisfied to its right value except for the last one. If it is a cycle, then every position will be set to its right value. This means that if there truly exists a sequence of operations that works, then the last operation MUST be a cycle of length k. However, notice that the graph is a functional graph, meaning that there is exactly one cycle and trees hanging from the vertices of that cycle (this is proven bc there are n edges and n nodes). Thus, the last operation must be a cycle of length k; this is non-negotiable. Thus, since we know a component will only have one cycle, then it must be that the cycle is of EXACTLY length k. Thus, if cycle is not of length k, we know the answer is a "NO". If it's of length k, is the answer "YES". Well, yes it is because for the non-cycle nodes we can just take paths that go in the cycle, and then finish it off by doing the cycle last. Now, since we are just finding a cycle of length k, it suffices that we just make this undirected and then confirm each component.

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

For problem F, I can't understand why I got wa for this code. Who can help me find out the problem please qwq. https://mirror.codeforces.com/contest/1867/submission/223794337

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Hi everyone its really hard for me to understand problem D. I have watched many videos and editorial but not able to understand problem D . Can anyone tell me what should I do?

  • »
    »
    8 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    So, the idea is first to understand how the operation works. You can try some examples with 1 2 3 4 and play with it(reverse order of them). You will realise: you cant get a number on its index(1 cant stay on first index, 2 on second...), and you must go in cycle to come back to 1(if you set first index to 3, index 3 must be 2 or 4 lets say 4, index 4 can be only 2 and index 2 1). After that try to do example: k = 4, array = 2 3 4 1 1, you will understand that you can do 1 operation to get 1 on fifth index and after that do operation on first 4 to solve them. So the conclusion is: if you have a good cycle of 4(cycle where all numbers remain in their indexes:3 5 4 5 1 5 5 5 example where indexes 1 3 4 5 are a good cycle, you can first solve for all other indexes that have one of those 4 numbers and then solve the cycle), but if you have a cycle of 3 when k is equal 4 you cant solve the problem(you can make only 4 cycle rotations). So the problem becomes finding good cycle indexes and all other indexes that have good cycle indexes as value and hoping that after finding all of those whole array is done. So we need to find cycles between numbers and their connections to rest of the array which is nothing more than a graph.

»
8 месяцев назад, # |
Rev. 3   Проголосовать: нравится +3 Проголосовать: не нравится

why hacks in third problem are forbidden?

»
7 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can some one explain in problem D, what is the insight that leads to constructing a directed graph with edges going from i to b[i] ? Specifically, why use edges from i to b[i] ?

»
5 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

for(int j = 0;j <= max_2;++j) { for(int k = 0;k <= max_1;++k) { t[ans + j*2 + k] = '1'; } } in B can you explain this part

»
5 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Зашло вот такое решение:

pragma GCC optimize("O3,unroll-loops")

pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

include <bits/stdc++.h>

using namespace std;

define int long long

define endl "\n"

int n; vector<vector> vec; map<vector,int> tree; vector sz;

int f(vector &a){ if(tree.count(a)){ return tree[a]; } int v=tree.size(); vec.push_back(a); int s=1; for(auto x : a){ s+=sz[x]; } sz.push_back(s); return tree[a]=v; }

signed main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; vector<vector> adj(n); for(int i=0;i<n-1;i++){ int u,v;cin>>u>>v; u--,v--; adj[u].push_back(v); adj[v].push_back(u); } if(n==2){ cout<<1<<" "<<2<<endl; return 0; } set se; auto dfs=[&](auto &&dfs,int u,int fa)->int{ vector a; for(auto v : adj[u]){ if(v==fa) continue; a.push_back(dfs(dfs,v,u)); } sort(a.begin(),a.end()); se.insert(f(a)); return f(a); }; dfs(dfs,0,-1); vector a; for(int i=1;i<=n;i++){ vector b,c; auto dfs=[&](auto &&dfs,int sum,int lst)->void{ if(sum==0){ int ret=f(b); if(!se.count(ret)){ int idx=n-i; auto work=[&](auto &&work,int cur)->int{ int now=++idx; for(auto v : vec[cur]){ int val=work(work,v); cout<<now<<" "<<val<<endl; } return now; }; for(int j=1;j<=n-i;j++){ cout<<j<<" "<<j+1<<endl; } work(work,ret); exit(0); } c.push_back(ret); }else{ for(int j=lst;j<a.size();j++){ if(sum>=sz[a[j]]){ b.push_back(a[j]); dfs(dfs,sum-sz[a[j]],j); b.pop_back(); } } } }; dfs(dfs,i-1,0); a.insert(a.begin(),c.begin(),c.end()); sort(a.begin(), a.end()); } }