原文:
给定一个树的 ,任务是打印该树中具有素数度的节点。 例:
input: arr[] = {4, 1, 3, 4}
output: 1 3 4
explanation:
the tree is:
2----4----3----1----5
|
6
hence, the degree of 1, 3 and 4
are 2, 2 and 3 respectively
which are prime.
input: a[] = {1, 2, 2}
output: 1 2
进场:
- 因为如果 n 是节点数,那么 prufer 序列的长度是n–2。因此,创建一个比普鲁弗序列长度大 2 倍的数组度[] 。
- 最初,用 1 填充度数数组。
- 迭代普鲁弗序列,增加每个元素在度表中的出现频率。这种方法之所以有效,是因为普鲁弗序列中一个节点的频率比树中的度数少一个。
- 此外,为了检查节点度是否为素数,我们将使用埃拉托斯特尼的筛。创建一个筛子,它将帮助我们识别度在 o(1)时间内是否是质数。
- 如果一个节点有质数,则打印节点号。
以下是上述方法的实现:
c
// c implementation to print the
// nodes with prime degree from the
// given prufer sequence
#include
using namespace std;
// function to create sieve
// to check primes
void sieveoferatosthenes(
bool prime[], int p_size)
{
// false here indicates
// that it is not prime
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= p_size; p ) {
// if prime[p] is not changed,
// then it is a prime
if (prime[p]) {
// update all multiples of p,
// set them to non-prime
for (int i = p * 2; i <= p_size;
i = p)
prime[i] = false;
}
}
}
// function to print the nodes with
// prime degree in the tree
// whose prufer sequence is given
void primedegreenodes(int prufer[], int n)
{
int nodes = n 2;
bool prime[nodes 1];
memset(prime, true, sizeof(prime));
sieveoferatosthenes(prime, nodes 1);
// hash-table to mark the
// degree of every node
int degree[n 2 1];
// initially let all the degrees be 1
for (int i = 1; i <= nodes; i )
degree[i] = 1;
// increase the count of the degree
for (int i = 0; i < n; i )
degree[prufer[i]] ;
// print the nodes with prime degree
for (int i = 1; i <= nodes; i ) {
if (prime[degree[i]]) {
cout << i << " ";
}
}
}
// driver code
int main()
{
int a[] = { 4, 1, 3, 4 };
int n = sizeof(a) / sizeof(a[0]);
primedegreenodes(a, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java implementation to print the
// nodes with prime degree from the
// given prufer sequence
import java.util.*;
class gfg{
// function to create sieve
// to check primes
static void sieveoferatosthenes(
boolean prime[], int p_size)
{
// false here indicates
// that it is not prime
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= p_size; p ) {
// if prime[p] is not changed,
// then it is a prime
if (prime[p]) {
// update all multiples of p,
// set them to non-prime
for (int i = p * 2; i <= p_size;
i = p)
prime[i] = false;
}
}
}
// function to print the nodes with
// prime degree in the tree
// whose prufer sequence is given
static void primedegreenodes(int prufer[], int n)
{
int nodes = n 2;
boolean []prime = new boolean[nodes 1];
arrays.fill(prime, true);
sieveoferatosthenes(prime, nodes 1);
// hash-table to mark the
// degree of every node
int []degree = new int[n 2 1];
// initially let all the degrees be 1
for (int i = 1; i <= nodes; i )
degree[i] = 1;
// increase the count of the degree
for (int i = 0; i < n; i )
degree[prufer[i]] ;
// print the nodes with prime degree
for (int i = 1; i <= nodes; i ) {
if (prime[degree[i]]) {
system.out.print(i " ");
}
}
}
// driver code
public static void main(string[] args)
{
int a[] = { 4, 1, 3, 4 };
int n = a.length;
primedegreenodes(a, n);
}
}
// this code contributed by princi singh
python 3
# python3 implementation to print the
# nodes with prime degree from the
# given prufer sequence
# function to create sieve
# to check primes
def sieveoferatosthenes(prime, p_size):
# false here indicates
# that it is not prime
prime[0] = false
prime[1] = false
p = 2
while (p * p <= p_size):
# if prime[p] is not changed,
# then it is a prime
if (prime[p]):
# update all multiples of p,
# set them to non-prime
for i in range(p * 2, p_size 1, p):
prime[i] = false
p = 1
# function to print the nodes with
# prime degree in the tree
# whose prufer sequence is given
def primedegreenodes(prufer, n):
nodes = n 2
prime = [true] * (nodes 1)
sieveoferatosthenes(prime, nodes 1)
# hash-table to mark the
# degree of every node
degree = [0] * (n 2 1);
# initially let all the degrees be 1
for i in range(1, nodes 1):
degree[i] = 1;
# increase the count of the degree
for i in range(0, n):
degree[prufer[i]] = 1
# print the nodes with prime degree
for i in range(1, nodes 1):
if prime[degree[i]]:
print(i, end = ' ')
# driver code
if __name__=='__main__':
a = [ 4, 1, 3, 4 ]
n = len(a)
primedegreenodes(a, n)
# this code is contributed by rutvik_56
c
// c# implementation to print the
// nodes with prime degree from the
// given prufer sequence
using system;
class gfg{
// function to create sieve
// to check primes
static void sieveoferatosthenes(bool []prime,
int p_size)
{
// false here indicates
// that it is not prime
prime[0] = false;
prime[1] = false;
for(int p = 2; p * p <= p_size; p )
{
// if prime[p] is not changed,
// then it is a prime
if (prime[p])
{
// update all multiples of p,
// set them to non-prime
for(int i = p * 2; i <= p_size;
i = p)
prime[i] = false;
}
}
}
// function to print the nodes with
// prime degree in the tree
// whose prufer sequence is given
static void primedegreenodes(int []prufer, int n)
{
int nodes = n 2;
bool []prime = new bool[nodes 1];
for(int i = 0; i < prime.length; i )
prime[i] = true;
sieveoferatosthenes(prime, nodes 1);
// hash-table to mark the
// degree of every node
int []degree = new int[n 2 1];
// initially let all the degrees be 1
for(int i = 1; i <= nodes; i )
degree[i] = 1;
// increase the count of the degree
for(int i = 0; i < n; i )
degree[prufer[i]] ;
// print the nodes with prime degree
for(int i = 1; i <= nodes; i )
{
if (prime[degree[i]])
{
console.write(i " ");
}
}
}
// driver code
public static void main(string[] args)
{
int []a = { 4, 1, 3, 4 };
int n = a.length;
primedegreenodes(a, n);
}
}
// this code is contributed by 29ajaykumar
java 描述语言
output:
1 3 4
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处