Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
import java.io.*; import java.text.*; import java.util.*; import java.math.*; public class template { public static void main(String[] args) throws Exception { new template().run(); } public void run() throws Exception { FastScanner f = new FastScanner(); PrintWriter out = new PrintWriter(System.out); /// int n = f.nextInt(), k = f.nextInt(); par = new int[n]; if(n % 2 == 0) out.println("NO"); else { ok = true; par[gen(n, k)] = -1; if(ok) { out.println("YES"); for(int i : par) out.print(i+1 + " "); } else out.println("NO"); } /// out.flush(); } int[] par; int node; boolean ok; public int gen(int n, int k) { int me = node++; if(k < 0) ok = false; if(n == 1) { } else if(n == 3) { if(k != 0) ok = false; par[gen(1, 0)] = me; par[gen(1, 0)] = me; } else if(n == 5) { if(k != 1) ok = false; par[gen(1, 0)] = me; par[gen(3, 0)] = me; } else if(n == 7) { if(k == 0) { par[gen(3, 0)] = me; par[gen(3, 0)] = me; } else if(k == 2) { par[gen(1, 0)] = me; par[gen(5, 1)] = me; } else ok = false; } else if(n == 9) { if(k == 1) { par[gen(1, 0)] = me; par[gen(7, 0)] = me; } else if(k == 3) { par[gen(1, 0)] = me; par[gen(7, 2)] = me; } else ok = false; } else if(n == 11) { if(k == 1 || k == 3) { par[gen(3, 0)] = me; par[gen(7, k-1)] = me; } else if(k == 2) { par[gen(5, 1)] = me; par[gen(5, 1)] = me; } else if(k == 4) { par[gen(1, 0)] = me; par[gen(9, 3)] = me; } else ok = false; } else if(k <= 1) { int d = 1; while((n - 1 - d) > 2*d) d = d*2+1; par[gen(d, 0)] = me; par[gen(n-d-1, k - (d > 2*(n-1-d) ? 1 : 0))] = me; } else { int asdf = n-1; while(asdf % 2 == 0) asdf /= 2; if(asdf == 1 && k == 2) { par[gen(3, 0)] = me; par[gen(n-4, k-1)] = me; } else { par[gen(1, 0)] = me; par[gen(n-2, k-1)] = me; } } return me; } /// static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
JAVA
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
import java.io.*; import java.util.*; public class Solution { int bound = 100000; private void solve() throws Exception { powers = new HashSet<Integer>(); int pow = 1; while(pow-1 <= bound) { powers.add(pow-1); pow = pow*2; } int n = nextInt(); int k = nextInt(); possible = true; Node res = doit(n,k); if(!possible) { out.println("NO"); } else { d = new int[n]; p = new int[n]; idx = 0; visit(res, -1); out.println("YES"); for(int x : p) out.print((x+1) + " "); } } int[] d; int[] p; int idx; void visit(Node u, int par){ int cur = idx++; p[cur] = par; if(u.left == null) return; visit(u.left, cur); visit(u.right, cur); } HashSet<Integer> powers; boolean possible; Node doit(int n, int k) { Node node = new Node(); if(n == 1 && k == 0) return node; else if(k <= 1) { int pow = 1; int l = 0; int r = n-1; while(l <= n-1) { l = pow-1; r = n-1-l; if(((l <= r && r < 2*l) || (r <= l && l < 2*r)) ) { node.left = doit(l,0); node.right = doit(r,k); return node; } else if(r >= 2*l && powers.contains(l) && powers.contains(r) && k == 1) { node.left = doit(l,0); node.right = doit(r,0); return node; } pow = 2*pow; } possible = false; } else if(k == 2 && powers.contains(n-2) || k ==3 && n == 11) { if(n == 9) possible = false; node.left = doit(3,0); node.right = doit(n-4, k-1); } else { node.left = doit(1,0); node.right = doit(n-2, k-1); } return node; } public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Solution().run(); } }, "1", 1 << 27).start(); } private BufferedReader in; private PrintWriter out; private StringTokenizer tokenizer; public void run() { try { // in = new BufferedReader(new FileReader("ks_10000_0")); in = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; // out = new PrintWriter(new File("outputPQ.txt")); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private float nextFloat() throws IOException { return Float.parseFloat(nextToken()); } private String nextLine() throws IOException { return new String(in.readLine()); } private String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } } class Node { Node left; Node right; }
JAVA
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, K, ans[100004]; int lowb(int x) { return x & -x; } int main() { scanf("%d%d", &n, &K); if (n % 2 == 0) return puts("NO"), 0; if (max(0, (n - 3) / 2) < K) return puts("NO"), 0; if (n == 9 && K == 2) return puts("NO"), 0; if (lowb(n + 1) == n + 1 && K == 1) return puts("NO"), 0; if (lowb(n + 1) != n + 1 && K < 1) return puts("NO"), 0; puts("YES"); int base = 2 * max(0, K - 1); for (int i = 1; i < base; i += 2) ans[i] = max(0, i - 2), ans[i + 1] = i; for (int i = 1; i <= n - base; i++) { if (i == 1) ans[i + base] = max(0, base - 1); else ans[i + base] = i / 2 + base; } if (lowb(n - base + 1) == n - base + 1 && K) ans[n - 1] = ans[n] = 2; for (int i = 1; i <= n; i++) printf("%d ", ans[i]); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int ans[200010]; int main() { int a, b; cin >> a >> b; if (a % 2 == 0) { printf("NO"); return 0; } if (b == 0) { int k = a + 1; while (k % 2 == 0) k /= 2; if (k == 1) { printf("YES\n"); for (int i = 1; i <= a; i++) printf("%d ", i / 2); } else printf("NO"); } else if (b == 1) { int k = a + 1; while (k % 2 == 0) k /= 2; if (k != 1) { printf("YES\n"); for (int i = 1; i <= a; i++) printf("%d ", i / 2); } else printf("NO"); } else { int node = 1; ans[1] = 0; for (int i = 1; i <= b - 1; i++) { ans[node + 1] = node; ans[node + 2] = node; node += 2; } if ((a - node + 1) <= 3) { printf("NO"); return 0; } int k = (a - node + 1) + 1; while (k % 2 == 0) k /= 2; if (k != 1) { for (int i = node + 1; i <= a; i++) ans[i] = (i - node + 1) / 2 + (node - 1); printf("YES\n"); for (int i = 1; i <= a; i++) printf("%d ", ans[i]); } else { if (a == 9) { printf("NO"); return 0; } ans[a] = 2; ans[a - 1] = 2; a -= 2; for (int i = node + 1; i <= a; i++) ans[i] = (i - node + 1) / 2 + (node - 1); printf("YES\n"); for (int i = 1; i <= a + 2; i++) printf("%d ", ans[i]); } } }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int ncnt; int anc[N], lg2[N]; bool Check(int n, int m) { if (n == 1 && m == 0) return true; if (m < 0 || n < 0) return false; if (n == 9 && m == 2) return false; if (((n + 1) & (-(n + 1))) == n + 1 && m == 1) return false; if (m == 0 && ((n + 1) & (-(n + 1))) != n + 1) return false; if (!(n & 1) || (m * 2 + 3) > n) return false; return true; } int Double1(int n) { if (((n) & (-(n))) == n) return 0; n ^= ((n) & (-(n))); if (((n) & (-(n))) == n) return 1; return 2; } int HalfFull(int n) { int res = 1; while (res <= n) { if (res * 2 > n - res && 2 * (n - res) > res) return res; res = res << 1 | 1; } printf("error5 %d\n", n), exit(5); return -1; } int NotHalfFull(int n) { int res = 1; while (res <= n) { if (res * 2 <= n - res || 2 * (n - res) <= res) if (((n - res + 1) & (-(n - res + 1))) != n - res + 1) return res; res = res << 1 | 1; } printf("error6 %d\n", n), exit(6); return -1; } void DFS(int fa, int siz, int key) { if (siz <= 0) printf("error2"), exit(2); int u = ++ncnt; anc[u] = fa; if (siz == 1) { if (key) printf("error3"), exit(3); return; } if (key == 2) { int tmp = NotHalfFull(siz - 1); DFS(u, tmp, 0), DFS(u, siz - 1 - tmp, 1); } else if (key == 1) { if (Double1(siz + 1) == 1) { int tmp = siz + 1; DFS(u, ((tmp) & (-(tmp))) - 1, 0); tmp ^= ((tmp) & (-(tmp))); DFS(u, ((tmp) & (-(tmp))) - 1, 0); } else { int tmp = HalfFull(siz - 1); if (tmp * 2 <= siz - 1 - tmp || 2 * (siz - 1 - tmp) <= tmp) printf("error4 %d,%d,%d", siz, tmp, siz - 1 - tmp), exit(4); DFS(u, tmp, 0); DFS(u, siz - 1 - tmp, 1); } } else { DFS(u, siz >> 1, 0); DFS(u, siz >> 1, 0); } } void BFSolve(int fa, int n, int m) { int u = ++ncnt; anc[u] = fa; for (int i = 1; i < n; i++) for (int j = 0; j <= m; j++) { int tag = (i * 2 <= n - i - 1) || (2 * (n - i - 1) <= i); if (Check(i, j) && Check(n - i - 1, m - j - tag)) { BFSolve(u, i, j); BFSolve(u, n - i - 1, m - j - tag); return; } } } int main() { int n, m; scanf("%d%d", &n, &m); int in = n, im = m; for (int i = 2; i <= n; i++) lg2[i] = lg2[i >> 1] + 1; if (!Check(n, m)) { printf("NO\n"); return 0; } int las = ncnt = 1; while (Check(n - 2, m - 1)) { n -= 2, m--; int u = ++ncnt, v = ++ncnt; anc[u] = anc[v] = las; las = v; } ncnt--; if (n <= 11) BFSolve(anc[las], n, m); else DFS(anc[las], n, m); printf("YES\n"); for (int i = 1; i <= in; i++) printf("%d%c", anc[i], i == in ? '\n' : ' '); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k, lim, m, fa[110000]; bool check(int n) { n++; return n == (n & -n); } int main() { scanf("%d%d", &n, &k); lim = max((n - 3) / 2, 0); if (!(n & 1) || (k > lim) || (k == check(n)) || (n == 9 && k == 2)) { puts("NO"); return 0; } puts("YES"); m = max(2 * (k - 1), 0); for (int i = 1; i <= m; i++) if (i & 1) fa[i] = max(i - 2, 0); else fa[i] = max(i - 1, 0); for (int i = 1; i <= n - m; i++) { if (i == 1) fa[i + m] = max(m - 1, 0); else fa[i + m] = (i >> 1) + m; } if (check(n - m) && k) fa[n - 1] = 2, fa[n] = 2; for (int i = 1; i <= n; i++) printf("%d ", fa[i]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, m, i, j, k, fa[100005]; int f(int n) { n++; return (n ^ (n & -n)) > 0; } int g(int n) { return (n == 1) ? 0 : (n - 3) / 2; } void cover(int x, int p, int n) { fa[x] = p; if (n == 1) return; for (int k = 1; k <= n; k = k << 1 | 1) if ((k << 1 | 1) > (n - 1) / 2) { cover(x + 1, x, k); cover(x + k + 1, x, n - 1 - k); return; } } int sz[100005]; int get(int a, int b) { return a * 2 <= b || b * 2 <= a; } int check() { int cnt = 0; for (i = n; i >= 1; i--) { sz[i]++; if (sz[fa[i]]) cnt += get(sz[fa[i]], sz[i]); sz[fa[i]] += sz[i]; } if (cnt != m) printf("%d!!!\n", cnt); } void write() { printf("YES\n"); for (i = 1; i <= n; i++) printf("%d ", fa[i]); } void cover(int st, int m) { for (int i = 2; i <= m; i++) fa[st + i - 1] = st + i / 2 - 1; } int main() { scanf("%d%d", &n, &m); if (!(n & 1)) printf("NO\n"), exit(0); if (m < f(n) || m > g(n)) printf("NO\n"), exit(0); if (n == 9 && m == 2) printf("NO\n"), exit(0); if (n == 1) printf("YES\n0\n"), exit(0); if (m <= 1) { if (f(n) == m) cover(1, n), check(), write(); else printf("NO\n"); exit(0); } for (i = 0; i < (m - 1); i++) fa[2 * i + 2] = fa[2 * i + 3] = 2 * i + 1; cover(2 * m - 1, n - 2 * (m - 1)); if (!f(n - 2 * (m - 1))) fa[n] = fa[n - 1] = 2; check(); write(); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int nmax = 1e5 + 42; void wrong() { printf("NO\n"); exit(0); } bool off_one(int n) { return (n & (n + 1)) == 0; } int parent[nmax]; int low(int SZ, int LHS) { int RHS = SZ - 1 - LHS; int ret = 0; if (LHS >= 2 * RHS || RHS >= 2 * LHS) ret++; if (off_one(LHS) == 0) ret++; if (off_one(RHS) == 0) ret++; return ret; } int rec(int l, int r, int wrong) { if (wrong == 0) { for (int i = l + 1; i <= r; i++) { int actual = (i - l + 1) / 2 + l - 1; parent[i] = actual; } return l; } int SZ = r - l + 1; int LHS, le; if (wrong == 1) { LHS = (r - l + 1) - 2; while (low(SZ, LHS) > 1) LHS = LHS - 2; le = 1 - off_one(LHS); } else if (wrong == 2) { if (off_one(SZ - 2)) LHS = 3, le = 0; else LHS = 1, le = 0; } else { if (SZ == 11 && wrong == 3) LHS = 3, le = 0; else LHS = 1, le = 0; } int remain = wrong; int RHS = SZ - LHS - 1; if (LHS >= 2 * RHS || RHS >= 2 * LHS) remain--; parent[rec(l, l + LHS - 1, le)] = l + LHS; parent[rec(l + LHS + 1, r, remain - le)] = l + LHS; return l + LHS; } void solve() { int n, k; scanf("%i%i", &n, &k); if (n == 1 && k == 0) { printf("YES\n0\n"); return; } if (n % 2 == 0 || (n - 3) / 2 < k) wrong(); if (k == 0 && off_one(n) == 0) wrong(); if (k == 1 && off_one(n)) wrong(); if (k == 2 && n == 9) wrong(); rec(1, n, k); printf("YES\n"); for (int i = 1; i <= n; i++) printf("%i ", parent[i]); printf("\n"); } int main() { solve(); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k; int fa[100005]; bool is_bal(int n) { int x = __builtin_popcount(n); return (n == ((1 << x) - 1)); } bool check(int n, int k) { if (n % 2 == 0) return false; if (n == 1 && k == 0) return true; if (k > (n - 3) / 2) return false; if (k == 0 && !is_bal(n)) return false; if (k == 1 && is_bal(n)) return false; if (n == 9 && k == 2) return false; return true; } void build(int l, int r, int k) { int n = r - l + 1; if (n == 1) return; if (k == 0) { for (int i = 2; i <= n; i++) fa[i + l - 1] = i / 2 + l - 1; return; } for (int i = 1; i <= n - 2; i++) { int x = is_bal(i) ? 0 : 1; x += (max(i, n - 1 - i) >= 2 * min(i, n - 1 - i) ? 1 : 0); if (k >= x && check(n - 1 - i, k - x)) { fa[l + 1] = l; build(l + 1, l + i, is_bal(i) ? 0 : 1); fa[l + i + 1] = l; build(l + i + 1, r, k - x); return; } } assert(0); } int main() { scanf("%d%d", &n, &k); if (!check(n, k)) { puts("NO"); return 0; } puts("YES"); build(1, n, k); for (int i = 1; i <= n; i++) printf("%d ", fa[i]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int SMALL = 100; vector<vector<int>> dp; bool check(int n, int k) { if (n % 2 == 0 || k < 0) return false; if (n < SMALL) { if (find(dp[n].begin(), dp[n].end(), k) == dp[n].end()) return false; } else if (n & (n + 1)) { if (k == 0 || k >= n / 2) return false; } else { if (k == 1 || k >= n / 2) return false; } return true; } void solve(vector<int> &vec, int n, int offset, int k) { assert(check(n, k)); if (n == 1) return; for (int i = n - 2;; i -= 2) { assert(i > 0); for (int j = min(k, max(10, i / 2)); j >= 0; --j) { if ((k - j - (n > (n - i - 1) * 3)) * 2 > n - i - 1) break; if (check(i, j) && check(n - i - 1, k - j - (n > (n - i - 1) * 3))) { vec[offset + 1] = offset + 1; solve(vec, i, offset + 1, j); vec[offset + i + 1] = offset + 1; solve(vec, n - i - 1, offset + 1 + i, k - j - (n > (n - i - 1) * 3)); return; } } } assert(false); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); dp.resize(SMALL); dp[1] = {0}; for (int i = 3; i < SMALL; i += 2) { vector<int> &cur = dp[i]; for (int j = 1; j * 2 < i; j += 2) { for (int x : dp[j]) for (int y : dp[i - j - 1]) cur.push_back(x + y + (j * 3 < i)); } sort(cur.begin(), cur.end()); cur.erase(unique(cur.begin(), cur.end()), cur.end()); } int n, k; cin >> n >> k; if (not check(n, k)) return cout << "NO" << endl, 0; vector<int> par(n, 0); solve(par, n, 0, k); cout << "YES" << endl; for (int x : par) cout << x << ' '; cout << endl; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; long long inf = 1LL << 50; const int N = 100000; int cnt = 1; int children[N]; int pwrs[17]; void build(int curr, int k, int n) { if (n == 1) { return; } int lg = log2(n); if (pwrs[lg] * 2 - 1 == n && k == 0) { children[cnt++] = curr; build(cnt - 1, k, n / 2); children[cnt++] = curr; build(cnt - 1, k, n / 2); } else if (k == 1) { int lg2 = log2(n / 3) + 1; children[cnt++] = curr; build(cnt - 1, 0, pwrs[lg2] - 1); children[cnt++] = curr; if ((pwrs[lg2] - 1) * 2 < (n - pwrs[lg2])) { build(cnt - 1, 0, n - pwrs[lg2]); } else { build(cnt - 1, 1, n - pwrs[lg2]); } } else if (pwrs[lg] + 3 == n && k == 3) { children[cnt++] = curr; build(cnt - 1, 0, 3); children[cnt++] = curr; build(cnt - 1, 2, pwrs[lg] - 1); } else if (pwrs[lg] + 1 == n && k == 2) { children[cnt++] = curr; build(cnt - 1, 0, 3); children[cnt++] = curr; build(cnt - 1, 1, n - 4); } else { children[cnt++] = curr; children[cnt++] = curr; build(cnt - 1, k - 1, n - 2); } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin.exceptions(cin.failbit); int n, k; cin >> n >> k; if (n % 2 == 0 || k > max(0, (n - 3) / 2)) { cout << "NO\n"; return 0; } pwrs[0] = 1; for (int i = 1; i < 17; i++) { pwrs[i] = pwrs[i - 1] * 2; } int lg = log2(n); if (pwrs[lg] * 2 - 1 != n && k == 0) { cout << "NO\n"; return 0; } if (pwrs[lg] * 2 - 1 == n && k == 1) { cout << "NO\n"; return 0; } if (9 == n && k == 2) { cout << "NO\n"; return 0; } children[0] = -1; build(0, k, n); cout << "YES\n"; for (int i = 0; i < n; i++) { cout << children[i] + 1 << " "; } cout << "\n"; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k, p[200001]; int low(int x) { return x & -x; } int check() { if (n % 2 == 0) return 0; if (n == 9 && k == 2) return 0; if (k > (n - 3) / 2) return 0; if (low(n + 1) == n + 1 && k == 1) return 0; if (low(n + 1) != n + 1 && k == 0) return 0; return 1; } int main() { cin >> n >> k; if (check() || (n == 1 && k == 0)) { cout << "YES" << endl; if (low(n + 1) == n + 1 && k == 0) { for (int i = 1; i <= n; i++) p[i] = i / 2; } else if (low(n - 2 * (k - 1) + 1) != n - 2 * (k - 1) + 1) { for (int i = 1; i <= k - 1; i++) p[i] = i - 1; for (int i = k; i <= (k - 1) * 2; i++) p[i] = i - (k - 1); p[(k - 1) * 2 + 1] = k - 1; for (int i = 2 * k; i <= n; i++) p[i] = (i - (2 * (k - 1))) / 2 + 2 * (k - 1); } else { for (int i = 1; i <= k - 1; i++) p[i] = i - 1; for (int i = k; i <= (k - 1) * 2; i++) p[i] = i - (k - 1); p[(k - 1) * 2 + 1] = k - 1; for (int i = 2 * k; i <= n - 2; i++) p[i] = (i - (2 * (k - 1))) / 2 + 2 * (k - 1); p[n - 1] = p[n] = k; } for (int i = 1; i <= n; i++) printf("%d ", p[i]); } else cout << "NO"; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; vector<int> ans; const int mod = 1e9 + 7; bool hasSoln(int n, int k) { if ((n & 1) == 0) return false; if (k == 0 && (n & (n + 1)) != 0) return false; if (k == 1 && (n & (n + 1)) == 0) return false; if (n == 1 && k != 0) return false; if (n == 1 && k == 0) return true; if (k > (n - 3) / 2) return false; if (k == 2 && n == 9) return false; return true; } void fill(int i, int n, int k) { if (n == 1) return; if (k > 2) { if (k == 3 && n == 11) { ans[i + 1] = i; ans[i + 4] = i; fill(i + 1, 3, 0); fill(i + 4, 7, 2); } else { ans[i + 1] = i; ans[i + 2] = i; fill(i + 2, n - 2, k - 1); } } else if (k == 2) { if (hasSoln(n - 2, k - 1)) { ans[i + 1] = i; ans[i + 2] = i; fill(i + 2, n - 2, k - 1); } else { ans[i + 1] = i; ans[i + 4] = i; fill(i + 1, 3, 0); fill(i + 4, n - 4, k - 1); } } else if (k == 1) { int m = (int)log2(n); int num1 = (1 << m); int num2 = n + 1 - num1; if (num2 * 2 == num1) { ans[i + 1] = i; ans[i + num2] = i; fill(i + 1, num2 - 1, 0); fill(i + num2, num1 - 1, 0); } else { if (num2 * 2 < num1) { num1 = (1 << (m - 1)); num2 = n + 1 - num1; } ans[i + 1] = i; ans[i + num1] = i; fill(i + 1, num1 - 1, 0); fill(i + num1, num2 - 1, 1); } } else { int hn = (n - 1) / 2; ans[i + 1] = i; ans[i + 1 + hn] = i; fill(i + 1, hn, 0); fill(i + 1 + hn, hn, 0); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; if (hasSoln(n, k)) { cout << "YES\n"; ans.resize(n + 1); ans[1] = 0; fill(1, n, k); for (int i = 1; i <= n; i++) cout << ans[i] << " "; cout << "\n"; } else cout << "NO\n"; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } template <typename T, size_t size> ostream &operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; } template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } template <typename T> void output_vector(const vector<T> &v, bool add_one = false, int start = -1, int end = -1) { if (start < 0) start = 0; if (end < 0) end = int(v.size()); for (int i = start; i < end; i++) cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n'); } bool is_power_of_two(int n) { return (n & (n - 1)) == 0; } vector<int> solution; bool possible(int n, int k) { if (n <= 0 || n % 2 == 0 || k < 0) return false; if (n <= 3) return k == 0; if (n == 5) return k == 1; if (n == 7) return k == 0 || k == 2; if (n == 9) return k == 1 || k == 3; int bad = is_power_of_two(n + 1) ? 1 : 0; return k <= (n - 3) / 2 && k != bad; } void build(int n, int k, int index, int parent) { solution[index] = parent; ; assert(n > 0 && possible(n, k)); if (n == 1) return; if (n <= 11) { for (int a = 1; a <= (n - 1) / 2; a += 2) { int b = n - 1 - a; int now = 2 * a <= b; for (int x = 0; x + now <= k; x++) if (possible(a, x) && possible(b, k - x - now)) { build(a, x, index + 1, index); build(b, k - x - now, index + a + 1, index); return; } } } if (k == 0) { assert(is_power_of_two(n + 1)); build(n / 2, 0, index + 1, index); build(n / 2, 0, index + n / 2 + 1, index); return; } for (int p = 2; p - 1 < n - 1; p *= 2) { int option = p - 1; int other = n - 1 - option; assert(possible(option, 0)); int now = (2 * option <= other) || (2 * other <= option); int need = k - now; if (possible(other, need)) { build(option, 0, index + 1, index); build(other, need, index + option + 1, index); return; } } assert(k > 1); build(1, 0, index + 1, index); build(n - 2, k - 1, index + 2, index); return; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, K; cin >> N >> K; if (!possible(N, K)) { cout << "NO" << '\n'; return 0; } cout << "YES" << '\n'; solution.assign(N, -1); build(N, K, 0, -1); output_vector(solution, true); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int sz = 1e5 + 10; int pr[sz], nu = 2; void f(int n, int k, int rt) { if (n > 1) { if (k == 0) { pr[nu] = rt, f(n / 2, k, nu++); pr[nu] = rt, f(n / 2, k, nu++); } else if (k == 1) { for (int x = 1;; x = x * 2 + 1) { int y = n - 1 - x; if (x * 2 + 1 == y) { pr[nu] = rt, f(x, 0, nu++); pr[nu] = rt, f(y, 0, nu++); break; } if (min(x, y) * 2 > max(x, y)) { pr[nu] = rt, f(x, 0, nu++); pr[nu] = rt, f(y, 1, nu++); break; } } } else if (k == 2) { if (__builtin_popcount(n - 1) != 1) { pr[nu++] = rt, pr[nu] = rt, f(n - 2, k - 1, nu++); } else { pr[nu] = rt, f(3, 0, nu++); pr[nu] = rt, f(n - 4, 1, nu++); } } else if (k == 3 and n == 11) { pr[nu] = rt, f(3, 0, nu++); pr[nu] = rt, f(7, 2, nu++); } else { pr[nu++] = rt, pr[nu] = rt, f(n - 2, k - 1, nu++); } } } int main() { int n, k; cin >> n >> k; if (n == 1 and k == 0) { cout << "YES\n0"; return 0; } if (n % 2 == 0) { cout << "NO"; return 0; } if ((n - 3) / 2 < k) { cout << "NO"; return 0; } if (__builtin_popcount(n + 1) == 1 and k == 1) { cout << "NO"; return 0; } if (__builtin_popcount(n + 1) != 1 and k == 0) { cout << "NO"; return 0; } if (n == 9 and k == 2) { cout << "NO"; return 0; } pr[1] = 0, f(n, k, 1); cout << "YES\n"; for (int a = 1; a <= n; a++) printf("%d ", pr[a]); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; using namespace std; const int maxn = 100000 + 10; const int inf = 2000000000; int n, k; int vis[1 << 19]; inline int gao(int n, int k) { if (k < 0) return 0; if (k == 1) { return !vis[n]; } if (k == 0) { return vis[n]; } if (n == 9 && k == 2) return 0; return (n - 3) / 2 >= k; } int cnt; int pre[maxn]; void print(int n, int k, int fa) { int x; pre[x = cnt++] = fa; if (n == 1) return; for (int i = 1; i < n; i *= 2, i++) { int j = n - i - 1; int kk = k; if (min(i, j) * 2 <= max(i, j)) kk--; if (gao(i, 0) && gao(j, kk)) { print(i, 0, x); print(j, kk, x); return; } } } inline int solve() { if (!gao(n, k)) { puts("NO"); return 0; } cnt = 1; print(n, k, 0); return 1; } int main(void) { scanf("%d%d", &n, &k); for (int i = 1; i < (1 << 19); i *= 2, i++) vis[i] = 1; int f = solve(); if (!f) return f; if (cnt != n + 1) { puts("NO"); return 0; } puts("YES"); for (int i = (1); i <= (n); ++i) { printf("%d ", pre[i]); } puts(""); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &num) { T x = 0, f = 1; char ch = getchar(); for (; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') f = -1; for (; ch <= '9' && ch >= '0'; ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ '0'); num = x * f; } int n, k, tot; void NO() { puts("NO"); exit(0); } bool OK(int x) { return x - (x & (-x)) == 0; } int main() { read(n); read(k); if (n % 2 == 0) NO(); if (k > max((n - 3) / 2, 0)) NO(); if (n == 9 && k == 2) NO(); if (OK(n + 1) && k == 1) NO(); if (!OK(n + 1) && !k) NO(); if (n == 1) { puts("YES\n0\n"); return 0; } if (OK(n + 1) && !k) { puts("YES"); for (int i = 1; i <= n; i++) { printf("%d ", tot + (i >> 1)); } return 0; } puts("YES"); for (int i = 1, j = 0; i < k; i++) { printf("%d %d ", j, ++tot); j = tot++; } if (!OK(n - tot + 1)) { printf("%d ", tot ? tot - 1 : 0); for (int i = 2; i <= n - tot; i++) { printf("%d ", tot + (i >> 1)); } } else { printf("%d ", tot ? tot - 1 : 0); for (int i = 2; i <= n - tot - 2; i++) { printf("%d ", tot + (i >> 1)); } printf("2 2"); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; bool Right(int n, int k) { if (n % 2 == 0) return false; if (n == 1) return k == 0; bool pw2 = ((n + 1) & n) == 0; if (pw2 && k == 1) return false; if (!pw2 && k == 0) return false; if (k > (n - 3) / 2) return false; if (n == 9 && k == 2) return false; return true; }; vector<pair<int, int>> leng(int n) { vector<pair<int, int>> a; bool pw2 = ((n + 1) & n) == 0; if (n == 9) { a.emplace_back(1, 1); a.emplace_back(3, 3); } else if (pw2) { a.emplace_back(0, 0); if (n > 3) a.emplace_back(2, (n - 3) / 2); } else { a.emplace_back(1, (n - 3) / 2); } return a; }; int main() { int n, k; cin >> n >> k; if (!Right(n, k)) { cout << "NO\n"; return 0; } cout << "YES\n"; int cnt = 0; auto get = [&](int n, int l, int k) { int r = n - l - 1; int g = (2 * l <= r); k -= g; auto a = leng(l); auto b = leng(r); int x = -1, y = -1; for (auto [l1, r1] : a) { for (auto [l2, r2] : b) { if (l1 + l2 <= k && k <= r1 + r2) { if (k <= r1 + l2) { y = l2; x = k - y; } else { x = r1; y = k - x; } } } } return make_pair(x, y); }; int res = 0; function<void(int, int, int)> build = [&](int n, int k, int p) { cout << p << " "; int x = ++cnt; if (n == 1) return; for (int l = 1;; l += 2) { auto [a, b] = get(n, l, k); if (a == -1) continue; assert(l <= n - l - 1); res += (2 * l <= n - l - 1); build(l, a, x); build(n - l - 1, b, x); return; } }; build(n, k, 0); cout << "\n"; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; bool isp2(int n) { while (n % 2 == 0) n /= 2; return n == 1; } int maxk(int n) { return (n - 3) / 2; } bool ok(int n, int k) { if (n < 0 || k < 0 || n % 2 == 0) return false; if (k == 0) { return isp2(n + 1); } if (k == 1) { return !isp2(n + 1); } return k <= maxk(n) && !((n == 7 && k == 1) || (n == 9 && k == 2)); } pair<int, int> getl(int n, int k) { for (int l = 1; l <= min(11, n / 2); l += 2) { int r = n - 1 - l; int add = 2 * l <= r; int sum = k - add; for (int k1 = 0; k1 <= maxk(l); ++k1) { if (ok(l, k1) && ok(r, sum - k1)) { return pair<int, int>(l, k1); } } } for (int l2 = 1; l2 < n; l2 = 2 * l2 + 1) { int r = n - 1 - l2; int add = 2 * min(l2, r) <= max(l2, r); int sum = k - add; if (ok(r, sum)) { if (l2 <= r) return pair<int, int>(l2, 0); else return pair<int, int>(r, sum); } } int l = n / 2, r = n - l - 1; int add = 2 * l <= r; int sum = k - add; int k1 = min(maxk(l), sum - 1); return pair<int, int>(l, k1); } void solve(vector<int>& v, int s0, int n, int k, int pr) { v[s0] = pr; if (n == 1) { return; } assert(ok(n, k)); pair<int, int> p = getl(n, k); int l = p.first, kl = p.second; int r = n - 1 - l; int add = 2 * l <= r; int kr = k - kl - add; solve(v, s0 + 1, l, kl, s0 + 1); solve(v, s0 + 1 + l, r, kr, s0 + 1); } void solve(int n, int k) { if (!ok(n, k)) { cout << "NO\n"; return; } if (k == 0) { if (isp2(n + 1)) { cout << "YES\n"; for (int i = 0; i < n; ++i) cout << (i + 1) / 2 << ' '; cout << endl; } else { cout << "NO\n"; } return; } vector<int> s(n); solve(s, 0, n, k, 0); cout << "YES\n"; for (int x : s) cout << x << ' '; cout << endl; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); if (0) for (int n = 1; n < 1000; ++n) for (int k = 0; k < 1000; ++k) { cerr << n << ' ' << k << endl; solve(n, k); } int n, k; cin >> n >> k; solve(n, k); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 100100; int n, k; void end() { puts("NO"); exit(0); } inline bool check(int x) { return x == (x & (-x)); } int fa[N]; int main() { scanf("%d%d", &n, &k); int lim = max(0, (n - 3) / 2); if (n % 2 == 0) end(); if (k > lim) end(); if (check(n + 1) && k == 1) end(); if (!check(n + 1) && k == 0) end(); if (n == 9 && k == 2) end(); int pos = 2 * max(0, k - 1); for (int i = 1; i < pos; i += 2) { fa[i + 1] = i; fa[i] = max(0, i - 2); } for (int i = 1; i <= n - pos; ++i) { if (i == 1) { fa[i + pos] = max(0, pos - 1); } else fa[i + pos] = (i >> 1) + pos; } if (check(n - pos + 1) && k) { fa[n - 1] = fa[n] = 2; } puts("YES"); for (int i = 1; i <= n; ++i) printf("%d ", fa[i]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k, ans[100050]; inline int qr() { char a = 0; int w = 1, np = 0; while (a < '0' || a > '9') { if (a == '-') w = -1; a = getchar(); } while (a <= '9' && a >= '0') { np = (np << 3) + (np << 1) + (a ^ 48); a = getchar(); } return np * w; } int main() { n = qr(), k = qr(); int sj = max((n - 3) >> 1, 0); if (k > sj) return printf("NO\n"), 0; if (!(n % 2)) return printf("NO\n"), 0; if (n == 9 && k == 2) return printf("NO\n"), 0; if (n + 1 != ((n + 1) & -(n + 1)) && !k) return printf("NO\n"), 0; if (n + 1 == ((n + 1) & -(n + 1)) && k == 1) return printf("NO\n"), 0; printf("YES\n"); int a = max(0, k - 1) << 1; for (register int i = 1; i < a; i += 2) ans[i] = max(0, i - 2), ans[i + 1] = i; for (register int i = 1; i <= n - a; i++) ans[i + a] = i == 1 ? max(0, a - 1) : (i >> 1) + a; if (n - a + 1 == ((n - a + 1) & (-(n - a + 1))) && k) ans[n] = ans[n - 1] = 2; for (register int i = 1; i <= n; i++) printf("%d%c", ans[i], " \n"[i == n]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; const int mod = 1000000007; int lowbit(int x) { return x & (-x); } int fast_power(int a, int b) { int x; for (x = 1; b; b >>= 1) { if (b & 1) x = 1ll * x * a % mod; a = 1ll * a * a % mod; } return x % mod; } int n, m; int ans[100005]; inline bool check(int cntNode, int cntCri) { if (cntNode % 2 == 0) return 0; if (!cntCri) return !(cntNode & (cntNode + 1)); if (cntCri == 1) return (cntNode & (cntNode + 1)); if (cntNode == 9 && cntCri == 2) return 0; return cntCri > 0 && cntCri <= (cntNode - 3) / 2; } void dfs(int u, int t, int s, int fa) { ans[u] = fa; if (t == 1) return; for (int lsz = 1; lsz < t; lsz = lsz * 2 + 1) { int rsz = t - lsz - 1; int rs = s - (max(lsz, rsz) >= 2 * min(lsz, rsz)); if (check(lsz, 0) && check(rsz, rs)) { dfs(u + 1, lsz, 0, u); dfs(u + lsz + 1, rsz, rs, u); return; } } } int main() { scanf("%d%d", &n, &m); if (check(n, m)) { puts("YES"); dfs(1, n, m, 0); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); puts(""); } else puts("NO"); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x * f; } int n, k, f[100010], cnt; bool can(int n, int k) { if (n <= 3) return !k; if (n == 9 && k == 2) return false; return k <= n / 2 - 1 && k ^ (!(bool)(n & (n + 1))); } inline int Get(int n) { if (!n) return 0; return (1LL * rand() * rand() + rand()) % n; } int dfs(int n, int k) { if (n == 1) { return ++cnt; } if (!k) { int nl = dfs(n >> 1, k); int nr = dfs(n >> 1, k); return f[nl] = f[nr] = ++cnt; } if (n > 3 && can(n - 2, k - 1)) { int nl = dfs(n - 2, k - 1); int nr = ++cnt; return f[nl] = f[nr] = ++cnt; } while ("MYH AK IOI") { int l = Get(n / 2) << 1 | 1, r = n - l - 1; int c = ((l << 1) <= r || (r << 1) <= l); int p = Get(k + 1 - c); if (can(l, p) && can(r, k - p - c)) { int nl = dfs(l, p); int nr = dfs(r, k - p - c); return f[nl] = f[nr] = ++cnt; } } } int main() { n = read(), k = read(); if (!(n & 1) || !can(n, k)) puts("NO"); else { srand(time(0)); puts("YES"); dfs(n, k); for (int i = 1; i <= n; ++i) { printf("%d ", f[i]); } } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int maxN = 1e5 + 10; int par[maxN]; int n, k; bool isPower(int n) { int dd = (n + 1); return ((dd & (dd - 1)) == 0); } bool can[105][105]; bool ok(int n, int k) { if (n < 100) { if (!can[n][k]) { return false; } } else { if (k == 0) { if (!isPower(n)) { return false; } } if (k == 1) { if (isPower(n)) { return false; } } if (k > (n - 3) / 2) { return false; } } return true; } void solve(int l, int r, int k) { int n = (r - l + 1); if (n == 1) { assert(k == 0); return; } assert((n - 3) / 2 >= k && k >= 0); if (n <= 40) { for (int c1 = 1; c1 < n; c1 += 2) { int c2 = n - 1 - c1; int fl = max(c1, c2) >= 2 * min(c1, c2); for (int p = 0; p + fl <= k && p <= c1; p++) { if (can[c1][p] && can[c2][k - fl - p]) { par[l + 1] = l; par[l + c1 + 1] = l; solve(l + 1, l + c1, p); solve(l + c1 + 1, r, k - fl - p); return; } } } assert(false); } if (k == 0) { assert(isPower(n)); int c1 = (n - 1) / 2; par[l + 1] = l; par[l + c1 + 1] = l; solve(l + 1, l + c1, 0); solve(l + c1 + 1, r, 0); return; } else { for (int c1 = 1; c1 < n; c1 += 2) { int c2 = n - 1 - c1; int fl = max(c1, c2) >= 2 * min(c1, c2); for (int p = 0; p + fl <= k && p <= c1; p++) { if (ok(c1, p) && ok(c2, k - fl - p)) { par[l + 1] = l; par[l + c1 + 1] = l; solve(l + 1, l + c1, p); solve(l + c1 + 1, r, k - fl - p); return; } } } assert(false); } } void go() { can[1][0] = true; for (int i = 3; i < 100; i += 2) { for (int c1 = 1; c1 < (i - 1); c1 += 2) { int c2 = i - 1 - c1; int fl = (max(c1, c2) >= 2 * min(c1, c2)); for (int d = 0; d <= c1; d++) { for (int p = 0; p <= c2; p++) { if (can[c1][d] && can[c2][p]) { can[i][d + p + fl] = true; } } } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); go(); cin >> n >> k; if (n % 2 == 0) { cout << "NO\n"; return 0; } if (!ok(n, k)) { cout << "NO\n"; return 0; } cout << "YES\n"; solve(1, n, k); for (int i = 1; i <= n; i++) cout << par[i] << " "; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> #pragma GCC Optimize(2) using namespace std; int n, k, id; inline int lowbit(int x) { return x & (-x); } inline bool isfull(int x) { return (x + 1) == lowbit(x + 1); } inline int lowerX(int x) { while (x != lowbit(x)) x -= lowbit(x); return x - 1; } inline int upperX(int x) { while (!isfull(x)) x += lowbit(~x); return x; } void writefull(int n, int f) { if (n < 1) return; int now = ++id; printf("%d ", f); if (n == 1) return; writefull(n / 2, now); writefull(n / 2, now); } void writeimb(int n, int k, int f) { if (n < 1) return; if (k == 0) { writefull(n, f); return; } int now = ++id; printf("%d ", f); if (k > 1) { if (isfull(n - (k - 1) * 2)) { writefull(3, now); writeimb(n - 4, k - 1, now); } else { writefull(1, now); writeimb(n - 2, k - 1, now); } } else if (k == 1) { int l = n / 2; if (!isfull(l)) { l = lowerX(l); if (l * 2 <= n - 1 - l && !isfull(n - 1 - l)) l = (l << 1) | 1; } writefull(l, now); writeimb(n - 1 - l, k - (l * 2 <= n - 1 - l || (n - 1 - l) * 2 <= l ? 1 : 0), now); } } int main() { scanf("%d%d", &n, &k); if ((n & 1) == 0 || n > 1 && n < 2 * k + 3 || isfull(n) && k == 1 || !isfull(n) && k == 0 || n == 9 && k == 2) { puts("NO"); return 0; } puts("YES"); writeimb(n, k, 0); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); mt19937 rnd(seed); const int MOD = 998244353; void dp(vector<int> &res, int k, int s, int e, int p) { if (s > e) return; int num = e - s + 1; res[s] = p; if (num == 1) return; int a = 1; if (k <= 1) { int p = 2; do { int b = num - p; int t = b + 1; int nlb = (t & (-t)) == t ? 0 : 1; int cnt = 0; if (b >= 2 * (p - 1) || p - 1 >= 2 * b) cnt++; if (cnt + nlb == k) break; p *= 2; } while (p < num); a = p - 1; } else if (k == 2 && ((num - 1) & (-num + 1)) == num - 1) { a = 3; } else if (num == 11 && k == 3) { a = 3; } dp(res, 0, s + 1, s + 1 + a - 1, s); int b = num - a - 1; if (a >= 2 * b || a * 2 <= b) { dp(res, k - 1, s + 1 + a, e, s); } else dp(res, k, s + 1 + a, e, s); } bool good(int n, int k) { if (n % 2 == 0) return false; if (n == 9) return k == 1 || k == 3; int t = n + 1; if ((t & -t) == t) { if (k == 1) return false; if (k == 0) return true; } int d2 = max(0, (n - 3) / 2); int d1 = 1; return k >= d1 && k <= d2; } void solve() { int n, k; cin >> n >> k; if (!good(n, k)) { cout << "NO\n"; return; } cout << "YES\n"; vector<int> res(n); dp(res, k, 0, n - 1, -1); for (int i = 0; i < n; i++) { if (i > 0) cout << " "; cout << res[i] + 1; } cout << "\n"; } int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.precision(10); int T = 1; for (int i = 1; i <= T; i++) { solve(); } cout.flush(); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> const int MAXN = 100005; using namespace std; int pa[MAXN], is_bal[MAXN]; vector<int> bal; int exist(int n, int k) { if (n % 2 == 0) return 0; if (n == 9 && k == 2) return 0; if (n == 1) return k == 0; if (k >= n / 2) return 0; if (is_bal[n]) return k != 1; return k != 0; } void build(int n, int k, int root) { if (n == 1) { return; } for (int l : bal) { int r = n - 1 - l; if (r <= 0) break; int imb = (min(l, r) * 2 < max(l, r)); if (imb > k) continue; if (exist(r, k - imb)) { build(l, 0, root + 1); build(r, k - imb, root + 1 + l); pa[root + 1] = pa[root + 1 + l] = root; return; } } assert(0); } int main() { bal.push_back(1); is_bal[1] = 1; while (bal.back() * 2 + 1 <= MAXN) { bal.push_back(bal.back() * 2 + 1); is_bal[bal.back()] = 1; } int n, k; cin >> n >> k; if (!exist(n, k)) { cout << "NO\n"; } else { cout << "YES\n"; build(n, k, 1); for (int i = 1; i <= n; i++) { cout << pa[i] << " \n"[i == n]; } } }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k; inline bool check(int n, int k) { if (n % 2 == 0) return false; if (k == 0) return !(n & (n + 1)); if (k == 1) return n & (n + 1); if (n == 9 && k == 2) return false; return k > 0 && k <= max(0, (n - 3) / 2); } int cnt; void dfs(int n, int k, int fa) { printf("%d ", fa); int x = ++cnt; for (int i = 1; i < n - 1; i = i * 2 + 1) { int j = n - 1 - i; int y = k - (max(i, j) >= min(i, j) * 2); if (check(j, y)) { dfs(i, 0, x); dfs(j, y, x); return; } } } int main() { scanf("%d%d", &n, &k); if (!check(n, k)) return printf("No"), 0; puts("Yes"); dfs(n, k, 0); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; bool Right(int n, int k) { if (n % 2 == 0) return false; if (n == 1) return k == 0; bool pw2 = ((n + 1) & n) == 0; if (pw2 && k == 1) return false; if (!pw2 && k == 0) return false; if (k > (n - 3) / 2) return false; if (n == 9 && k == 2) return false; return true; }; vector<pair<int, int>> leng(int n) { vector<pair<int, int>> a; bool pw2 = ((n + 1) & n) == 0; if (n == 9) { a.emplace_back(1, 1); a.emplace_back(3, 3); } else if (pw2) { a.emplace_back(0, 0); if (n > 3) a.emplace_back(2, (n - 3) / 2); } else { a.emplace_back(1, (n - 3) / 2); } return a; }; int main() { int n, k; cin >> n >> k; if (!Right(n, k)) { cout << "NO\n"; return 0; } cout << "YES\n"; int cnt = 0; auto get = [&](int n, int l, int k) { int r = n - l - 1; int g = (2 * l <= r); k -= g; auto a = leng(l); auto b = leng(r); int x = -1, y = -1; for (auto [l1, r1] : a) { for (auto [l2, r2] : b) { if (l1 + l2 <= k && k <= r1 + r2) { if (k <= r1 + l2) { y = l2; x = k - y; } else { x = r1; y = k - x; } } } } return make_pair(x, y); }; int res = 0; function<void(int, int, int)> build = [&](int n, int k, int p) { cout << p << " "; int x = ++cnt; if (n == 1) return; for (int l = 1;; l += 2) { auto [a, b] = get(n, l, k); if (a == -1) continue; res += (2 * l <= n - l - 1); build(l, a, x); build(n - l - 1, b, x); return; } }; build(n, k, 0); cout << "\n"; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e5 + 100; const long long mod = 1e9 + 7; const long long inf = 1e18; long long t, a[maxn]; bool isp[maxn]; bool ok(long long x, long long y) { if (x == 1 && y == 0) return 1; if (x % 2 == 0) return 0; if (y > (x - 3) / 2) return 0; if (x == 9 && y == 2) return 0; if (y < 0) return 0; if (y == 0) return isp[x + 1]; if (y == 1) return !isp[x + 1]; return 1; } void tr(long long v, long long x) { if (x == 0) return; long long u = ++t; a[u] = v; tr(u, x / 2); tr(u, x / 2); return; } void solve(long long v, long long x, long long y) { long long u = ++t; a[u] = v; for (long long i = 1; i < x - 1; i = i * 2 + 1) { long long z = y - (2 * i <= x - 1 - i || i >= (x - 1 - i) * 2); if (ok(x - 1 - i, z)) { tr(u, i); solve(u, x - i - 1, z); return; } } return; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long x = 1; while (x < maxn) { isp[x] = 1; x <<= 1; } long long n, k; cin >> n >> k; if (!ok(n, k)) { cout << "NO\n"; return 0; } cout << "YES\n"; solve(0, n, k); for (long long i = 1; i <= n; i++) { cout << a[i] << " "; } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; bool pt(int n) { return !(n & (n - 1)); } bool check(int n, int k) { if (n <= 0 or n % 2 == 0 or k < 0) return false; if (n <= 3) return k == 0; if (n == 5) return k == 1; if (n == 7) return k == 0 or k == 2; if (n == 9) return k == 1 or k == 3; return k <= (n - 3) / 2 and k != pt(n + 1); } vector<int> ans; void build(int n, int k, int i, int par) { ans[i] = par; if (n == 1) return; if (n <= 11) { for (int a = 1; a <= (n - 1) / 2; a += 2) { int b = n - 1 - a; int rt = 2 * a <= b; for (int x = 0; x + rt <= k; ++x) { if (check(a, x) and check(b, k - x - rt)) { build(a, x, i + 1, i); build(b, k - x - rt, i + a + 1, i); return; } } } } if (k == 0) { build(n / 2, 0, i + 1, i); build(n / 2, 0, i + n / 2 + 1, i); return; } for (int p = 2; p - 1 < n - 1; p *= 2) { int a = p - 1; int b = n - 1 - a; int rt = (2 * a <= b) or (2 * b <= a); if (check(b, k - rt)) { build(a, 0, i + 1, i); build(b, k - rt, i + a + 1, i); return; } } build(1, 0, i + 1, i); build(n - 2, k - 1, i + 2, i); } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n, k; cin >> n >> k; if (!check(n, k)) { cout << "NO"; return 0; } cout << "YES\n"; ans.resize(n); build(n, k, 0, -1); for (int i = 0; i < n; ++i) { cout << ans[i] + 1 << ' '; } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> const int MAXN = 1e5; int n, K, fa[MAXN + 5]; int main() { scanf("%d %d", &n, &K); if (!(n & 1)) return puts("NO"), 0; if (!K) { if (n & (n + 1)) return puts("NO"), 0; puts("YES"); for (int i = 1; i <= n; ++i) printf("%d%c", i >> 1, i ^ n ? ' ' : '\n'); return 0; } if (2 * K + 3 > n) return puts("NO"), 0; for (int i = 2; i <= 2 * K - 2; i += 2) fa[fa[i] = i - 1] = i - 3; fa[2 * K - 1] = 2 * K - 3; int ontr = n - 2 * K + 2; if (!(ontr & (ontr + 1))) { if (n <= 9 || K == 1) return puts("NO"), 0; ontr -= 2, fa[n] = fa[n - 1] = 2; } for (int i = 2; i <= ontr; ++i) fa[i + 2 * K - 2] = 2 * K - 2 + (i >> 1); puts("YES"), fa[1] = 0; for (int i = 1; i <= n; ++i) printf("%d%c", fa[i], i ^ n ? ' ' : '\n'); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5, mod = 998244353; template <class o> inline void qr(o &x) { x = 0; char c = getchar(); int f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + (c ^ 48); c = getchar(); } x *= f; } template <class o> void qw(o x) { if (x / 10) qw(x / 10); putchar(x % 10 + 48); } template <class o> void pr1(o x) { if (x < 0) putchar('-'), x = -x; qw(x); putchar(' '); } template <class o> void pr2(o x) { if (x < 0) putchar('-'), x = -x; qw(x); puts(""); } inline int ksm(int a, int b = mod - 2) { int ans = 1; for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) ans = 1ll * ans * a % mod; return ans; } inline int add(int a, int b) { return a += b, a >= mod ? a - mod : a; } inline int sub(int a, int b) { return a -= b, a < 0 ? a + mod : a; } bool can[55][55]; int cnt; void init() { can[1][0] = 1; for (int i = 3; i <= 15; i += 2) for (int c1 = 1; c1 < (i - 1); c1 += 2) { int c2 = i - c1 - 1; int f1 = max(c1, c2) >= min(c1, c2) * 2; for (int p = 0; p <= c1; ++p) for (int q = 0; q <= c2; ++q) if (can[c1][p] && can[c2][q]) can[i][p + q + f1] = 1; } } bool ok(int i, int j) { if (i % 2 == 0) return 0; if (i <= 15) return can[i][j]; if (j == 0 || j == 1) return ((i & (i + 1)) == 0) ^ j; return j > 0 && j * 2 + 3 <= i; } void dfs(int n, int k, int f) { pr1(f); int x = ++cnt; if (n == 1) return; for (int l = 1; l < n - 1; l = l << 1 | 1) { int r = n - l - 1, kr = k - (max(l, r) >= 2 * min(l, r)); if (ok(l, 0) && ok(r, kr)) { dfs(l, 0, x); dfs(r, kr, x); return; } } } void solve() { int n, k; qr(n); qr(k); init(); if (!ok(n, k)) { puts("No"); return; } cnt = 0; puts("Yes"); dfs(n, k, 0); } int main() { solve(); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int chtholly = 0; const int N = 30; bool v[N][N]; int n; using pii = pair<int, int>; void construct(int n, int tar, int par, int val) { int j, k, l; bool pass = false; pii tar_1, tar_2; cout << par << " "; if (n == 1) { return; } else if (n <= N) { for (j = 1; j < n; j++) { if (j % 2 == 1) { for (k = 0; k < N; k++) for (l = 0; l < N; l++) if (v[j][k] && v[n - j - 1][l]) { if (j > 2 * (n - j - 1) || (n - j - 1) > 2 * j) { if (k + l + 1 == tar) { tar_1 = {j, k}; tar_2 = {n - j - 1, l}; pass = true; } } else if (k + l == tar) { tar_1 = {j, k}; tar_2 = {n - j - 1, l}; pass = true; } } } } construct(tar_1.first, tar_1.second, val, val + 1); construct(tar_2.first, tar_2.second, val, val + tar_1.first + 1); } else { if (tar > 2) { construct(n - 2, tar - 1, val, val + 1); construct(1, 0, val, n + val - 1); } else if (tar == 2) { int cur = 2; while (cur * 2 < n) cur *= 2; if (cur + 1 == n) { construct(n - 4, 1, val, val + 1); construct(3, 0, val, val + n - 3); } else { construct(n - 2, 1, val, val + 1); construct(1, 0, val, val + n - 1); } } else if (tar == 0) { construct(n / 2, 0, val, val + 1); construct(n / 2, 0, val, val + 1 + n / 2); } else { int cur = 2; while (cur * 2 < n) cur *= 2; if (cur + cur / 2 == n + 1) { construct(cur - 1, 0, val, val + 1); construct(n - cur, 0, val, val + cur); } else if (cur + cur / 2 < n) { construct(cur - 1, 0, val, val + 1); construct(n - cur, 1, val, val + cur); } else { cur /= 2; construct(cur - 1, 0, val, val + 1); construct(n - cur, tar, val, val + cur); } } } } int main() { int i, j, k, l; v[1][0] = true; v[3][0] = true; for (i = 4; i < N; i++) { if (i % 2 == 1) { for (j = 1; j < i; j++) { if (j % 2 == 1) { for (k = 0; k < N; k++) for (l = 0; l < N; l++) if (v[j][k] && v[i - j - 1][l]) { if (j > 2 * (i - j - 1) || (i - j - 1) > 2 * j) v[i][k + l + 1] = true; else v[i][k + l] = true; } } } } } cin >> n >> k; if (n % 2 == 0) cout << "NO\n"; else if (n >= 11) { int cur = n + 1; int powe = 1; while (cur % 2 == 0) { cur /= 2; powe *= 2; } if (k > (n - 3) / 2) cout << "NO\n"; else if (powe == n + 1 && k == 1) cout << "NO\n"; else if (powe != n + 1 && k == 0) cout << "NO\n"; else { cout << "YES\n"; construct(n, k, 0, 1); } } else { if (v[n][k]) { cout << "YES\n"; construct(n, k, 0, 1); } else cout << "NO\n"; } return chtholly; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; mt19937_64 rng( (unsigned)chrono::system_clock::now().time_since_epoch().count()); const long double PI = acosl(-1); const int nmax = 1e5 + 10; inline bool isTwo(int x) { return x == (x & -x); } int parent[nmax]; int nodecnt = 0; void build(int n, int imbalance, int par) { int cur = ++nodecnt; parent[cur] = par; if (n == 1) { ; } else if (n == 11 && imbalance == 3) { build(3, 0, cur); build(7, 2, cur); } else if (isTwo(n + 1) && imbalance == 0) { build(n / 2, 0, cur); build(n / 2, 0, cur); } else if (!isTwo(n + 1) && imbalance == 1) { int a = 1; while (a <= n) a *= 2; a /= 2; a--; int b = a / 2; if (n - 1 - b < a) { build(b, 0, cur); build(n - 1 - b, 1, cur); } else if (n - 1 - b == a) { build(b, 0, cur); build(a, 0, cur); } else { build(n - 1 - a, 1, cur); build(a, 0, cur); } } else if (isTwo(n - 1) && imbalance == 2) { build(3, 0, cur); build(n - 1 - 3, 1, cur); } else { build(1, 0, cur); build(n - 2, imbalance - 1, cur); } return; } int subtree[nmax][2]; void check(int n, int k) { for (int i = n; i >= 1; i--) { int p = parent[i]; int a = max(subtree[i][0], subtree[i][1]); int b = min(subtree[i][0], subtree[i][1]); if (b != 0 && a >= 2 * b) k--; int tmp = subtree[i][0] + subtree[i][1] + 1; if (subtree[p][0] == 0) subtree[p][0] += tmp; else subtree[p][1] += tmp; } assert(k == 0); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n, k; cin >> n >> k; if (n == 1) { if (k == 0) { cout << "YES\n"; cout << 0 << "\n"; } else { cout << "NO\n"; return 0; } return 0; } if (n % 2 == 0) { cout << "NO\n"; return 0; } if (k > (n - 3) / 2) { cout << "NO\n"; return 0; } if (isTwo(n + 1) && k == 1) { cout << "NO\n"; return 0; } if (!isTwo(n + 1) && k == 0) { cout << "NO\n"; return 0; } if (n == 9 && k == 2) { cout << "NO\n"; return 0; } build(n, k, 0); cout << "YES\n"; for (int i = 1; i <= n; i++) cout << parent[i] << " "; cout << endl; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k, w; void v(int a) { cout << a << " "; } void t(int a, int b) { for (int i = a + 1; i <= b; i++) v(a + (i - a - 1) / 2); } void solve(int db, int p, int f) { v(f); if (db < 2) t(p, n); else { if (db == 3 && n - p == 10) t(p, p + 6), t(p + 6, p + 8), t(p + 8, p + 10); else if (db == 2 && ((n - p) & (n - p - 1)) == 0) v(p), t(p + 1, p + 3), v(p), t(p + 4, n); else v(p), solve(db - 1, p + 2, p); } } int main() { cin >> n >> k, w = (n & (n + 1)); if (n % 2 == 0 || 2 * k > max(0, n - 3) || (w && !k) || (!w && k == 1) || n == 9 && k == 2) { cout << "NO\n"; return 0; } cout << "YES\n"; solve(k, 1, 0); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5, M = 50 + 5; const long long INF = 1e18 + 5; inline long long read() { long long sum = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fh = -1; c = getchar(); } while (c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); } return sum * fh; } inline int read2() { char c = getchar(); while (c < '0' || c > '9') c = getchar(); return c - '0'; } inline int read3() { char c = getchar(); while (c < 'a' || c > 'z') { c = getchar(); } return c - 'a'; } inline void write(long long x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } inline int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); } inline long long ab(long long x) { return x < 0 ? -x : x; } inline long long fpow(long long qwe, long long asd, long long zxc) { if (asd < 0) return 0; long long a = qwe, b = 1, c = asd; while (c) { if (c & 1) b = b * a % zxc; a = a * a % zxc; c >>= 1; } return b; } int f[N]; int isp[N], ssp[N]; vector<int> cs[15][15]; inline void csh() { cs[1][0] = vector<int>{0}; cs[3][0] = vector<int>{0, 0, 0}; cs[5][1] = vector<int>{0, 0, 0, 1, 1}; cs[7][0] = vector<int>{0, 0, 0, 1, 1, 2, 2}; cs[7][2] = vector<int>{0, 0, 0, 1, 1, 3, 3}; cs[9][1] = vector<int>{0, 0, 0, 1, 1, 3, 3, 4, 4}; cs[9][3] = vector<int>{0, 0, 0, 1, 1, 3, 3, 5, 5}; cs[11][1] = vector<int>{0, 0, 0, 1, 1, 3, 3, 4, 4, 2, 2}; cs[11][2] = vector<int>{0, 0, 0, 1, 1, 3, 3, 4, 4, 5, 5}; cs[11][3] = vector<int>{0, 0, 0, 1, 1, 3, 3, 5, 5, 2, 2}; cs[11][4] = vector<int>{0, 0, 0, 1, 1, 3, 3, 5, 5, 7, 7}; } int qw[N]; inline void dfs(int l, int r, int k, int fa) { qw[l] = k; f[l] = fa; int s = r - l + 1; if (s < 13) { for (int i = 1; i < s; ++i) { f[l + i] = l + cs[s][k][i]; } return; } else if (s == 13) { if (k == 1) { dfs(l + 1, l + 5, 1, l); dfs(l + 5 + 1, l + 5 + 7, 0, l); } else { dfs(l + 1, l + 1, 0, l); dfs(l + 1 + 1, l + 1 + 11, k - 1, l); } } else { if ((s >= 17 && s <= 21) && k == 1) { dfs(l + 1, l + 7, 0, l); dfs(l + 7 + 1, l + 7 + (s - 1 - 7), 1, l); } else if (isp[s] && k == 0) { int s2 = (s - 1) / 2; dfs(l + 1, l + s2, 0, l); dfs(l + s2 + 1, l + s2 + s2, 0, l); } else if (k == 1) { int s2 = s - 1, s3 = ssp[s2], s4 = s2 - s3; if (s4 * 2 < s3) { s3 /= 2; s4 = s2 - s3; } dfs(l + 1, l + s3, 0, l); dfs(l + s3 + 1, l + s3 + s4, isp[s4] ^ 1, l); } else { int s2 = s - 1; if (isp[s2 - 1] && k == 2) { dfs(l + 1, l + 3, 0, l); dfs(l + 3 + 1, l + 3 + (s2 - 3), k - 1, l); } else { dfs(l + 1, l + 1, 0, l); dfs(l + 1 + 1, l + 1 + (s2 - 1), k - 1, l); } } } } int main() { int n = read(), k = read(); csh(); for (int i = 16; i <= 500000; i *= 2) isp[i - 1] = 1; for (int i = 16, j = 15; i < 500000; ++i) { if (isp[i]) j = i; else ssp[i] = j; } if (n % 2 == 0) { puts("NO"); } else if (n < 13 && cs[n][k].size() == 0) { puts("NO"); } else if (n >= 13 && isp[n] == 0 && k == 0) { puts("NO"); } else if (n >= 13 && isp[n] && k == 1) { puts("NO"); } else if (n >= 13 && k > (n - 3) / 2) { puts("NO"); } else { puts("YES"); dfs(1, n, k, 0); for (int i = 1; i <= n; ++i) { write(f[i]), putchar(' '); } } }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k; int s[100005], si[100005]; int main() { scanf("%d%d", &n, &k); if (n % 2 == 0) { puts("NO"); return 0; } if (n == 1 && k == 0) { puts("YES"); puts("0"); return 0; } int tmp = (n - 3) / 2; if (tmp < k) { puts("NO"); return 0; } if (n == 7 && k == 1) { puts("NO"); return 0; } if (n == 9 && k == 2) { puts("NO"); return 0; } if (k == 0) { int len = 1; while (len <= n) len = len * 2; if (len != n + 1) { puts("NO"); return 0; } for (int i = 1; i <= n; i++) s[i] = i / 2; puts("YES"); for (int i = 1; i < n; i++) printf("%d ", s[i]); printf("%d\n", s[n]); return 0; } if (k == 1) { int len = 1; while (len <= n) len = len * 2; if (len == n + 1) { puts("NO"); return 0; } } int ti = n - 2 * k + 2, len = 1; while (len < ti) len = len * 2; if (len != ti + 1) { for (int i = 1; i <= ti; i++) s[i] = i / 2; int la = 1, tot = ti; for (int i = 1; i <= k - 1; i++) { tot += 2; s[tot - 1] = tot; s[la] = tot; la = tot; } } else { ti -= 2; for (int i = 1; i <= ti; i++) s[i] = i / 2; int la = 1, tot = ti; for (int i = 1; i <= k; i++) { tot += 2; s[tot - 1] = tot; s[la] = tot; la = tot; } s[tot - 2] = 0; s[tot - 1] = s[tot] = tot - 3; } puts("YES"); for (int i = 1; i < n; i++) printf("%d ", s[i]); printf("%d\n", s[n]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 100005; bool mark[N]; int full_binary[N]; bool small[10][4]; int ans[N], cnt; bool valid(int n, int k) { if (k < 0) return false; if (n % 2 == 0) return false; if (n == 1 && k == 0) return true; if (k > (n - 3) / 2) { return false; } if (n <= 9) return small[n][k]; if (mark[n] && k == 1) return false; if (!mark[n] && k == 0) return false; return true; } void build(int n, int k, int p) { if (n == 0) exit(0); int nw = ++cnt; ans[nw] = p; if (n == 1) return; if (k == 0) { build((n - 1) / 2, 0, nw); build((n - 1) / 2, 0, nw); return; } if (k == 1) { for (int i = 1; (1 << i) - 1 < n; ++i) { int a = (1 << i) - 1; int b = n - 1 - a; int add = 0; if (2 * min(a, b) <= max(a, b)) add = 1; if (valid(b, k - add) == false) continue; build(a, 0, nw); build(b, k - add, nw); break; } return; } for (int i = 1; i <= min(9, (n - 1) / 2); i += 2) { int add = (2 * i <= n - i - 1); for (int j = 0; j <= max(0, (n - 3) / 2); ++j) { if (valid(i, j) && valid(n - i - 1, k - j - add)) { build(i, j, nw); build(n - i - 1, k - j - add, nw); return; } } } } void solve() { for (int i = 1; i <= 100000; i <<= 1) mark[i - 1] = 1; int n, k; scanf("%d %d", &n, &k); small[1][0] = small[3][0] = true; small[5][1] = small[7][0] = small[7][2] = true; small[9][3] = small[9][1] = true; if (valid(n, k) == false) { printf("NO"); return; } puts("YES"); build(n, k, 0); assert(cnt == n); for (int i = (1); i <= (cnt); ++i) printf("%d ", ans[i]); } int main() { solve(); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int lowbit(int x) { return x & -x; } int n, k, fa[100010]; int main() { scanf("%d%d", &n, &k); if (!(n & 1) || k > max(0, (n - 3) / 2) || (n == 9 && k == 2)) puts("NO"), exit(0); if (n + 1 == lowbit(n + 1)) { if (k == 1) puts("NO"), exit(0); } else if (k == 0) puts("NO"), exit(0); puts("YES"); int cln = max(0, k - 1); for (int i = 1; i <= cln; i++) { fa[i * 2] = i * 2 - 1; fa[i * 2 - 1] = max(0, i * 2 - 3); } for (int i = 2 * cln + 1; i <= n; i++) { if (i == 2 * cln + 1) fa[i] = max(2 * cln - 1, 0); else fa[i] = ((i - 2 * cln) >> 1) + 2 * cln; } if (n - 2 * cln + 1 == lowbit(n - 2 * cln + 1) && k) fa[n - 1] = fa[n] = 2; for (int i = 1; i <= n; i++) printf("%d ", fa[i]); puts(""); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; template <class T> int chkmax(T& a, T b) { if (b > a) { a = b; return 1; } return 0; } template <class T> int chkmin(T& a, T b) { if (b < a) { a = b; return 1; } return 0; } template <class iterator> void output(iterator begin, iterator end, ostream& out = cerr) { while (begin != end) { out << (*begin) << " "; begin++; } out << endl; } template <class T> void output(T x, ostream& out = cerr) { output(x.begin(), x.end(), out); } void fast_io() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int get_minimal(int t) { return (__builtin_popcount(t + 1) > 1); } vector<int> ans = {-1}; void build_minimal(int t, int s) { queue<int> q; q.push(s); for (int it = 0; it < t; ++it) { int v = q.front(); q.pop(); q.push(ans.size()); ans.push_back(v); q.push(ans.size()); ans.push_back(v); } } const int T = 200; int dp[T][T + 1]; void calc_dp() { dp[0][0] = 1; for (int i = 1; i < T; ++i) { for (int s1 = 0; s1 <= i - 1; ++s1) { int s2 = i - 1 - s1, w1 = 2 * s1 + 1, w2 = 2 * s2 + 1; int add = (w2 >= 2 * w1 || w1 >= 2 * w2); for (int j1 = 0; j1 <= s1; ++j1) { for (int j2 = 0; j2 <= s2; ++j2) { if (dp[s1][j1] && dp[s2][j2]) { dp[i][j1 + j2 + add] = 1; } } } } } } int can(int t, int k) { if (t < T) { return dp[t][k]; } if (k < get_minimal(t)) { return 0; } if (k == 1 && t >= 3 && get_minimal(t) == 0) { return 0; } if (k >= t) { return 0; } return 1; } void build(int v, int t, int k) { if (t == 0) { assert(k == 0); return; } cerr << "build " << v << " " << t << " " << k << endl; if (t < T) { int v1 = ans.size(); ans.push_back(v); int v2 = ans.size(); ans.push_back(v); for (int s1 = 0; s1 <= t - 1; ++s1) { int s2 = t - 1 - s1, w1 = 2 * s1 + 1, w2 = 2 * s2 + 1; int add = (w2 >= 2 * w1 || w1 >= 2 * w2); for (int j1 = 0; j1 <= s1; ++j1) { for (int j2 = 0; j2 <= s2; ++j2) { if (dp[s1][j1] && dp[s2][j2] && j1 + j2 + add == k) { build(v1, s1, j1); build(v2, s2, j2); return; } } } } } if (k == get_minimal(t)) { build_minimal(t, v); return; } int v1 = ans.size(); ans.push_back(v); int v2 = ans.size(); ans.push_back(v); if (k > 2) { build(v2, t - 1, k - 1); return; } for (int s1 = 0; s1 <= t - 1; ++s1) { int s2 = t - 1 - s1, w1 = 2 * s1 + 1, w2 = 2 * s2 + 1; int add = (w2 >= 2 * w1 || w1 >= 2 * w2); for (int j1 = 0; j1 <= min(s1, k); ++j1) { for (int j2 = 0; j2 <= min(s2, k); ++j2) { if (j1 + j2 + add == k && can(s1, j1) && can(s2, j2)) { build(v1, s1, j1); build(v2, s2, j2); return; } } } } assert(0); } signed main() { fast_io(); calc_dp(); int n, k; cin >> n >> k; if (n % 2 == 0) { cout << "NO\n"; exit(0); } int t = (n - 1) / 2; if (can(t, k) == 0) { cout << "NO\n"; exit(0); } build(0, t, k); cout << "YES\n"; for (int i = 0; i < n; ++i) { cout << ans[i] + 1 << ' '; } cout << '\n'; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 100003; template <typename T> void read(T &x) { int ch = getchar(); x = 0; bool f = false; for (; ch < '0' || ch > '9'; ch = getchar()) f |= ch == '-'; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; if (f) x = -x; } int n, k, fa[N]; int EI(int x) { return x & -x; } bool chk(int x) { return x == EI(x); } int main() { read(n); read(k); if (!k) { if (n + 1 != EI(n + 1)) return puts("NO"), 0; puts("YES"); for (int i = 1; i <= n; ++i) printf("%d%c", i >> 1, " \n"[i == n]); return 0; } if (!(n & 1) || k > (n - 3 >> 1) || (n == 9 && k == 2) || (k == 1 && chk(n + 1))) return puts("NO"), 0; puts("YES"); for (int i = 1; i < k; ++i) fa[i << 1] = fa[i << 1 | 1] = i * 2 - 1; for (int i = k * 2; i <= n; ++i) fa[i] = (i >> 1) + k - 1; if (chk(n - k * 2 + 3)) fa[n - 1] = fa[n] = 2; for (int i = 1; i <= n; ++i) printf("%d%c", fa[i], " \n"[i == n]); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) res += ", "; first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } template <class T> inline bool chmax(T &a, T b) { return a < b && (a = b, true); } template <class T> inline bool chmin(T &a, T b) { return a > b && (a = b, true); } using ll = long long; using P = pair<int, int>; using T = tuple<int, int, int>; constexpr int INF = 0x3f3f3f3f; constexpr ll LLINF = 0x3f3f3f3f3f3f3f3f; constexpr ll MOD = 1e9 + 7; int n, k; vector<int> s; bool valid() { if (n == 1 && k == 0) return true; if (n % 2 == 0 || k > n / 2 - 1) { return false; } if (n == 9 && k == 2) { return false; } if (((n + 1) & n) == 0) { return k != 1; } else { return k != 0; } } void solve(int start, int n, int k, int fa) { s[start] = fa; fa = start + 1; if (n == 1) return; if (((n + 1) & n) == 0) { if (k == 0) { solve(start + 1, n / 2, 0, fa); solve(start + 1 + n / 2, n / 2, 0, fa); return; } } else if (((n - 1) & (n - 2)) == 0) { if (k == 2) { solve(start + 1, 3, 0, fa); solve(start + 4, n - 4, 1, fa); return; } } else if (k == 1) { int cnt = 0, x = 1; while (x < n) { x *= 2; cnt++; } int a = (1 << (cnt - 1)) - 1; int b = (1 << (cnt - 2)) - 1; if (n == a + b + 1) { solve(start + 1, a, 0, fa); solve(start + 1 + a, b, 0, fa); } else if (n > a + b + 1) { solve(start + 1, a, 0, fa); solve(start + 1 + a, n - 1 - a, 1, fa); } else { solve(start + 1, b, 0, fa); solve(start + 1 + b, n - 1 - b, 1, fa); } return; } if (n == 11 && k == 3) { solve(start + 1, 3, 0, fa); solve(start + 4, 7, 2, fa); return; } solve(start + 1, 1, 0, fa); solve(start + 2, n - 2, k - 1, fa); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; if (!valid()) { cout << "NO\n"; return 0; } s.resize(n); solve(0, n, k, 0); cout << "YES\n"; for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) { cout << s[i] << (i == n - 1 ? '\n' : ' '); } }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; const long long INF = 0x3f3f3f3f3f3f3f3f; __inline__ __attribute__((always_inline)) unsigned int rnd() { static unsigned int seed = 416; return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13; } template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) { return (!b) ? a : gcd(b, a % b); } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) { return a >= 0 ? a : -a; } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b) { return a < b ? b : a; } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b) { return a < b ? a : b; } template <typename _Tp> __inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) { (a < b) && (a = b); } template <typename _Tp> __inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) { (b < a) && (a = b); } template <typename _Tp> __inline__ __attribute__((always_inline)) void read(_Tp &x) { char ch(getchar()); bool f(false); while (ch < 48 || ch > 57) f |= ch == 45, ch = getchar(); x = ch & 15, ch = getchar(); while (ch >= 48 && ch <= 57) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); if (f) x = -x; } template <typename _Tp, typename... Args> __inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) { read(t); read(args...); } __inline__ __attribute__((always_inline)) int read_str(char *s) { char ch(getchar()); while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar(); char *tar = s; *tar = ch, ch = getchar(); while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF) *(++tar) = ch, ch = getchar(); return tar - s + 1; } const int N = 100005; bool check(int n, int k) { if (!(n & 1)) return false; if (n == 1) return k == 0; if (k > ((n - 3) >> 1)) return false; if (k == 0) return ((n + 1) & (-(n + 1))) == n + 1; if (k == 1) return ((n + 1) & (-(n + 1))) != n + 1; if (k == 2) return n != 9; return true; } int node_cnt, fa[N]; int solve(int n, int k) { int cur = ++node_cnt; if (n == 1) return cur; if (k == 0) { fa[solve((n - 1) >> 1, k)] = cur; fa[solve((n - 1) >> 1, k)] = cur; return cur; } if (k == 1) { if (__builtin_popcount(n + 1) == 2) { fa[solve(((n + 1) & (-(n + 1))) - 1, 0)] = cur; fa[solve(((n + 1) ^ ((n + 1) & (-(n + 1)))) - 1, 0)] = cur; } else { int qwq = n + 1; for (int i = 17; i >= 0; --i) { if ((qwq >> i) & 1) { if ((qwq >> (i - 1)) & 1) { fa[solve((1 << i) - 1, 0)] = cur; fa[solve((qwq ^ (1 << i)) - 1, 1)] = cur; } else { fa[solve((1 << (i - 1)) - 1, 0)] = cur; fa[solve(n - (1 << (i - 1)), 1)] = cur; } break; } } } return cur; } if (n == 11 && k == 3) { fa[solve(3, 0)] = cur; fa[solve(7, 2)] = cur; } else { if (k == 2 && ((n - 1) & (-(n - 1))) == n - 1) { fa[solve(3, 0)] = cur; fa[solve(n - 4, 1)] = cur; } else { fa[solve(1, 0)] = cur; fa[solve(n - 2, k - 1)] = cur; } } return cur; } int main() { int n, k; read(n, k); if (check(n, k)) { printf("YES\n"); solve(n, k); for (int i = 1; i <= n; ++i) printf("%d ", fa[i]); printf("\n"); } else { printf("NO\n"); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; inline int read() { char c = getchar(); int x = 0; bool f = 0; for (; !isdigit(c); c = getchar()) if (c == '-') f = 1; for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); if (f) x = -x; return x; } int n, k, fa[100005]; bool ispw[200005]; bool chk(int n, int k) { if (n == 1 && k == 0) return 1; if (n % 2 == 0 || k > n / 2 - 1) return 0; if (n == 9 && k == 2) return 0; if (ispw[n + 1]) return k != 1; else return k != 0; } void build(int l, int r, int k) { if (l == r) return; int len = r - l + 1; for (int i = 1; i <= len; i += 2) { int j = len - 1 - i, nk = k; if (i * 2 <= j || j * 2 <= i) nk--; if (nk < 0) continue; for (register int p = (0); p <= (min(nk, max(0, i / 2 - 1))); ++p) if (chk(i, p) && chk(j, nk - p)) { fa[l + 1] = l, fa[l + i + 1] = l; build(l + 1, l + i, p); build(l + i + 1, r, nk - p); return; } } } int main() { int x = 1; while (x <= 100000) { ispw[x] = 1; x *= 2; } n = read(), k = read(); if (!chk(n, k)) puts("NO"); else { puts("YES"); build(1, n, k); for (register int i = (1); i <= (n); ++i) cout << fa[i] << ' '; } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; int ans[maxn]; int lowbit(int x) { return x & -x; } bool is_two(int x) { return x == lowbit(x); } int main() { int n, k; scanf("%d%d", &n, &k); int lim = max(0, (n - 3) / 2); if (n % 2 == 0 || k > lim || (n == 9 && k == 2) || (!is_two(n + 1) && k == 0) || (is_two(n + 1) && k == 1)) { printf("NO\n"); return 0; } int id = 1; while (k >= 2) { ans[id] = max(id - 2, 0); ans[id + 1] = id; n -= 2; k -= 1; id += 2; } ans[id] = max(id - 2, 0); for (int i = 2; i <= n; ++i) { ans[id + i - 1] = id + i / 2 - 1; } if (is_two(n + 1) && k == 1) ans[id + n - 2] = ans[id + n - 1] = 2; printf("YES\n"); for (int i = 1; i < id + n - 1; ++i) { printf("%d ", ans[i]); } printf("%d\n", ans[id + n - 1]); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, m, k; bool works(int n, int k) { if (!(n & 1) || k < 0) return 0; if (k < 2) return k ^ !(n & (n + 1)); if (n == 9 && k == 2) return 0; return k && 2 * k + 3 <= n; } void sol(int n, int k, int p) { int x = ++m; cout << p << (x > ::n ? '\n' : ' '); for (int i = 1; i < n; i = 2 * i + 1) { int j = n - i - 1, l = k - (max(i, j) >= 2 * min(i, j)); if (works(i, 0) && works(j, l)) { sol(i, 0, x), sol(j, l, x); return; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; if (!works(n, k)) { cout << "NO" << '\n'; } else { cout << "YES" << '\n'; sol(n, k, 0); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
from heapq import * import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def ng(): print("NO") exit() n, k = MI() if (n,k)==(1,0): print("YES") print(0) exit() ans = [0] * n popcnt = lambda x: bin(x).count("1") if n & 1 == 0 or n < 2 * k + 3 or (popcnt(n + 1) > 1 and k == 0): ng() u = 0 if popcnt(n + 1 - 2 * (k - 1)) == 1: for v in range(1, 4): ans[v] = v // 2+1 k -= 1 if n - 4 < 2 * k + 3 or k==0 or n<11: ng() ans[4] = 1 u = 4 #print(n, k, u, ans) for _ in range(k - 1): ans[u + 1] = ans[u + 2] = u+1 u += 2 #print(n, k, u, ans) for v in range(1,n-u):ans[v+u]=(v-1)//2+u+1 print("YES") print(*ans)
PYTHON3
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k, las; int main() { scanf("%d%d", &n, &k); if (n % 2 == 0 || (k > (n - 3) / 2 && n != 1) || (n == 9 && k == 2) || (((int)(pow(2, (int)log2(n + 1)) + 1e-8)) != n + 1 && k == 0) || (((int)(pow(2, (int)log2(n + 1)) + 1e-8)) == n + 1 && k == 1) || n <= 3 && k > 0) { printf("NO"); return 0; } printf("YES\n"); if (k > 1) { printf("0 "); las = 1; } for (int i = 2; i <= 2 * (k - 1); i++) { printf("%d ", las); if (i & 1) las = i; } if (((int)(pow(2, (int)log2(n - max(0, 2 * (k - 1))) + 1) + 1e-8)) != n - max(0, 2 * (k - 1)) + 1) { printf("%d ", las); las = max(0, 2 * (k - 1)); for (int i = max(2 * k, 2); i <= n; i++) printf("%d ", (i - las >> 1) + las); } else { if (k) { printf("%d ", las); las = max(0, 2 * (k - 1)); for (int i = max(2 * k, 2); i <= n - 2; i++) printf("%d ", (i - las >> 1) + las); if (n >= 2) printf("2 2"); } else { printf("%d ", las); las = max(0, 2 * (k - 1)); for (int i = max(2 * k, 2); i <= n; i++) printf("%d ", (i - las >> 1) + las); } } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k, cnt; bool check(int cntNode, int cntCritical) { if (cntNode % 2 == 0) return 0; if (!cntCritical) return !(cntNode & (cntNode + 1)); if (cntCritical == 1) return cntNode & (cntNode + 1); if (cntNode == 9 && cntCritical == 2) return 0; return cntCritical > 0 && cntCritical <= (cntNode - 3) / 2; } void dfs(int cntNode, int cntCritical, int cur) { cout << cur << " "; int fa = ++cnt; if (cntNode == 1) return; for (int lsize = 1; lsize < cntNode; lsize = lsize * 2 + 1) { int rsize = cntNode - lsize - 1; int rem = cntCritical - (max(lsize, rsize) >= (min(lsize, rsize) * 2)); if (check(lsize, 0) && check(rsize, rem)) { dfs(lsize, 0, fa); dfs(rsize, rem, fa); return; } } } int main() { cin >> n >> k; if (!check(n, k)) cout << "NO" << endl, exit(0); cout << "YES" << endl; dfs(n, k, 0); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; int now = 0; int p[100005]; int buildbinary(int dep) { now++; if (dep == 1) return now; int node = now; p[buildbinary(dep - 1)] = node; p[buildbinary(dep - 1)] = node; return node; } bool dfs(int first, int need, int &val) { if (need < 0) return false; if (__builtin_popcount(first + 1) == 1 && need == 1) return false; if (__builtin_popcount(first + 1) == 1 && need == 0) { val = buildbinary(__builtin_popcount(first)); return true; } first--; for (int i = 1; (1 << i) - 1 < first; i++) { int l = (1 << i) - 1; int r = first - l; if (l * 2 < r || r * 2 < l) { int a; int last = now; if (dfs(r, need - 1, a)) { int b = buildbinary(i); now++; p[a] = now; p[b] = now; val = now; return true; } now = last; } else { int a; int last = now; if (dfs(r, need, a)) { int b = buildbinary(i); now++; p[a] = now; p[b] = now; val = now; return true; } now = last; } } return false; } void solve() { int n, need; scanf("%d %d", &n, &need); if (n % 2 == 0) { printf("NO\n"); } else { if (need == 0) { if (__builtin_popcount(n + 1) == 1) { printf("YES\n"); for (int i = 1; i <= n; i++) { printf("%d ", i / 2); } printf("\n"); } else { printf("NO\n"); } return; } else if ((n - 3) / 2 < need) { printf("NO\n"); } else { int root; if (dfs(n, need, root)) { p[root] = 0; printf("YES\n"); for (int i = 1; i <= n; i++) { printf("%d ", p[i]); } printf("\n"); } else { printf("NO\n"); } } } } int main() { int t = 1; while (t--) { solve(); } }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #pragma GCC target("avx2") using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "RDLU"; const long long MOD2 = (long long)1000000007 * (long long)1000000007; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 1000000007) { x += y; if (x >= mod) x -= mod; if (x < 0) x += mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 1000000007) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } int cnt; bool ck(int n, int k) { if (n % 2 == 0) return 0; if (k == 0) return (n & (n + 1)) == 0; if (k == 1) return (n & (n + 1)) != 0; if (n == 9 && k == 2) return 0; return k > 0 && k <= (n - 3) / 2; } inline void cal(int n, int k, int fa) { printf("%d ", fa); int x = ++cnt; if (n == 1) return; for (int l = 1; l < n; l = l << 1 | 1) { int r = n - l - 1; int z = max(l, r) >= min(l, r) * 2; int rk = k - z; if (ck(l, 0) && ck(r, rk)) { cal(l, 0, x); cal(r, rk, x); return; } } } void fmain(int tid) { scanf("%d%d", &n, &k); if (!ck(n, k)) etp(); puts("YES"); cal(n, k, 0); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; bool f[100005]; void out() { cout << "NO"; exit(0); } int fa[100005]; signed main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; f[1] = 1; for (int i = 2; i <= n + 1; i++) { if (!(i & 1) && f[i / 2]) f[i] = 1; } int maxi = (n - 3) / 2; maxi = max(maxi, 0); if (!(n & 1) || n == 9 && k == 2 || k > maxi || f[n + 1] && k == 1 || !f[n + 1] && k == 0) out(); if (k == 0) { cout << "YES\n"; for (int i = 1; i <= n; i++) { cout << i / 2 << ' '; } return 0; } int t = 2 * (k - 1); for (int i = 1; i < t; i += 2) { fa[i + 1] = i; fa[i] = max(i - 2, 0); } fa[t + 1] = max(0, t - 1); for (int i = 2; i <= n - t; i++) { fa[i + t] = i / 2 + t; } if (f[n - t + 1]) { fa[n - 1] = fa[n] = 2; } cout << "YES\n"; for (int i = 1; i <= n; i++) cout << fa[i] << ' '; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; int fa[maxn]; int chk(int x) { return x - (x & -x); } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n, k; cin >> n >> k; int lim = max((n - 3) / 2, 0); if ((k > lim) || (n % 2 == 0) || (n == 9 && k == 2) || (chk(n + 1) == 0 && k == 1) || (chk(n + 1) != 0 && k == 0)) cout << "NO\n", exit(0); cout << "YES\n"; int num = 2 * max(0, k - 1); for (int i = 1; i < num; i += 2) fa[i + 1] = i, fa[i] = max(0, i - 2); for (int i = 1; i <= n - num; ++i) { if (i == 1) fa[i + num] = max(0, num - 1); else fa[i + num] = (i >> 1) + num; } if (chk(n - num + 1) == 0 && k) fa[n - 1] = fa[n] = 2; for (int i = 1; i <= n; ++i) cout << fa[i] << ' '; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int s[maxn] = {}; void create2(int i, int n) { s[i + 1] = i; s[i + 2] = i; if (n - 2 > 0) create2(i + 2, n - 2); } bool create(int i, int n, int k) { if (n < 2 * k + 2) { return false; } if (n == 0) return true; if (n == 2 * k + 2) { create2(i, n); return true; } if (k >= 2) { if (n > 11) { while (n > 11 && k >= 2 && (k > 2 || ((n) & (n - 1)) != 0)) { s[i + 1] = i; s[i + 2] = i; i = i + 2; k--; n -= 2; } if (k == 2 && n > 11) { s[i + 1] = s[i + 4] = i; s[i + 2] = s[i + 3] = i + 1; return create(i + 4, n - 4, 1); } return create(i, n, k); } } if (n == 2 * k + 4) { if (n < 10) return false; s[i + 1] = s[i + 4] = i; s[i + 2] = s[i + 3] = i + 1; create2(i + 4, n - 4); return true; } int nn = n; nn -= 2; int nl = nn / 4 * 2, nr = nn - nl; if (k == 1) { nn += 2; int p = 1, q = 1; while (true) { p *= 2; nl = q - 2; if (p + q - 2 == nn) k = 0; if (p + q - 2 >= nn) break; q *= 2; nl = p - 2; if (p + q - 2 == nn) return false; if ((p + q - 2) > nn) break; } nr = nn - 2 - nl; } int kl = max(min((nl - 2) / 2, k / 2), 0), kr = k - kl; s[i + 1] = i; s[i + nl + 2] = i; return create(i + 1, nl, kl) && create(i + nl + 2, nr, kr); } int main() { int n, k; cin >> n >> k; if (n == 1 && k == 0) { printf("YES\n0"); return 0; } if (((n + 1) & (n)) != 0 && k == 0) { cout << "NO"; return 0; } if (((n + 1) & (n)) == 0 && k == 1) { cout << "NO"; return 0; } if (n % 2 == 0 || k > (n - 3) / 2) { cout << "NO"; return 0; } if (!create(1, n - 1, k)) { cout << "NO" << endl; return 0; } cout << "YES" << endl; for (int i = 1; i <= n; ++i) { printf("%d ", s[i]); } }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; inline int read() { int first = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) { if (ch == '-') f = -1; } for (; isdigit(ch); ch = getchar()) { first = first * 10 + ch - 48; } return first * f; } const int mxN = 1e6; int fa[mxN + 3]; int mn[mxN + 3]; int qwq[mxN + 3]; vector<int> son[mxN + 3]; int sz[mxN + 3]; int n, m, cnt, ans; int getmax(int n) { return n == 1 ? 0 : (n - 1) / 2 - 1; } int getmin(int n) { return mn[n]; } int build(int n) { int u = ++cnt; if (n == 1) { return u; } if (mn[n] == 0) { int ls = build(n / 2); fa[ls] = u; int rs = build(n / 2); fa[rs] = u; } else if (n <= qwq[n] + qwq[n] / 2 + 1) { int ls = build(qwq[n] / 2); fa[ls] = u; int rs = build(n - qwq[n] / 2 - 1); fa[rs] = u; } else { int ls = build(qwq[n]); fa[ls] = u; int rs = build(n - qwq[n] - 1); fa[rs] = u; } return u; } int solve(int n, int m) { int u = ++cnt; if (n == 11 && m == 3) { int v1 = build(3); fa[cnt + 6] = fa[cnt + 7] = cnt + 5, fa[cnt + 4] = fa[cnt + 5] = cnt + 3, fa[cnt + 2] = fa[cnt + 3] = cnt + 1, fa[cnt + 1] = fa[v1] = u; cnt += 7; return u; } else if (n > 9 && qwq[n] == n - 2 && m == 2) { int ls = build(3); fa[ls] = u; int rs = build(n - 4); fa[rs] = u; return u; } else if (m <= 1) { if (m != getmin(n)) { return -1; } cnt--; build(n); return u; } else { int ls = ++cnt; fa[ls] = u; int rs = solve(n - 2, m - 1); fa[rs] = u; return u; } } void check(int u) { assert(son[u].size() == 0 || son[u].size() == 2); sz[u] = 1; if (son[u].size() == 2) { check(son[u][0]), check(son[u][1]); if (sz[son[u][0]] >= sz[son[u][1]] * 2 || sz[son[u][1]] >= sz[son[u][0]] * 2) { ans++; } sz[u] += sz[son[u][0]] + sz[son[u][1]]; } } int main() { for (int i = 1; i <= mxN; i++) mn[i] = 1; for (int i = 1; i <= mxN; i = i * 2 + 1) mn[i] = 0; qwq[1] = 1; for (int i = 2; i <= mxN; i++) qwq[i] = mn[i] == 0 ? i : qwq[i - 1]; n = read(), m = read(); if (n % 2 == 0) { puts("NO"); return 0; } if (m > getmax(n) || m < getmin(n)) { puts("NO"); return 0; } if (n <= 9) { if (m == getmin(n)) { build(n); } else if (n == 7 && m == 2) { printf("YES\n0 1 1 3 3 5 5\n"); return 0; } else if (n == 9 && m == 3) { printf("YES\n0 1 1 3 3 5 5 7 7\n"); return 0; } else { puts("NO"); return 0; } } else { if (solve(n, m) == -1) { puts("NO"); return 0; } } assert(cnt == n); puts("YES"); for (int i = 1; i <= cnt; i++) printf("%d ", fa[i]); puts(""); for (int i = 2; i <= cnt; i++) son[fa[i]].push_back(i); check(1); assert(ans == m); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> const int MAXN = 1e5 + 5, MAXLOG = 50; template <typename _T> void read(_T &x) { x = 0; char s = getchar(); int f = 1; while (s > '9' || s < '0') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = (x << 3) + (x << 1) + (s - '0'), s = getchar(); } x *= f; } template <typename _T> void write(_T x) { if (x < 0) { putchar('-'); x = (~x) + 1; } if (9 < x) { write(x / 10); } putchar(x % 10 + '0'); } template <typename _T> void swapp(_T &x, _T &y) { _T t = x; x = y, y = t; } template <typename _T> _T MIN(const _T a, const _T b) { return a < b ? a : b; } template <typename _T> _T MAX(const _T a, const _T b) { return a > b ? a : b; } int ance[MAXN]; int N, M, K; int Lowbit(const int x) { return x & (-x); } bool Chk(int x) { return x - Lowbit(x) == 0; } int main() { read(N), read(K); int lim = MAX((N - 3) / 2, 0); if (K > lim) return puts("NO"), 0; if (N % 2 == 0) return puts("NO"), 0; if (N == 9 && K == 2) return puts("NO"), 0; if (Chk(N + 1) && K == 1) return puts("NO"), 0; if (!Chk(N + 1) && K == 0) return puts("NO"), 0; puts("YES"); int base = 2 * MAX(0, K - 1); for (int i = 1; i < base; i += 2) ance[i + 1] = i, ance[i] = MAX(0, i - 2); for (int i = 1; i <= N - base; i++) { if (i == 1) ance[i + base] = MAX(0, base - 1); else ance[i + base] = (i >> 1) + base; } if (Chk(N - base + 1) && K) ance[N - 1] = ance[N] = 2; for (int i = 1; i <= N; i++) write(ance[i]), putchar(i == N ? '\n' : ' '); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k, i, fa[100002]; bool check(int x) { while (x != 1) { if (x % 2 != 0) return 0; x /= 2; } return 1; } int main() { scanf("%d%d", &n, &k); if ((n == 9 && k == 2) || n % 2 == 0) puts("NO"); else if (k == 0) { if (!check(n + 1)) puts("NO"); else { puts("YES"); for (i = 1; i <= n; i++) printf("%d ", i / 2); puts(""); } } else if (k == 1) { if (check(n + 1)) puts("NO"); else { puts("YES"); for (i = 1; i <= n; i++) printf("%d ", i / 2); puts(""); } } else { int n1 = n - 2 * (k - 1); if (n1 <= 3) puts("NO"); else if (!check(n1 + 1)) { fa[1] = n1 + k - 1; for (i = 2; i <= n1; i++) fa[i] = i / 2; for (i = n1 + 2; i <= n1 + k - 1; i++) fa[i] = i - 1; for (i = n1 + k; i <= n; i++) fa[i] = i - k + 1; puts("YES"); for (i = 1; i <= n; i++) printf("%d ", fa[i]); puts(""); } else { for (i = 2; i <= n1 - 2; i++) fa[i] = i / 2; fa[1] = n1 + k - 1; for (i = n1 + 2; i <= n1 + k - 1; i++) fa[i] = i - 1; for (i = n1 + k; i <= n; i++) fa[i] = i - k + 1; fa[n1 - 1] = fa[n1] = n1 + k; if (n - 4 < 6 && 2 * (n - 4) > 3) puts("NO"); else { puts("YES"); for (i = 1; i <= n; i++) printf("%d ", fa[i]); puts(""); } } } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, k, f[N], b; void no() { puts("NO"); exit(0); } int ok(int x) { return x == (x & (-x)); } int main() { scanf("%d%d", &n, &k); if (n % 2 == 0) no(); if (k > max((n - 3) / 2, 0)) no(); if (n == 9 && k == 2) no(); if (ok(n + 1) && k == 1) no(); if (!ok(n + 1) && k == 0) no(); puts("Yes"); b = 2 * max(0, k - 1); for (int i = 1; i < b; i += 2) f[i + 1] = i, f[i] = max(0, i - 2); for (int i = 1; i <= n - b; i++) { if (i == 1) f[i + b] = max(0, b - 1); else f[i + b] = (i >> 1) + b; } if (ok(n - b + 1) && k) f[n - 1] = f[n] = 2; for (int i = 1; i <= n; i++) printf("%d ", f[i]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> inline int Bitcount(int x) { int ret = 0; while (x) ret += x & 1, x >>= 1; return ret; } int n, k; int main() { scanf("%d %d", &n, &k); if (n == 1 && k == 0) { printf("YES\n0\n"); return 0; } if (!(n & 1) || n - 2 * k < 3) { puts("NO"); return 0; } if (n == 9 && k == 2) { puts("NO"); return 0; } if (k == 0 && Bitcount(n + 1) != 1) { puts("NO"); return 0; } if (k == 1 && Bitcount(n + 1) == 1) { puts("NO"); return 0; } puts("YES"); if (k == 0 || k == 1) { for (int i = 1; i < n; i++) printf("%d ", i >> 1); printf("%d\n", n >> 1); return 0; } int lim = n - 2 * (k - 1); printf("%d", lim + 2); for (int i = 2; i <= lim - 2; i++) printf(" %d", i >> 1); if (Bitcount(lim + 1) == 1) printf(" %d %d", n - 1, n - 1); else printf(" %d %d", (lim - 1) >> 1, lim >> 1); for (int i = lim + 1; i < n - 1; i += 2) printf(" %d %d", i + 1, i + 3); printf(" %d 0\n", n); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 2e5; const int oo = 1e9; int ans[N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; if (n == 1 && k == 0) { cout << "yes 0\n"; return 0; } int nini = n; if (n % 2 == 0 || k >= n / 2) cout << "no\n"; else { int cur = 2, lst = 1, enter = 0; n--; for (int i = 0; i < k - 1; ++i) { ans[cur] = lst; ans[cur + 1] = lst; lst = cur, cur += 2; n -= 2; } queue<int> q; int pot = 1, curP = 0; q.push(lst); while (1) { lst = q.front(); q.pop(); if (curP + 1 == pot && n == 2) { if (k == 1 || (nini < 10 && k)) { cout << "no\n"; return 0; } if (k) { ans[cur + 1] = 3; ans[cur] = 3; break; } enter = 1; } ans[cur] = lst; ans[cur + 1] = lst; q.push(cur); q.push(cur + 1); cur += 2; n -= 2; curP++; if (curP == pot) pot *= 2, curP = 0; if (n <= 0) break; } if (k == 0 && !enter) cout << "no\n"; else { cout << "yes\n"; for (int i = 1; i <= nini; ++i) cout << ans[i] << " "; cout << endl; } } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int na, ka; int lowbit(int x) { return x & (-x); } bool chk(int n, int k) { if (k < 0) return 0; if (!(n % 2)) return 0; if (k && k * 2 + 3 > n) return 0; if (lowbit(n + 1) == n + 1 && k == 1) return 0; if (lowbit(n + 1) != n + 1 && k == 0) return 0; if (n == 9 && k == 2) return 0; return 1; } int cnt = 1; void pr(int u, int n, int k, int fa) { printf("%d ", fa); if (n == 1) return; for (int i = 1; i < n; i = i * 2 + 1) { int p; if (n - i - 1 >= i * 2 || i >= (n - i - 1) * 2) p = k - 1; else p = k; if (chk(i, 0) && chk(n - i - 1, p)) { pr(++cnt, i, 0, u); pr(++cnt, n - i - 1, p, u); return; } } } int main() { scanf("%d%d", &na, &ka); if (!chk(na, ka)) printf("NO\n"); else { printf("YES\n"); pr(1, na, ka, 0); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k, fa[100005]; bool check(int x) { return x - (x & -x) == 0; } int main() { scanf("%d%d", &n, &k); int mx = max((n - 3) / 2, 0); if (k > mx || (n % 2 == 0) || (n == 9 && k == 2) || (check(n + 1) && k == 1) || (!check(n + 1) && k == 0)) { printf("NO"); return 0; } printf("YES\n"); mx = 2 * max(k - 1, 0); for (register int i = 1; i <= mx; i += 2) fa[i] = max(i - 2, 0), fa[i + 1] = i; for (register int i = 1; i <= n - mx; i++) { if (i == 1) fa[i + mx] = max(0, mx - 1); else fa[i + mx] = i / 2 + mx; } if (check(n - mx + 1) && k) fa[n] = fa[n - 1] = 2; for (register int i = 1; i <= n; i++) printf("%d ", fa[i]); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int N, K, cnt; bool can(int n, int k) { if (!(n & 1)) return false; if (k == 0) return (n & (n + 1)) ? false : true; if (k == 1) return (n & (n + 1)) ? true : false; if (n == 9 && k == 2) return false; return k > 0 && k * 2 + 3 <= n; } void solve(int n, int k, int p) { printf("%d ", p); cnt++; int x = cnt; if (n == 1) return; for (int l = 1; l <= n; l = l * 2 + 1) { int r = n - l - 1; int kr = k; if (max(l, r) >= 2 * min(l, r)) kr--; if (can(l, 0) && can(r, kr)) { solve(l, 0, x); solve(r, kr, x); break; } } return; } int main() { scanf("%d %d", &N, &K); if (!can(N, K)) puts("NO"); else { puts("YES"); solve(N, K, 0); puts(""); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k; int s[100005]; int main() { cin >> n >> k; if (n == 1 && k == 0) return puts("YES\n0"), 0; if (n % 2 == 0 || n - 3 < k * 2) return puts("NO"), 0; if (n == 9 && k == 2) return puts("NO"), 0; if (k == 1 && ((n + 1) & (-(n + 1))) == n + 1) return puts("NO"), 0; if (k == 0) { if (((n + 1) & (-(n + 1))) != n + 1) return puts("NO"), 0; for (int i = 1; i <= n; ++i) { s[i] = (i >> 1); } puts("YES"); for (int i = 1; i <= n; ++i) printf("%d ", s[i]); return 0; } puts("YES"); if (n == 3) return puts("0 1 1"), 0; int cnt = 1; for (int i = 1; i < k; ++i) { s[++cnt] = cnt - 1; s[++cnt] = cnt - 2; } for (int i = cnt + 1; i <= n; ++i) { s[i] = ((i - cnt - 1) >> 1) + cnt; } if (((n - cnt + 2) & (-(n - cnt + 2))) == n - cnt + 2) s[n] = 2, s[n - 1] = 2; for (int i = 1; i <= n; ++i) printf("%d ", s[i]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
from heapq import * import sys def ng(): print("NO") exit() n, k = map(int,input().split()) if (n,k)==(1,0): print("YES") print(0) exit() ans = [0] * n popcnt = lambda x: bin(x).count("1") if n & 1 == 0 or n < 2 * k + 3 or (popcnt(n + 1) > 1 and k == 0): ng() u = 0 if popcnt(n + 1 - 2 * (k - 1)) == 1: for v in range(1, 4): ans[v] = v // 2+1 k -= 1 if n - 4 < 2 * k + 3 or k==0 or n<11: ng() ans[4] = 1 u = 4 for _ in range(k - 1): ans[u + 1] = ans[u + 2] = u+1 u += 2 for v in range(1,n-u):ans[v+u]=(v-1)//2+u+1 print("YES") print(*ans)
PYTHON3
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int p[100010]; int cnt; int solve0(int n) { if (n == 1) return ++cnt; int l = solve0(n / 2), r = solve0(n / 2); p[l] = p[r] = cnt + 1; return ++cnt; } int solve1(int n) { int base = 1 << (31 - __builtin_clz(n)), l, r; if (n + 1 == base * 3 / 2) l = solve0(base - 1), r = solve0(base / 2 - 1); else if ((base / 2) & n) l = solve0(base - 1), r = solve1(n - base); else l = solve0(base / 2 - 1), r = solve1(n - base / 2); p[l] = p[r] = cnt + 1; return ++cnt; } int solve(int n, int k) { if (k == 1) return solve1(n); int rem = n - (k - 1) * 2; if (rem & (rem + 1)) { int s = solve(n - 2, k - 1); p[s] = cnt + 1; p[cnt + 2] = cnt + 1; cnt += 2; return p[s]; } else { int s = solve(n - 4, k - 1); p[s] = cnt + 1; p[cnt + 2] = cnt + 1; p[cnt + 3] = p[cnt + 4] = cnt + 2; cnt += 4; return p[s]; } } void fail() { cout << "NO" << endl; exit(0); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; if (n == 9 && k == 2) fail(); if (n % 2 == 0) fail(); if (k == 0) { if (n & (n + 1)) fail(); cout << "YES" << endl; for (int i = 1; i <= n; i++) cout << (i / 2) << ' '; return 0; } if (k == 1 && !(n & (n + 1))) fail(); if ((n - 3) / 2 < k) fail(); cout << "YES" << endl; solve(n, k); for (int i = 1; i <= n; i++) cout << p[i] << ' '; cout << endl; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; template <typename __T> inline void read(__T &x) { x = 0; int f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } x *= f; } pair<int, int> mp(int a, int b) { return make_pair(a, b); } const int mod = 1000000007; long long qpow(long long a, long long b = mod - 2) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % mod; b >>= 1; a = a * a % mod; } return ans; } int popc(int n) { return __builtin_popcount(n); } int n, k; bool check(int n, int k) { if ((n & 1) == 0) return 0; if (n == 1 && k == 0) return 1; if (n == 9 && k == 2) return 0; int sb = (n - 3) / 2; if (k > sb) return 0; if (k < 0) return 0; if (n < 0) return 0; if (popc(n + 1) == 1) return k != 1; return k != 0; } int tot = 0; int faa[100005]; bool ckv(int a, int b) { return a >= 2 * b || b >= 2 * a; } void dfs(int n, int k, int fa) { faa[++tot] = fa; int now = tot; if (n == 1) return; if (k == 0) { dfs(n / 2, 0, now); dfs(n / 2, 0, now); return; } bool jb = 0; for (int a = 1; a <= n && jb == 0; a += 2) { bool fg = ckv(a, n - a - 1); int mv = min(a - 1, k); for (int x = 0; x <= mv; x++) { if (check(a, x) == 0) continue; if (check(n - a - 1, k - x - fg) == 0) continue; jb = 1; dfs(a, x, now); dfs(n - a - 1, k - x - fg, now); break; } } if (jb == 0) cout << "SMJB" << n << ' ' << k << endl; } int main() { int T; T = 1; while (T--) { read(n); read(k); if (check(n, k) == 0) { cout << "NO" << endl; } else { cout << "YES" << endl; dfs(n, k, 0); for (int i = 1; i <= n; i++) cout << faa[i] << ' '; cout << endl; } } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 1; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); if (f) x = -x; } template <typename F> inline void write(F x, char ed = '\n') { static short st[30]; short tp = 0; if (x < 0) putchar('-'), x = -x; do st[++tp] = x % 10, x /= 10; while (x); while (tp) putchar('0' | st[tp--]); putchar(ed); } template <typename T> inline void Mx(T &x, T y) { x < y && (x = y); } template <typename T> inline void Mn(T &x, T y) { x > y && (x = y); } int n, k; bool check(int n) { int mi = 1, t = n + 1; for (; mi <= t; mi <<= 1) if (mi == t) return 1; return 0; } void output(int n, int x, int f) { write(f, ' '); for (int i = 2; i <= n; i++) write(i / 2 + x, ' '); } namespace subtask1 { int main() { return puts("YES"), output(n, 0, 0), 0; } } // namespace subtask1 int main() { read(n), read(k); if ((~n & 1) || max(n - 3, 0) < k * 2) return puts("NO"), 0; if (n == 9 && k == 2) return puts("NO"), 0; if (k == 0) return check(n) ? subtask1::main() : (puts("NO"), 0); if (k == 1) return check(n) ? (puts("NO"), 0) : subtask1::main(); puts("YES"); write(0, ' '), write(1, ' '); k--; for (int i = 3; i <= 2 * k; i += 2) write(i - 2, ' '), write(i, ' '); int pre = 2 * k - 1; if (check(n - 2 * k)) k++, write(2, ' '), write(2, ' '); output(n - 2 * k, 2 * k, pre); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; const int nax = 105; bool dp[nax][nax]; bool f(int n, int k) { if (n % 2 == 0) { return false; } if (k == 0) { return __builtin_popcount(n + 1) == 1; } if (k == 2 && n == 9) { return false; } if (k == 1 && __builtin_popcount(n + 1) == 1) { return false; } return n >= 2 * k + 3; } const int MAX_N = 1e5 + 5; int parent[MAX_N]; void rec(int n, int k, int root) { assert(f(n, k)); if (n == 1) { return; } for (int n1 = 1; n1 < n; n1 += 2) { int n2 = n - 1 - n1; for (int k1 = 0; k1 <= min(n1, k); ++k1) { bool am_bad = (n1 >= 2 * n2 || n2 >= 2 * n1); int k2 = k - k1 - am_bad; if (k2 < 0) { continue; } if (f(n1, k1) && f(n2, k2)) { parent[root + 1] = root; parent[root + n1 + 1] = root; debug() << make_pair(n, k) << " = " << make_pair(n1, k1) << " + " << make_pair(n2, k2); rec(n1, k1, root + 1); rec(n2, k2, root + n1 + 1); return; } } } assert(false); } int main() { { int n, k; cin >> n >> k; if (f(n, k)) { rec(n, k, 1); puts("YES"); for (int i = 1; i <= n; ++i) { printf("%d ", parent[i]); } return 0; } puts("NO"); return 0; } dp[1][0] = true; for (int n1 = 1; n1 < nax; ++n1) { for (int n2 = 1; n1 + n2 + 1 < nax; ++n2) { for (int k1 = 0; k1 < n1; ++k1) { for (int k2 = 0; k2 < n2; ++k2) { if (dp[n1][k1] && dp[n2][k2]) { dp[n1 + n2 + 1][k1 + k2 + (n1 >= 2 * n2 || n2 >= 2 * n1)] = true; } } } } } for (int k = 0; k < 15; ++k) { for (int n = 1; n < nax; n += 2) { assert(f(n, k) == dp[n][k]); cout << dp[n][k]; } cout << endl; } cout << "OK" << endl; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const long long D = 30; long long deg[D] = {1}; vector<long long> ans; long long check() { vector<long long> sz(ans.size() + 1); vector<pair<long long, long long> > son(ans.size() + 1); long long ret = 0; for (long long i = ans.size() - 1; i >= 0; --i) { ++sz[i + 1]; sz[ans[i]] += sz[i + 1]; (!son[ans[i]].first ? son[ans[i]].first : son[ans[i]].second) = sz[i + 1]; if (son[i + 1].first) ret += son[i + 1].first * 2 <= son[i + 1].second || son[i + 1].first >= son[i + 1].second * 2; } return ret; } void make0(long long v, long long p, long long sz) { ans.push_back(p); for (long long i = 0; i < (long long)sz / 2; ++i) { ans.push_back(v + i); ans.push_back(v + i); } } long long make1(long long v, long long p, long long sz) { ans.push_back(p); long long half = *lower_bound(deg, deg + D, sz / 2); if (half <= (sz - 1 - half) * 2) { make0(v + 1, v, half); return half; } else { half = (half + 1 >> 1) - 1; if (half * 2 >= sz - 1 - half) { make0(v + 1, v, half); return half; } else for (long long i = 0; i < D; ++i) if (*lower_bound(deg, deg + D, sz - 1 - deg[i]) == sz - 1 - deg[i]) { make0(v + 1, v, deg[i]); make0(v + 1 + deg[i], v, sz - 1 - deg[i]); return sz - 1; } } } void solve1(long long sz, long long pv, long long v) { while (sz) { long long d = make1(v, pv, sz); sz -= d + 1; pv = v; v += d + 1; } } void solve() { for (long long i = 0; i < (long long)D; ++i) deg[i] = (1 << i) - 1; long long n, k; cin >> n >> k; long long sk = k; if (!(n & 1) || (k == 0 && *lower_bound(deg, deg + D, n) != n) || (k == 1 && *lower_bound(deg, deg + D, n) == n) || (k > (n - 2) / 2)) { cout << "NO"; return; } if (k == 0) make0(1, 0, n); else if (k == 1) solve1(n, 0, 1); else { long long sz = n, pv = 0, v = 1; while (k > 2) { ans.push_back(pv); ans.push_back(v); --k; -- --sz; pv = v; ++ ++v; } if (*lower_bound(deg, deg + D, sz - 2) != sz - 2) { ans.push_back(pv); ans.push_back(v); solve1(sz - 2, v, v + 2); } else { if (sz == n) { ans.push_back(0); ans.push_back(1); ans.push_back(2); ans.push_back(2); } else { ans.push_back(pv); ans.push_back(v); ans.push_back(2); ans.push_back(2); } solve1(sz - 4, v, v + 4); } } if (check() == sk) { cout << "YES\n"; for (long long x : ans) cout << x << " "; } else cout << "NO"; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 200050; int n, k, fa[N]; queue<int> q; inline bool check(int x) { x++; int w = 1; while (w < x) w <<= 1; return (w == x); } inline void get(int rt, int l, int r) { fa[l] = rt; q.push(l); for (int i = l + 1; i <= r; i += 2) { int now = q.front(); q.pop(); fa[i] = fa[i + 1] = now; q.push(i); q.push(i + 1); } } inline void T(int a) { for (int i = 2; i <= 2 * a; i++) { if (i & 1) fa[i] = i - 2; else fa[i] = i - 1; } } int main() { scanf("%d%d", &n, &k); if (!(n & 1)) printf("NO\n"); else if (k == 0) { if (check(n)) { printf("YES\n"); get(0, 1, n); for (int i = 1; i <= n; i++) printf("%d ", fa[i]); printf("\n"); } else printf("NO\n"); } else if (k == 1) { if (!check(n)) { printf("YES\n"); get(0, 1, n); for (int i = 1; i <= n; i++) printf("%d ", fa[i]); printf("\n"); } else printf("NO\n"); } else if (n < 2 * k + 3) printf("NO\n"); else if (n == 9 && k == 2) printf("NO\n"); else { printf("YES\n"); T(k - 1); if (check(n - 2 * (k - 1))) { get(2 * k - 2 - 1, 2 * k - 1, n - 2); fa[n - 1] = fa[n] = 2; } else get(2 * k - 2 - 1, 2 * k - 1, n); for (int i = 1; i <= n; i++) printf("%d ", fa[i]); printf("\n"); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char c = getchar(); while ((c < '0' || c > '9') && (c != '-')) c = getchar(); if (c == '-') f = -1, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } const int N = 510, maxn = 1e5 + 10; int n, k; bool flag[maxn]; inline void init() { for (int i = 1; i <= 100000; i <<= 1) flag[i] = 1; } int cnt, ans[maxn]; inline void solve0(int k, int fa) { ans[++cnt] = fa; if (k == 1) return; int now = cnt; solve0(k >> 1, now), solve0(k >> 1, now); } inline void solve(int n, int k, int rt) { if (n == 11 && k == 3) { int k = 3, now = cnt + 1; ans[++cnt] = rt; solve0(k, now), solve(n - 1 - k, 2, now); return; } if (k == 1) { if (flag[n - 1]) { ans[++cnt] = rt; ++cnt, ans[cnt] = cnt - 1; solve0(n - 2, cnt - 1); } else { int k = (1 << int(log2((n - 1) / 2))) - 1, now = cnt + 1; ans[++cnt] = rt; if (2 * k <= n - 1 - k) k = k * 2 + 1; solve0(k, now), solve(n - 1 - k, 1, now); } return; } if (k == 2) { if (flag[n - 1]) { int k = 3, now = cnt + 1; ans[++cnt] = rt; solve0(k, now), solve(n - 1 - k, 1, now); } else { ans[++cnt] = rt; ++cnt, ans[cnt] = cnt - 1; solve(n - 2, k - 1, cnt - 1); } return; } ans[++cnt] = rt; ++cnt, ans[cnt] = cnt - 1; solve(n - 2, k - 1, cnt - 1); } int main() { init(), n = read(), k = read(); if (n % 2 == 0) return puts("NO"), 0; if (n == 1) { if (k > 0) return puts("NO"), 0; printf("YES\n0\n"); return 0; } if ((n - 3) / 2 < k) return puts("NO"), 0; if (n == 9 && k == 2) return puts("NO"), 0; if (k == 0) { if (!flag[n + 1]) return puts("NO"), 0; solve0(n, 0); } else if (k == 1) { if (flag[n + 1]) return puts("NO"), 0; solve(n, k, 0); } else { solve(n, k, 0); } puts("YES"); for (register int i = (1); i <= (n); i++) printf("%d ", ans[i]); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 1e5; int n, m, t, a[maxn], b[maxn], ans; bool can(int num, int k) { if (num % 2 == 0 or k < 0) return 0; if (k <= 1) return k ^ (!(num & (num + 1))); if (num == 9 && k == 2) return 0; return 2 * k + 3 <= num; } void did(int num, int k, int from) { int now = ++t; cout << from << " "; for (int i = 1; i <= num - 1; i = 2 * i + 1) { int other = num - i - 1, otherk = k - (max(other, i) >= 2 * min(other, i)); if (can(i, 0) && can(other, otherk)) { did(i, 0, now), did(other, otherk, now); return; } } } void solve() { cin >> n >> m; if (!can(n, m)) cout << "NO"; else { cout << "YES\n"; did(n, m, 0); cout << "\n"; } return; } int main() { int T = 1; while (T--) solve(); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; char buf[1048576], *p0, *p1; inline int read() { int r = 0; char c = (p0 == p1 && (p1 = (p0 = buf) + fread(buf, 1, 1048576, stdin), p0 == p1) ? EOF : *p0++); while (c < 48 || c > 57) c = (p0 == p1 && (p1 = (p0 = buf) + fread(buf, 1, 1048576, stdin), p0 == p1) ? EOF : *p0++); while (c > 47 && c < 58) { r = (r << 3) + (r << 1) + (c ^ 48); c = (p0 == p1 && (p1 = (p0 = buf) + fread(buf, 1, 1048576, stdin), p0 == p1) ? EOF : *p0++); } return r; } int f[100005]; int main() { int n = read(), m, b; m = read(); if ((!(n & 1)) || (m > max((n - 3) >> 1, 0)) || (n == 9 && m == 2) || (n + 1 == ((n + 1) & (-n - 1)) && m == 1) || (n + 1 != ((n + 1) & (-n - 1)) && (!m))) { puts("NO"); return 0; } puts("YES"); b = max(m - 1, 0) << 1; f[b + 1] = max(b - 1, 0); for (int i = 1; i < b; i += 2) f[f[i + 1] = i] = max(i - 2, 0); for (int i = 2; i <= n - b; ++i) f[b + i] = b + (i >> 1); if (n - b + 1 == ((n - b + 1) & (b - n - 1)) && m) f[n - 1] = f[n] = 2; for (int i = 1; i <= n; ++i) printf("%d ", f[i]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; std::mt19937 rnd( (int)std::chrono::steady_clock::now().time_since_epoch().count()); long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 100000; int n, wantimbal; int par[MAXN]; int nxtnode; int calcmn(int n) { ++n; while (n % 2 == 0) n /= 2; return n == 1 ? 0 : 1; } int calcmx(int n) { if (n == 1) return 0; return (n - 3) / 2; } bool forbidden(int n, int imbal) { if (calcmn(n) == 0 && imbal == 1) return true; if (n == 7 && imbal == 1 || n == 9 && imbal == 2) return true; return false; } void rec(int target, int cnt, int at) { assert(cnt >= 1 && cnt % 2 == 1); if (cnt == 1) { assert(target == 0); return; } for (int l = 1; l < cnt; l += 2) { int r = cnt - l - 1; assert(r >= 1 && r % 2 == 1); int extra = max(l, r) >= 2 * min(l, r) ? 1 : 0; if (extra > target) continue; int lmn = calcmn(l), lmx = calcmx(l), rmn = calcmn(r), rmx = calcmx(r); if (extra + lmn + rmn > target) continue; if (extra + lmx + rmx < target) continue; int ltarget = min(target - extra - rmn, lmx); int rtarget = target - extra - ltarget; if (l == 7 && ltarget == 1) { if (rtarget < rmx) --ltarget, ++rtarget; else if (rtarget > rmn) ++ltarget, --rtarget; else continue; } if (r == 7 && rtarget == 1) { if (ltarget < lmx) --rtarget, ++ltarget; else if (ltarget > lmn) ++rtarget, --ltarget; else continue; } if (l == 7 && ltarget == 1 || r == 7 && rtarget == 1) continue; if (forbidden(l, ltarget) || forbidden(r, rtarget)) continue; assert(ltarget >= lmn && ltarget <= lmx && rtarget >= rmn && rtarget <= rmx && target == ltarget + rtarget + extra); int lnode = nxtnode++; int rnode = nxtnode++; par[lnode] = at; par[rnode] = at; rec(ltarget, l, lnode); rec(rtarget, r, rnode); return; } assert(false); } bool solve() { if (n % 2 == 0) return false; par[0] = -1, nxtnode = 1; if (wantimbal < calcmn(n) || wantimbal > calcmx(n) || forbidden(n, wantimbal)) return false; rec(wantimbal, n, 0); return true; } void run() { scanf("%d%d", &n, &wantimbal); if (!solve()) { printf("NO\n"); return; } printf("YES\n"); for (int i = (0); i < (n); ++i) { if (i != 0) printf(" "); printf("%d", par[i] + 1); } puts(""); } void research() { int n = 101; vector<int> mn(n + 1, INT_MAX); vector<pair<int, int>> howmn(n + 1, pair<int, int>(-1, -1)); vector<int> mx(n + 1, INT_MIN); vector<pair<int, int>> howmx(n + 1, pair<int, int>(-1, -1)); mn[1] = mx[1] = 0; for (int i = 3; i <= n; i += 2) { for (int l = 1; l < i; l += 2) { int r = i - l - 1; assert(r >= 1 && r % 2 == 1); int extra = max(l, r) >= 2 * min(l, r) ? 1 : 0; int curmn = mn[l] + mn[r] + extra; if (curmn < mn[i]) mn[i] = curmn, howmn[i] = make_pair(l, r); int curmx = mx[l] + mx[r] + extra; if (curmx > mx[i]) mx[i] = curmx, howmx[i] = make_pair(l, r); } } for (int i = (0); i <= (n); ++i) if (i % 2 == 1) assert(mn[i] == calcmn(i) && mx[i] == calcmx(i)); for (int i = (0); i <= (n); ++i) if (i % 2 == 1) printf("%d: %d..%d [%d/%d] [%d/%d]\n", i, mn[i], mx[i], howmn[i].first, howmn[i].second, howmx[i].first, howmx[i].second); } void verify() { vector<int> sz(n, 1); vector<vector<int>> chsz(n); assert(par[0] == -1); for (int i = n - 1; i >= 1; --i) { assert(par[i] < i); sz[par[i]] += sz[i]; chsz[par[i]].push_back(sz[i]); } for (int i = (0); i < (n); ++i) assert(((int)(chsz[i]).size()) == 0 || ((int)(chsz[i]).size()) == 2); int have = 0; for (int i = (0); i < (n); ++i) if (((int)(chsz[i]).size()) == 2) { int a = chsz[i][0], b = chsz[i][1]; if (max(a, b) >= 2 * min(a, b)) ++have; } assert(have == wantimbal); } void stress() { for (n = 1; n <= 1001; n += 2) { int mn = calcmn(n), mx = calcmx(n); for (wantimbal = mn; wantimbal <= mx; ++wantimbal) { if (forbidden(n, wantimbal)) continue; printf("n=%d wantimbal=%d\n", n, wantimbal); bool have = solve(); assert(have); verify(); } } } int main() { run(); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 100; int par[N]; int ID; int two(int x) { return __builtin_popcount(x + 1) == 1; } int solve(int n, int K) { if (K > 3) { int p = ++ID, u = ++ID; par[u] = p; int v = solve(n - 2, K - 1); par[v] = p; return p; } if (K == 3) { if (n != 11) { int p = ++ID, u = ++ID; par[u] = p; int v = solve(n - 2, K - 1); par[v] = p; return p; } else { int p = ++ID; par[solve(7, 2)] = p; par[solve(3, 0)] = p; return p; } } if (K == 2) { if (!two(n - 2)) { int p = ++ID; int u = ++ID; int v = solve(n - 2, K - 1); par[u] = par[v] = p; return p; } if (n == 17) { int p = ++ID; par[solve(3, 0)] = p; par[solve(13, 1)] = p; return p; } int p = ++ID; par[solve(n / 2 - 1, 0)] = p; par[solve(n / 2 + 1, 2)] = p; return p; } if (K == 1) { assert(two(n) == 0); int t = 2; while (true) { int x = t - 1, y = (n - 1) - x; if (x > y) swap(x, y); if (x >= 1 && y >= 1) { if (two(x) && two(y) && y >= 2 * x) { int p = ++ID; int u = solve(x, 0); int v = solve(y, 0); par[u] = p; par[v] = p; return p; } if (two(x) && !two(y) && y < 2 * x) { int p = ++ID; int u = solve(x, 0); int v = solve(y, 1); par[u] = par[v] = p; return p; } if (!two(x) && two(y) && y < 2 * x) { int p = ++ID; int u = solve(y, 0); int v = solve(x, 1); par[u] = par[v] = p; return p; } } else break; t *= 2; } assert(false); } if (K == 0) { assert(two(n)); int off = ID; for (int i = 1; i <= n; i++) { ++ID; par[ID] = (ID - off) / 2 + off; } return off + 1; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, K; cin >> n >> K; if (n % 2 == 0) { cout << "NO" << endl; return 0; } if (n == 1) { if (K == 0) { cout << "YES" << endl; cout << "0" << endl; } else { cout << "NO" << endl; } return 0; } int k = (n - 1) / 2; if (K > k - 1) { cout << "NO" << endl; return 0; } if (two(n) && K == 1) { cout << "NO" << endl; return 0; } if (!two(n) && K == 0) { cout << "NO" << endl; return 0; } if (n == 9 && K == 2) { cout << "NO" << endl; return 0; } solve(n, K); par[1] = 0; assert(ID == n); cout << "YES" << endl; for (int i = 1; i <= n; i++) { cout << par[i] << " "; } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int M = 1e6 + 5; int read() { int x = 0, y = 1; char ch = getchar(); while (ch < '0' || ch > '9') y = (ch == '-') ? -1 : 1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * y; } int lowbit(int x) { return x & (-x); } bool check(int x) { return x == lowbit(x); } int n, K, fa[M]; void solve() { n = read(), K = read(); int lim = max((n - 3) / 2, 0); if (n % 2 == 0 || K > lim || (n == 9 && K == 2) || (check(n + 1) && K == 1) || (!check(n + 1) && K == 0)) return (void)(printf("NO\n")); printf("YES\n"); int bas = 2 * max(0, K - 1); for (int i = 1; i < bas; i += 2) fa[i] = max(0, i - 2), fa[i + 1] = i; fa[bas + 1] = max(0, bas - 1); for (int i = 2; i <= n - bas; i++) fa[i + bas] = (i >> 1) + bas; if (check(n - bas + 1) && K) fa[n - 1] = fa[n] = 2; for (int i = 1; i <= n; i++) printf("%d ", fa[i]); printf("\n"); } signed main() { solve(); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; int K, N; int ans[MAXN]; bool check(int n, int k) { if (n == 1 && k == 0) return true; if (k * 2 + 3 > n) return false; if (n % 2 == 0) return false; if (n == 9 && k == 2) return false; if (__builtin_popcount(n + 1) == 1) { if (k == 1) return false; else return true; } return k != 0; } bool solve(int l, int r, int k) { if (l == r) return k == 0; for (int i = l + 1; i < r; i += 2) { int num_l = i - l; int num_r = r - i; int num_k = k; if (num_l * 2 <= num_r || num_r * 2 <= num_l) num_k--; for (int j = 0; j <= num_l / 2 && j <= num_k; j++) { if (check(num_l, j) && check(num_r, num_k - j)) { ans[l + 1] = l; ans[i + 1] = l; if (solve(l + 1, i, j) && solve(i + 1, r, num_k - j)) return true; } } } return false; } int main() { scanf("%d%d", &N, &K); if (!check(N, K)) printf("NO"); else if (!solve(1, N, K)) printf("NO"); else { printf("YES\n"); for (int i = 1; i <= N; i++) { printf("%d ", ans[i]); } } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k, cnt; bool check(int x, int y) { if (x % 2 == 0) return 0; if (y == 0) { if (x & (x + 1)) return 0; else return 1; } if (y == 1) { if (x & (x + 1)) return 1; else return 0; } if (x == 9 && y == 2) return 0; return y > 0 && y * 2 + 3 <= x; } void dfs(int x, int y, int cur) { cout << cur << " "; int xx = ++cnt; if (x == 1) return; for (int l = 1; l < n; l = l * 2 + 1) { int r = x - l - 1; int rr = y - (max(l, r) >= (min(l, r) * 2)); if (check(l, 0) && check(r, rr)) { dfs(l, 0, xx); dfs(r, rr, xx); return; } } } int main() { cin >> n >> k; if (!check(n, k)) { cout << "NO" << endl; return 0; } cout << "YES" << endl; dfs(n, k, 0); cout << endl; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int fa[100010]; int n, k; inline int read() { int ans = 0; char ch = getchar(); while (ch < '0' || ch > '9') ch = getchar(); while (ch >= '0' && ch <= '9') ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); return ans; } inline bool check(int x) { return (x & (-x)) == x; } int main() { n = read(), k = read(); int num = max((n - 3) / 2, 0); if (k > num) return puts("NO"), 0; if (!(n & 1)) return puts("NO"), 0; if (n == 9 && k == 2) return puts("NO"), 0; if (check(n + 1) && k == 1) return puts("NO"), 0; if (!check(n + 1) && !k) return puts("NO"), 0; puts("YES"); int len = max(0, (k + k - 2)); for (int i = 1; i <= len; i += 2) fa[i + 1] = i, fa[i] = i == 1 ? 0 : i - 2; for (int i = 1; i <= n - len; i++) if (i == 1) fa[i + len] = max(0, len - 1); else fa[i + len] = (i >> 1) + len; if (check(n - len + 1) && k) fa[n - 1] = fa[n] = 2; for (int i = 1; i <= n; i++) printf("%d%c", fa[i], i == n ? '\n' : ' '); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k; int ans[100000 + 10]; int p2 = 2 << 20; void pt(int start, int num) { for (int i = 0; i < num; i++) { ans[start + i] = i / 2 + start; } return; } int main() { scanf("%d%d", &n, &k); if (n == 1 && k == 0) { printf("YES\n0\n"); return 0; } if (n % 2 == 0) { printf("NO\n\n"); return 0; } if ((n - 3) / 2 < k) { printf("NO\n\n"); return 0; } if (n == 9 && k == 2) { printf("NO\n\n"); return 0; } if (p2 % (n + 1) == 0 && k == 0) { pt(1, n); printf("YES\n"); for (int i = 0; i < n; i++) { printf("%d ", ans[i]); } printf("\n"); } else if (p2 % (n + 1) == 0 && k == 1 || p2 % (n + 1) != 0 && k == 0) { printf("NO\n\n"); } else { int count = 1; while (k > 1) { ans[count * 2 - 1] = (count - 1) * 2 + 1; ans[count * 2] = (count - 1) * 2 + 1; count++; k--; } pt(count * 2 - 1, n - 2 * (count - 1)); if (k == 1 && p2 % (n - 2 * (count - 1) + 1) == 0) { ans[n - 2] = 2; ans[n - 1] = 2; } printf("YES\n"); for (int i = 0; i < n; i++) { printf("%d ", ans[i]); } printf("\n"); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; const int mod = 1000000007; int lowbit(int x) { return x & (-x); } int fast_power(int a, int b) { int x; for (x = 1; b; b >>= 1) { if (b & 1) x = 1ll * x * a % mod; a = 1ll * a * a % mod; } return x % mod; } int n, m; int ans[100005]; bool check(int n, int m) { if (n % 2 == 0) return 0; if (m < 0 || m > max(0, (n - 3) / 2)) return 0; if (!m) return !(n & (n + 1)); if (m == 1) return (n & (n + 1)); if (n == 9 && m == 2) return 0; return 1; } void dfs(int u, int t, int s, int fa) { ans[u] = fa; if (t == 1) return; for (int lsz = 1; lsz < t; lsz = lsz * 2 + 1) { int rsz = t - lsz - 1; int rs = s - (max(lsz, rsz) >= 2 * min(lsz, rsz)); if (check(lsz, 0) && check(rsz, rs)) { dfs(u + 1, lsz, 0, u); dfs(u + lsz + 1, rsz, rs, u); return; } } } int main() { scanf("%d%d", &n, &m); if (check(n, m)) { puts("YES"); dfs(1, n, m, 0); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); puts(""); } else puts("NO"); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 7; bool isbalanced(int x, int y) { return (2 * x > y && 2 * y > x); } bool ispow(int x) { return (__builtin_popcount(x + 1) == 1); } int getdep(int x) { return __builtin_ctz(x + 1); } int n, k, cnt; int ans[MAXN]; void build(int fa, int dep) { if (dep == 0) return; ans[++cnt] = fa; build(cnt, dep - 1); ans[++cnt] = fa; build(cnt, dep - 1); } void fail() { puts("NO"); exit(0); } bool check(int n, int k) { if ((n + 1) & 1) return false; if (n == 1 || n == 3) return k == 0; if (n == 5) return k == 1; if (n == 7) return k == 0 || k == 2; if (n == 9) return k == 1 || k == 3; if (k == 0) return ispow(n); if (k == 1) { for (int l = 1; l <= n; l = l * 2 + 1) { if (!isbalanced(l, n - l - 1) && check(n - l - 1, k - 1)) return true; if (isbalanced(l, n - l - 1) && check(n - l - 1, k)) return true; } return false; } return k <= max(0, (n - 3) / 2); } void dfs(int n, int k, int rt) { if (!n) return; if (k == 0) { if (!ispow(n)) fail(); build(rt, getdep(n) - 1); } else if (k == 1) { for (int y = (1 << (31 - __builtin_clz(n))); y >= 2; y /= 2) { if (isbalanced(n - y, y - 1) && check(n - y, k)) { ans[++cnt] = rt; build(cnt, getdep(y - 1) - 1); ans[++cnt] = rt; rt = cnt; dfs(n - y, k, rt); return; } else if (ispow(n - y) && ispow(y - 1) && !isbalanced(n - y, y - 1)) { ans[++cnt] = rt; build(cnt, getdep(n - y) - 1); ans[++cnt] = rt; build(cnt, getdep(y - 1) - 1); return; } } fail(); } else { for (int l = 1; l <= n; l = l * 2 + 1) { if (!isbalanced(l, n - l - 1) && check(n - l - 1, k - 1)) { ans[++cnt] = rt; dfs(l, 0, cnt); ans[++cnt] = rt; dfs(n - l - 1, k - 1, cnt); return; } } assert(false); } } int main() { scanf("%d %d", &n, &k); if (!check(n, k)) fail(); ans[++cnt] = 0; dfs(n, k, 1); puts("YES"); for (int i = 1; i <= cnt; i++) printf("%d%c", ans[i], " \n"[i == n]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; template <class T> inline void cmin(T &a, T b) { ((a > b) && (a = b)); } template <class T> inline void cmax(T &a, T b) { ((a < b) && (a = b)); } char IO; template <class T = int> T rd() { T s = 0; int f = 0; while (!isdigit(IO = getchar())) f |= IO == '-'; do s = (s << 1) + (s << 3) + (IO ^ '0'); while (isdigit(IO = getchar())); return f ? -s : s; } const int N = 1e5 + 10; int n, m; int fa[N]; int chk(int a, int b) { if (a > b) swap(a, b); return a * 2 <= b; } void Out() { puts("YES"); static int ind[N]; for (int i = 1, iend = n; i <= iend; ++i) ind[fa[i]]++; for (int i = 1, iend = n; i <= iend; ++i) if (ind[i] != 0 && ind[i] != 2) { cout << "SOMETHING WENT WRONG? " << i << ' ' << ind[i] << endl; exit(0); } for (int i = 1, iend = n; i <= iend; ++i) printf("%d ", fa[i]); exit(0); } int Get(int l, int r) { for (int i = l + 1, iend = r; i <= iend; ++i) fa[i] = l - 1 + (i - l + 1) / 2; return l; } int Mincost(int x) { if (~x & 1) return 1e9; for (int i = 0, iend = 17; i <= iend; ++i) if (x + 1 == (1 << i)) return 0; return 1; } int main() { n = rd(), m = rd(); if (m == 0) { if (Mincost(n) == 0) Get(1, n), Out(); puts("NO"), exit(0); } if (~n & 1) puts("NO"), exit(0); if ((m + 1) * 2 >= n) puts("NO"), exit(0); int r = (m + 1) * 2 + 1; if (r == n) { for (int i = 1, iend = m + 1; i <= iend; ++i) fa[i * 2] = i * 2 - 1, fa[i * 2 + 1] = i * 2 - 1; Out(); } r -= 2; int c = n - r + 2; for (int x = 1, xend = c - 1; x <= xend; ++x) if (Mincost(x) + Mincost(c - x) + (min(x, c - x) * 2 <= max(x, c - x)) == 1) { for (int i = 1, iend = m - 1; i <= iend; ++i) fa[i * 2] = i * 2 - 1, fa[i * 2 + 1] = i * 2 - 1; int t = max(0, r - 2); fa[Get(r - 1, r + x - 2)] = t; fa[Get(r + x - 1, n)] = t; Out(); } if (m > 1) for (int x = 1, xend = c - 1; x <= xend; ++x) if (Mincost(x) + Mincost(c - x) - !chk(c - x, n - 1 - (c - x)) + (x >= 3) == 1) { for (int i = 1, iend = m; i <= iend; ++i) fa[i * 2] = i * 2 - 1, fa[i * 2 + 1] = i * 2 - 1; int t = max(0, r - 2); fa[Get(r - 1, r + x - 2)] = t; fa[2] = t; fa[Get(r + x - 1, n)] = 1; Out(); } r -= 2; c = n - r + 2; if (r < 3) puts("NO"), exit(0); for (int x = 1, xend = c - 1; x <= xend; ++x) if (Mincost(x) + Mincost(c - x) + (min(x, c - x) * 2 <= max(x, c - x)) == 2) { for (int i = 1, iend = m - 2; i <= iend; ++i) fa[i * 2] = i * 2 - 1, fa[i * 2 + 1] = i * 2 - 1; int t = max(0, r - 2); fa[Get(r - 1, r + x - 2)] = t; fa[Get(r + x - 1, n)] = t; Out(); } r -= 2; c = n - r + 2; if (r < 3) puts("NO"), exit(0); for (int x = 1, xend = c - 1; x <= xend; ++x) if (Mincost(x) + Mincost(c - x) + (min(x, c - x) * 2 <= max(x, c - x)) == 3) { for (int i = 1, iend = m - 3; i <= iend; ++i) fa[i * 2] = i * 2 - 1, fa[i * 2 + 1] = i * 2 - 1; int t = max(0, r - 2); fa[Get(r - 1, r + x - 2)] = t; fa[Get(r + x - 1, n)] = t; Out(); } puts("NO"), exit(0); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 110000, maxm = 550000; int n, k, lim = 0, ans[maxn]; bool tag = 1; int lowbit(int x) { return x & (-x); } int main() { scanf("%d%d", &n, &k), lim = max((n - 3) / 2, 0); if (k > lim || !(n & 1)) tag = 0; if (n == 9 && k == 2) tag = 0; if (n + 1 - lowbit(n + 1) != 0 && k == 0) tag = 0; if (n + 1 - lowbit(n + 1) == 0 && k == 1) tag = 0; printf("%s", tag ? "YES\n" : "NO"); if (tag) { int b = 2 * max(0, k - 1); for (int i = 1; i < b; i += 2) ans[i + 1] = i, ans[i] = max(0, i - 2); for (int i = 1; i <= n - b; i++) { if (i == 1) ans[i + b] = max(0, b - 1); else ans[i + b] = (i / 2) + b; } if ((n - b + 1 - lowbit(n - b + 1) == 0) && k) ans[n - 1] = ans[n] = 2; for (int i = 1; i <= n; i++) printf("%d ", ans[i]); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int memo[105][6]; pair<int, int> next1[105][6]; pair<int, int> next2[105][6]; bool boleh(int n, int k); int dp(int n, int k) { if (n % 2 == 0) { return false; } if (k < 0) { return false; } if (n == 1) { return k == 0; } if (memo[n][k] != -1) { return memo[n][k]; } bool ans = false; for (int i = 1; i <= n - 2; i += 2) { bool subtract1 = false; if (n - i - 1 >= 2 * i || i >= 2 * (n - i - 1)) { subtract1 = true; } for (int j = 0; j <= k - subtract1; j++) { if (boleh(i, j) && boleh(n - i - 1, k - subtract1 - j)) { next1[n][k] = pair<int, int>(i, j); next2[n][k] = pair<int, int>(n - i - 1, k - subtract1 - j); ans = true; break; } } if (ans) { break; } } return memo[n][k] = ans; } int cnt = 2; int p[1000005]; void rec(int n, int k, int p2) { if (n == 1) { return; } int temp = cnt; p[temp] = p2; p[temp + 1] = p2; cnt += 2; pair<int, int> val1 = next1[n][k]; pair<int, int> val2 = next2[n][k]; rec(val1.first, val1.second, temp); rec(val2.first, val2.second, temp + 1); } bool isPowerOf2(int x) { return (x & (x - 1)) == 0; } void rec2(int n, int k, int p2) { if (k <= 3 && n <= 15) { rec(n, k, p2); return; } int temp = cnt; p[temp] = p2; p[temp + 1] = p2; cnt += 2; pair<int, int> val1, val2; if (k == 0) { val1 = pair<int, int>((n - 1) / 2, 0); val2 = pair<int, int>((n - 1) / 2, 0); } else if (k > 1) { if (k == 2 && isPowerOf2(n - 1)) { val1 = pair<int, int>(3, 0); val2 = pair<int, int>(n - 4, k - 1); } else { val1 = pair<int, int>(1, 0); val2 = pair<int, int>(n - 2, k - 1); } } else if ((n + 1) % 3 == 0 && isPowerOf2((n + 1) / 3)) { val1 = pair<int, int>(n / 3, 0); val2 = pair<int, int>(2 * n / 3 + 1, 0); } else { int size2 = (1 << (int)log2(n)) - 1; int size1 = n - 1 - size2; if (size2 >= 2 * size1) { size2 /= 2; size1 = n - 1 - size2; } val1 = pair<int, int>(size1, 1); val2 = pair<int, int>(size2, 0); } rec2(val1.first, val1.second, temp); rec2(val2.first, val2.second, temp + 1); } bool boleh(int n, int k) { if (n == 1) { return k == 0; } else if (n % 2 == 0) { return false; } else if (k > (n - 3) / 2) { return false; } else { if (n == 9 && k == 2) { return false; } if (isPowerOf2(n + 1) && k == 1) { return false; } else if (!isPowerOf2(n + 1) && k == 0) { return false; } return true; } } int main() { int n, k; scanf("%d%d", &n, &k); memset(memo, -1, sizeof(memo)); memset(p, 0, sizeof(p)); int temp = boleh(n, k); if (temp) { for (int i = 1; i <= min(n, 15); i += 2) { for (int j = 0; j <= 3; j++) { dp(i, j); } } printf("YES\n"); cnt = 2; memset(p, 0, sizeof(p)); rec2(n, k, 1); for (int i = 1; i <= n; i++) { printf("%d ", p[i]); } } else { printf("NO\n"); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 10; const int M = 20; vector<int> soln[M][M]; int compute(vector<int> sha) { vector<int> sz(sha.size(), 1); vector<int> out[sha.size()]; for (int i = sha.size() - 1; i > 0; i--) { sz[sha[i]] += sz[i]; out[sha[i]].push_back(i); } int l1, l2; int bb = 0; for (int i = 0; i < sha.size(); i++) { if (out[i].size() == 2) { l1 = sz[out[i][0]]; l2 = sz[out[i][1]]; if (l1 > l2) swap(l1, l2); if (l1 * 2 <= l2) { bb++; } } } return bb; } void brute(int cid, vector<int> pp) { if (cid == pp.size()) { int bal = compute(pp); soln[pp.size()][bal] = pp; return; } brute(cid + 1, pp); if (pp.size() + 2 < M) { pp.push_back(cid); pp.push_back(cid); brute(cid + 1, pp); } } bool ish(int n) { if ((n & (n + 1)) == 0) { return true; } return false; } int res[N]; bool valid(int n, int s1, int s2) { if (s1 > s2) swap(s1, s2); if (ish(s1) && ish(s2)) return false; int cn = !ish(s1) + !ish(s2) + (s1 * 2 <= s2); if (cn > 1) return false; return true; } int cnt = 1; void bins(int n, int c) { int root = cnt; res[cnt] = c; cnt++; if (n == 1) { return; } bins(n / 2, root); bins(n / 2, root); } void solve1(int n, int c) { int root = cnt; cnt++; res[root] = c; int k = 1; while ((k * 2 - 1) < n) { k *= 2; } int sz1 = (k - 1); int sz2 = (n - 1 - sz1); if (!valid(n, sz1, sz2)) { k /= 2; sz1 = k - 1; sz2 = (n - 1 - sz1); } if (ish(sz1)) bins(sz1, root); else solve1(sz1, root); if (ish(sz2)) bins(sz2, root); else solve1(sz2, root); } void extend(vector<int> pp, int c) { res[cnt] = c; for (int i = 1; i < pp.size(); i++) { res[cnt + i] = pp[i] + cnt; } cnt += pp.size(); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.tie(0); ; int n, k; cin >> n >> k; if (n % 2 == 0) { cout << "NO\n"; return 0; } if (k > max((n - 3) / 2, 0)) { cout << "NO\n"; return 0; } brute(0, {-1}); int par = 0; while (n > M && k > 1) { if (k == 2) { if (!ish(n - 2)) { n -= 2; res[cnt + 1] = cnt; res[cnt + 2] = cnt; par = cnt; cnt += 2; } else { n -= 4; res[cnt + 1] = cnt; res[cnt + 2] = cnt + 1; res[cnt + 3] = cnt + 1; res[cnt + 4] = cnt; par = cnt; cnt += 4; } } else { n -= 2; res[cnt + 1] = cnt; res[cnt + 2] = cnt; par = cnt; cnt += 2; } k--; } if (k == 0) { if (!ish(n)) { cout << "NO\n"; return 0; } bins(n, par); } else if (k == 1) { if (ish(n)) { cout << "NO\n"; return 0; } solve1(n, par); } else { if (soln[n][k].empty()) { cout << "NO\n"; return 0; } extend(soln[n][k], par); } cout << "YES\n"; for (int i = 1; i < cnt; i++) { cout << res[i] << " "; } cout << "\n"; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; const int ha = 998244353; int ok(int n, int k) { if (n == 9 && k == 2) return 0; if (~n & 1 || k < 0) return 0; if (n > 1 && (n - 3) / 2 < k) return 0; if (k == 0) return !(n & (n + 1)); if (k == 1) return n & (n + 1); return 1; } int cnt = 0; void work(int n, int k, int fa) { printf("%d ", fa); int now = ++cnt; for (int i = 0; (i << 1 | 1) < n; ++i) { int lsz = i << 1 | 1, rsz = n - lsz - 1; int z = (min(lsz, rsz) << 1 <= max(lsz, rsz)); if (ok(lsz, 0) && ok(rsz, k - z)) { work(lsz, 0, now); work(rsz, k - z, now); return; } } } int main() { int n, k; std::ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; if (!ok(n, k)) cout << "NO"; else { cout << "YES" << endl; work(n, k, 0); } }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> namespace ztd { using namespace std; template <typename T> inline T read(T& t) { t = 0; short f = 1; char ch = getchar(); double d = 0.1; while (ch < '0' || ch > '9') { if (ch == '-') f = -f; ch = getchar(); } while (ch >= '0' && ch <= '9') t = t * 10 + ch - '0', ch = getchar(); if (ch == '.') { ch = getchar(); while (ch <= '9' && ch >= '0') t += d * (ch ^ 48), d *= 0.1, ch = getchar(); } t *= f; return t; } const int mod = 998244353; inline long long ksm(long long x, long long y) { long long ret = 1; while (y) { if (y & 1) ret = ret * x % mod; y >>= 1; x = x * x % mod; } return ret; } inline long long inv(long long x) { return ksm(x, mod - 2); } } // namespace ztd using namespace ztd; const int maxn = 1e5 + 7; int n, k; inline int lowbit(int x) { return x & (-x); } inline bool ermi(int x) { return (x)-lowbit(x) == 0; } int fa[maxn]; signed main() { read(n); read(k); if (n % 2 == 0) { puts("NO"); return 0; } if (k > max((n - 3) / 2, 0)) { puts("NO"); return 0; } if (!ermi(n + 1) && k == 0) { puts("NO"); return 0; } if (ermi(n + 1) && k == 1) { puts("NO"); return 0; } if (n == 9 && k == 2) { puts("NO"); return 0; } puts("YES"); int base = max(2 * k - 2, 0); for (int i = 1; i < base; i += 2) fa[i + 1] = i, fa[i] = max(0, i - 2); for (int i = 1; i <= n - base; ++i) { fa[i + base] = max(i / 2 + base, 0); } fa[base + 1] = max(base - 1, 0); if (ermi(n - base + 1) && k) fa[n - 1] = fa[n] = 2, cerr << "ss\n"; for (int i = 1; i <= n; ++i) cout << fa[i] << ' '; cout << '\n'; return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 7; bool isbalanced(int x, int y) { return (2 * x > y && 2 * y > x); } bool ispow(int x) { return (__builtin_popcount(x + 1) == 1); } int getdep(int x) { return __builtin_ctz(x + 1); } int n, k, cnt; int ans[MAXN]; void build(int fa, int dep) { if (dep == 0) return; ans[++cnt] = fa; build(cnt, dep - 1); ans[++cnt] = fa; build(cnt, dep - 1); } void fail() { puts("NO"); exit(0); } bool check(int n, int k) { if ((n + 1) & 1) return false; if (n == 1 || n == 3) return k == 0; if (n == 5) return k == 1; if (n == 7) return k == 0 || k == 2; if (n == 9) return k == 1 || k == 3; if (k == 0) return ispow(n); if (k == 1) { for (int l = 1; l <= n; l = l * 2 + 1) { if (!isbalanced(l, n - l - 1) && check(n - l - 1, k - 1)) return true; if (isbalanced(l, n - l - 1) && check(n - l - 1, k)) return true; } return false; } return k <= max(0, (n - 3) / 2); } void dfs(int n, int k, int rt) { if (!n) return; if (k == 0) { if (!ispow(n)) fail(); build(rt, getdep(n) - 1); } else if (k == 1) { for (int y = (1 << (31 - __builtin_clz(n))); y >= 2; y /= 2) { if (isbalanced(n - y, y - 1) && check(n - y, k)) { ans[++cnt] = rt; build(cnt, getdep(y - 1) - 1); ans[++cnt] = rt; rt = cnt; dfs(n - y, k, rt); return; } else if (ispow(n - y) && ispow(y - 1) && !isbalanced(n - y, y - 1)) { ans[++cnt] = rt; build(cnt, getdep(n - y) - 1); ans[++cnt] = rt; build(cnt, getdep(y - 1) - 1); return; } } fail(); } else { bool ok = 0; for (int l = 1; l <= n; l = l * 2 + 1) { assert(ispow(l)); if (!isbalanced(l, n - l - 1) && check(n - l - 1, k - 1)) { ans[++cnt] = rt; dfs(l, 0, cnt); ans[++cnt] = rt; dfs(n - l - 1, k - 1, cnt); ok = 1; break; } } if (!ok) { printf("n = %d k = %d\n", n, k); } assert(ok); } } int main() { scanf("%d %d", &n, &k); if (!check(n, k)) fail(); ans[++cnt] = 0; dfs(n, k, 1); puts("YES"); for (int i = 1; i <= cnt; i++) printf("%d%c", ans[i], " \n"[i == n]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; namespace io { inline char nc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } template <class I> inline void fr(I &num) { num = 0; register char c = nc(); register int p = 1; while (c < '0' || c > '9') c == '-' ? p = -1, c = nc() : c = nc(); while (c >= '0' && c <= '9') num = num * 10 + c - '0', c = nc(); num *= p; } }; // namespace io using io ::fr; const int N = 1000005; int n, k, f[N]; inline bool chk(const int x) { return (x & (x + 1)) != 0; } inline bool ck(const int n, const int k) { if (!k && n == 1) return 1; if (!(n & 1)) return 0; if (k > (n - 3 >> 1)) return 0; if (n == 9 && k == 2) return 0; return chk(n) ? (k != 0) : (k != 1); } inline int Max(const int p, const int q) { return p > q ? p : q; } inline int Min(const int p, const int q) { return p < q ? p : q; } inline void solve(const int l, const int r, const int k) { if (l == r) return; if (!k) { for (register int i = l + 1, cnum = l, fl = 0; i <= r; cnum += fl, fl ^= 1, ++i) f[i] = cnum; return; } const int n = r - l; for (register int i = 1, x; i < n; ++i) { x = chk(i) + (Max(i, n - i) >= 2 * Min(i, n - i)); if (k >= x && ck(n - i, k - x)) return f[l + 1] = l, solve(l + 1, l + i, chk(i)), f[l + i + 1] = l, solve(l + i + 1, r, k - x); } } int main() { scanf("%d%d", &n, &k); if (!ck(n, k)) return puts("NO"), 0; solve(1, n, k), puts("YES"); for (register int i = 1; i <= n; ++i) printf("%d ", f[i]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, k; int par[100005]; void solve(int n1, int k1, int idx) { if (n1 == 1) return; if (n1 == 11 && k1 == 3 || __builtin_popcount(n1 - 1) == 1 && k1 == 2) { par[idx + 1] = idx; par[idx + 2] = par[idx + 3] = idx + 1; par[idx + 4] = idx; solve(n1 - 4, k1 - 1, idx + 4); } else if (k1 > 1) { par[idx + 1] = idx; par[idx + 2] = idx; solve(n1 - 2, k1 - 1, idx + 2); } else { int l, r; l = (1 << (30 - __builtin_clz(n1 + 1))) - 1; r = n1 - 1 - l; if (r > l * 2) { l = l * 2 + 1; r = n1 - 1 - l; } par[idx + 1] = idx; solve(l, 0, idx + 1); par[idx + l + 1] = idx; solve(r, (l >= r ? 0 : 1), idx + l + 1); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; if (n == 1 && k == 0) { cout << "YES\n0\n"; return 0; } if (n % 2 == 0 || k > (n - 3) / 2 || n == 9 && k == 2) { cout << "NO\n"; return 0; } bool b = __builtin_popcount(n + 1) == 1; if (b && k == 1 || !b && k == 0) { cout << "NO\n"; return 0; } cout << "YES\n"; int idx = 1; while (k > 3) { par[idx + 1] = par[idx + 2] = idx; idx += 2; k--; } solve(n - idx + 1, k, idx); for (int i = 1; i <= n; i++) cout << par[i] << ' '; cout << '\n'; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; long long N, K, cnt; long long can(long long n, long long k) { if (n % 2 == 0) { return 0; } if (k == 0) { if (n & (n + 1)) { return 0; } else { return 1; } } if (k == 1) { if (n & (n + 1)) { return 1; } else { return 0; } } if (n == 9 && k == 2) { return 0; } long long df = k > 0 && k * 2 + 3 <= n; return df; } void dfs(long long n, long long k, long long pre) { cout << pre << " "; long long x = ++cnt; if (n == 1) { return; } for (long long l = 1; l < n; l = l * 2 + 1) { long long r = n - 1 - l; long long kr = k - (max(l, r) >= (min(l, r) << 1)); if (can(l, 0) && can(r, kr)) { dfs(l, 0, x); dfs(r, kr, x); return; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N >> K; if (!can(N, K)) { cout << "NO\n"; return 0; } cout << "YES\n"; dfs(N, K, 0); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { int v = 0, f = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1; else v = (c & 15); while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); x = v * f; } inline void read(long long &x) { long long v = 0ll, f = 1ll; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1; else v = (c & 15); while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); x = v * f; } inline void readc(char &x) { char c; while (((c = getchar()) == ' ') || c == '\n') ; x = c; } int n, m, i, j, fa[100005]; bool checkfull(int x) { x++; if (x == (x & -x)) return 1; return 0; } int main() { read(n); read(m); if (n == 9 && m == 2) { puts("NO"); return 0; } if (!(n & 1)) { puts("NO"); return 0; } int lim = max(0, (n - 3) / 2); if (m > lim) { puts("NO"); return 0; } if (checkfull(n) && m == 1) { puts("NO"); return 0; } if (!checkfull(n) && m == 0) { puts("NO"); return 0; } puts("YES"); for (((i)) = (1); ((i)) <= ((m - 1)); ((i))++) { fa[i * 2 - 1] = max(0, i * 2 - 3); fa[i * 2] = i * 2 - 1; } int bse = max(0, m - 1) * 2; for (((i)) = (1); ((i)) <= ((n - bse)); ((i))++) { if (i == 1) { fa[i + bse] = max(0, bse - 1); } else { fa[i + bse] = i / 2 + bse; } } if (checkfull(n - bse) && m) { fa[n - 1] = fa[n] = 2; } for (((i)) = (1); ((i)) <= ((n)); ((i))++) { printf("%d ", fa[i]); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> const int N = 1e5 + 5; using namespace std; int n, m, k, cnt, num[105], fa[N], stk[N], top; template <typename T> inline T read() { T x = 0, w = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * w; } bool check(int x) { int cnt = 0; while (x) cnt += x & 1, x >>= 1; return cnt == 1; } int main() { n = read<int>(), k = read<int>(), m = max((n - 3) / 2, 0); if (k > m) return puts("NO"), 0; if (n % 2 == 0) return puts("NO"), 0; if (n == 9 && k == 2) return puts("NO"), 0; if (check(n + 1) && k == 1) return puts("NO"), 0; if (!check(n + 1) && !k) return puts("NO"), 0; puts("YES"); int base = max(0, 2 * (k - 1)); for (int i = 1; i <= base; i += 2) fa[i + 1] = i, fa[i] = max(0, i - 2); for (int i = 1; i <= n - base; i++) { if (i == 1) fa[i + base] = max(0, base - 1); else fa[i + base] = (i >> 1) + base; } if (check(n - base + 1) && k) fa[n - 1] = fa[n] = 2; for (int i = 1; i <= n; i++) printf("%d%c", fa[i], i == n ? '\n' : ' '); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> const int MAX_N = 100000; int son[1 + MAX_N]; bool canDo[4][1 + MAX_N]; int root(int N, int K, int firstNode) { if (N % 2 == 0) return -1; if (K >= 3 && N < 2 * K + 3) return -1; else if (K >= 4) { int a = root(1, 0, firstNode); int b = root(N - 2, K - 1, firstNode + 1); son[a] = firstNode + N - 1; son[b] = firstNode + N - 1; return firstNode + N - 1; } if (K < 3 && !canDo[K][N]) return -1; if (K == 0 && N > 1) { int a = root(N / 2, 0, firstNode); int b = root(N / 2, 0, firstNode + N / 2); son[a] = firstNode + N - 1; son[b] = firstNode + N - 1; return firstNode + N - 1; } else if (K == 0 && N == 1) return firstNode; for (int na = 1; na < N; na += 2) { int nb = N - na - 1; for (int ka = 0; ka < 4; ++ka) { int kb = K - ka - (2 * na <= nb || 2 * nb <= na); if (kb >= 0 && canDo[ka][na] && canDo[kb][nb]) { int a = root(na, ka, firstNode); int b = root(nb, kb, firstNode + na); son[a] = firstNode + N - 1; son[b] = firstNode + N - 1; return firstNode + N - 1; } } } return -1; } void init() { for (int i = 1; (1 << i) - 1 <= MAX_N; ++i) canDo[0][(1 << i) - 1] = true; ; for (int i = 1; i <= MAX_N; ++i) { int ic = i; while (ic % 2 == 1) ic /= 2; if (ic > 0) canDo[1][i] = true; if (i >= 7 && i != 9) canDo[2][i] = true; if (i >= 9) canDo[3][i] = true; } } int main() { int N, K; scanf("%d%d", &N, &K); init(); if (root(N, K, 1) == -1) { printf("NO"); } else { printf("YES\n"); for (int i = 1; i <= N; ++i) printf("%d ", son[i]); } return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; int n, m, i, j, k, fa[100005]; int f(int n) { n++; return (n ^ (n & -n)) > 0; } int g(int n) { return (n == 1) ? 0 : (n - 3) / 2; } void cover(int x, int p, int n) { fa[x] = p; if (n == 1) return; for (int k = 1; k <= n; k = k << 1 | 1) if ((k << 1 | 1) > (n - 1) / 2) { cover(x + 1, x, k); cover(x + k + 1, x, n - 1 - k); return; } } void write() { printf("YES\n"); for (i = 1; i <= n; i++) printf("%d ", fa[i]); } void cover(int st, int m) { for (int i = 2; i <= m; i++) fa[st + i - 1] = st + i / 2 - 1; } int main() { scanf("%d%d", &n, &m); if (!(n & 1)) printf("NO\n"), exit(0); if (m < f(n) || m > g(n)) printf("NO\n"), exit(0); if (n == 9 && m == 2) printf("NO\n"), exit(0); if (n == 1) printf("YES\n0\n"), exit(0); if (m <= 1) { if (f(n) == m) cover(1, n), write(); else printf("NO\n"); exit(0); } for (i = 0; i < (m - 1); i++) fa[2 * i + 2] = fa[2 * i + 3] = 2 * i + 1; cover(2 * m - 1, n - 2 * (m - 1)); if (!f(n - 2 * (m - 1))) fa[n] = fa[n - 1] = 2; write(); }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; char buf[1048576], *p0, *p1; inline int read() { int r = 0; char c = (p0 == p1 && (p1 = (p0 = buf) + fread(buf, 1, 1048576, stdin), p0 == p1) ? EOF : *p0++); while (c < 48 || c > 57) c = (p0 == p1 && (p1 = (p0 = buf) + fread(buf, 1, 1048576, stdin), p0 == p1) ? EOF : *p0++); while (c > 47 && c < 58) { r = (r << 3) + (r << 1) + (c ^ 48); c = (p0 == p1 && (p1 = (p0 = buf) + fread(buf, 1, 1048576, stdin), p0 == p1) ? EOF : *p0++); } return r; } int f[100005]; int main() { int n = read(), m, b; m = read(); if ((!(n & 1)) || (m > max((n - 3) >> 1, 0)) || (n == 9 && m == 2) || (n + 1 == ((n + 1) & (-n - 1)) && m == 1) || (n + 1 != ((n + 1) & (-n - 1)) && (!m))) { puts("NO"); return 0; } puts("YES"); b = max(m - 1, 0) << 1; f[b + 1] = max(b - 1, 0); for (int i = 1; i < b; i += 2) f[f[i + 1] = i] = max(i - 2, 0); for (int i = 2; i <= n - b; ++i) f[b + i] = b + (i >> 1); if (n - b + 1 == ((n - b + 1) & (b - n - 1)) && m) f[n - 1] = f[n] = 2; for (int i = 1; i <= n; ++i) printf("%d ", f[i]); return 0; }
CPP
1379_E. Inverse Genealogy
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2
11
#include <bits/stdc++.h> using namespace std; template <typename T> istream& operator>>(istream& in, vector<T>& a) { for (auto& i : a) in >> i; return in; } template <typename T> ostream& operator<<(ostream& out, const vector<T>& a) { for (auto& i : a) { out << i << " "; } return out; } template <typename T, typename D> istream& operator>>(istream& in, pair<T, D>& a) { in >> a.first >> a.second; return in; } template <typename T, typename D> ostream& operator<<(ostream& out, const pair<T, D>& a) { out << a.first << " " << a.second; return out; } const long long INFL = 9187201950435737471; const long long nINFL = -9187201950435737472; const long long INF = 2139062143; const long long nINF = -2139062144; const unsigned long long ULINF = numeric_limits<unsigned long long>::max(); const long double PI = acos(-1); auto seed = chrono::high_resolution_clock::now().time_since_epoch().count(); mt19937 rnd(seed); inline void IO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const long long MAXN = 2e5; const long long MAXE = 100 + 15; const long long MOD = 1e9 + 7; const double EPS = 1e-10; void MyAssert(bool f = false) { if (!f) { cout << "NO"; exit(0); } } long long timer = 0; long long ans[MAXN]; set<long long> st; void BuildTreeWith1(long long tl, long long tr) { if (tl == tr) { return; } long long childrenSz = tr - tl; tr--; long long leftSize = 0; while (leftSize * 3 < childrenSz) { leftSize = leftSize * 2 + 1; } long long tm = tl + leftSize - 1; long long curV = timer + 1; ans[++timer] = curV; BuildTreeWith1(tl, tm); ans[++timer] = curV; BuildTreeWith1(tm + 1, tr); } void solve() { for (long long i = 0; i < (20); ++i) { st.insert(1 << i); } long long n, k; cin >> n >> k; long long nn = n; if ((n & 1) == 0 || k > max(0ll, (n - 3) / 2)) { MyAssert(); } while (n > 13) { long long curV = timer + 1; if (k == 0) { MyAssert(st.count(n + 1)); BuildTreeWith1(0, n - 1); break; } if (k == 1) { MyAssert(!st.count(n + 1)); BuildTreeWith1(0, n - 1); break; } if (k == 2 && st.count(n - 1)) { ans[++timer] = curV; BuildTreeWith1(0, 2); ans[++timer] = curV; k--; n -= 4; } else { ans[++timer] = curV; ans[++timer] = curV; k--; n -= 2; } } if (n == 13 && k == 4) { long long curV = timer + 1; ans[++timer] = curV; BuildTreeWith1(0, 2); ans[++timer] = curV; n -= 4; k--; } if (k == 0) { MyAssert(st.count(n + 1)); BuildTreeWith1(0, n - 1); } else if (k == 1 && !st.count(n + 1)) { BuildTreeWith1(0, n - 1); } else if (k * 2 + 3 == n || (n == 11 && k == 2) || (n == 13 && k == 2)) { while (1) { long long curV = timer + 1; if (k == 0) { MyAssert(st.count(n + 1)); BuildTreeWith1(0, n - 1); break; } if (k == 1) { MyAssert(!st.count(n + 1)); BuildTreeWith1(0, n - 1); break; } if (k == 2 && st.count(n - 1)) { ans[++timer] = curV; BuildTreeWith1(0, 2); ans[++timer] = curV; k--; n -= 4; } else { ans[++timer] = curV; ans[++timer] = curV; k--; n -= 2; } } } else { MyAssert(); } cout << "YES" << '\n'; for (long long i = 0; i < nn; i++) { cout << ans[i] << ' '; } } signed main() { IO(); long long t = 1; while (t--) { solve(); } return 0; }
CPP