查找可以在树中折断的边数,以使结果两棵树的按位或在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的每个比特,设置了该特定比特的数字在两个结果树中均为零,或者在两个结果树中均大于零,并且已经观察到条件如果对所有位都满足,则将边沿计入结果。
示例
// 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