在 C++ 中查找可以在树中断开的边数,使得生成的两个树的按位或相等
概念
对于给定的一棵具有 m 个节点的树,每个节点都关联一个数字,我们可以断开任何树边,这将导致形成 2 棵新树。在这里,我们必须以这种方式计算边的数量,以便在断开该边后构造的两棵树中存在的节点的按位或相等。需要注意的是,每个节点的值都 ≤ 10^6。
输入
values[]={1, 3, 1, 3}
1
/ | \
2 3 4输出
2
这里,可以在 1 和 2 之间的边断开,生成的两个树的按位或将为 3。
同样,也可以断开 1 和 4 之间的边。
方法
这里,上述问题可以通过实现简单的深度优先搜索 (DFS) 来解决。因为节点的值 ≤ 10^6,所以可以使用 22 个二进制位来表示。因此,节点值的按位或也可以用 22 个二进制位来表示。这里,方法是确定在子树的所有值的每个位中设置位的次数。对于每条边,我们将验证从 0 到 21 的每个位,具有该特定位设置为 1 的数字在两棵生成的树中均为零或在两棵生成的树中均大于零,如果发现所有位都满足该条件,则将该边计入结果中。
示例
// C++ implementation of the approach
#include<bits/stdc++.h>
using namespace std;
int m1[1000],x1[22];
// Uses array to store the number of times each bit
// is set in all the values of a subtree
int a1[1000][22];
vector<vector<int>> g;
int ans1 = 0;
// Shows function to perform simple DFS
void dfs(int u1, int p1){
for (int i=0;i<g[u1].size();i++) {
int v1 = g[u1][i];
if (v1 != p1) {
dfs(v1, u1);
// Determining the number of times each bit is set
// in all the values of a subtree rooted at v
for (int i = 0; i < 22; i++)
a1[u1][i] += a1[v1][i];
}
}
// Verifying for each bit whether the numbers
// with that particular bit as set are
// either zero in both the resulting trees or
// greater than zero in both the resulting trees
int pp1 = 0;
for (int i = 0; i < 22; i++) {
if (!((a1[u1][i] > 0 && x1[i] - a1[u1][i] > 0)
|| (a1[u1][i] == 0 && x1[i] == 0))) {
pp1 = 1;
break;
}
}
if (pp1 == 0)
ans1++;
}
// Driver code
int main(){
// Number of nodes
int n1 = 4;
// int n1 = 5;
// Uses ArrayList to store the tree
g.resize(n1+1);
// Uses array to store the value of nodes
m1[1] = 1;
m1[2] = 3;
m1[3] = 1;
m1[4] = 3;
/* m1[1] = 2;
m1[2] = 3;
m1[3] = 32;
m1[4] = 43;
m1[5] = 8;*/
//Uses array to store the number of times each bit
// is set in all the values in complete tree
for (int i = 1; i <= n1; i++) {
int y1 = m1[i];
int k1 = 0;
// Determining the set bits in the value of node i
while (y1 != 0) {
int p1 = y1 % 2;
if (p1 == 1) {
x1[k1]++;
a1[i][k1]++;
}
y1 = y1 / 2;
k1++;
}
}
// push_back edges
g[1].push_back(2);
g[2].push_back(1);
g[1].push_back(3);
g[3].push_back(1);
g[1].push_back(4);
g[4].push_back(1);
//g[1].push_back(5);
//g[5].push_back(1);
dfs(1, 0);
cout<<(ans1);
}输出
2
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP