Java 查找树中k元组的总数
问题陈述:在树中查找k元组(不作为二叉树提及) 有一棵树,其中N个节点的根为1,其中第i个节点与值Ai关联。现在考虑下面的定义:Java 查找树中k元组的总数,java,algorithm,tree,graph-theory,depth-first-search,Java,Algorithm,Tree,Graph Theory,Depth First Search,问题陈述:在树中查找k元组(不作为二叉树提及) 有一棵树,其中N个节点的根为1,其中第i个节点与值Ai关联。现在考虑下面的定义: K-Tuple:如果: 1)席是席+1的祖先∀1.≤我≤K 2.)AXi>AXi+1∀1.≤我≤K 计算树中K元组的数量 输入: 第1行:2个整数N和K、树中的节点数以及需要计算答案的元组类型的值。 第2行:N个空格分隔的整数,其中 第i个整数表示值Ai。 N−1行:X和Y表示它们之间有一条边 约束条件: 1.≤N、 哎≤100000, 1≤十、 Y≤N、 一,≤K≤
K-Tuple:如果:
1)席是席+1的祖先∀1.≤我≤K
2.)AXi>AXi+1∀1.≤我≤K 计算树中K元组的数量 输入:
第1行:2个整数N和K、树中的节点数以及需要计算答案的元组类型的值。
第2行:N个空格分隔的整数,其中 第i个整数表示值Ai。
N−1行:X和Y表示它们之间有一条边 约束条件:
1.≤N、 哎≤100000, 1≤十、 Y≤N、 一,≤K≤二十 输出:元组数 这是我试过的代码
public class Solutions {
static class Node {
int index, value, tupleLength;
Node parent;
boolean inTuple, hasChildren;
static int tupleCount = 0;
Node (int index, int value) {
this.index = index;
this.value = value;
this.tupleLength = 0;
this.parent = null;
this.inTuple = false;
this.hasChildren = false;
}
// to add an edge between child and parent
void addChild (Node child) {
this.hasChildren = true;
child.parent = this;
}
void setTupleLengths (int k) {
// stop looking for tuple if root node is encountered
if (this.parent == null)
return;
// conditions for a parent node to be in a tuple
if ((this.parent.index < this.index) && (this.parent.value > this.value)) {
// if parent is already part of a k-tuple, no need to proceed further, just increment the tuple count and return
if ((this.parent.tupleLength >= this.tupleLength + 1) && (this.parent.tupleLength >= k)) {
tupleCount++;
return;
}
// if parent satisfies the tuple conditions, then add it to the m-1 tuple to get m-tuple
this.parent.tupleLength = this.tupleLength + 1;
// wrote this condition as any (k+1)-tuple will have 2 k-tuples etc.
if (this.parent.tupleLength >= k)
tupleCount++;
this.parent.inTuple = true;
this.parent.setTupleLengths(k);
}
else {
// if parent is not in tuple, then start looking for a new tuple which covers this parent's ancestors starting from it.
if (!this.parent.inTuple)
this.parent.setTupleLengths(k);
}
}
}
// to get all leaf nodes
static ArrayList<Node> getLeaves (ArrayList<Node> nodes) {
ArrayList<Node> leaves = new ArrayList<>();
for (Node node : nodes) {
if (!node.hasChildren)
leaves.add(node);
}
return leaves;
}
// prints the max length of a tuple starting from each node for all nodes
static void printTupleLengths (ArrayList<Node> nodes) {
for (Node node : nodes)
System.out.print ("(" + node.index + "," + node.value + "," + node.tupleLength + ") ");
System.out.println ();
}
public static void main (String args[]) {
String fileName = "/home/user/Downloads/750ac6c427-input-750a1f8.txt.clean.txt";
File file = new File(fileName);
try {
Scanner sc = new Scanner (file);
//int t = sc.nextInt();
int n = sc.nextInt(), k = sc.nextInt();
ArrayList<Node> nodes= new ArrayList<>();
for (int i = 0; i<n; i++) {
int value = sc.nextInt();
Node node = new Node(i+1, value);
nodes.add(node);
}
for (int i = 1; i<n; i++) {
int x = sc.nextInt(), y = sc.nextInt();
nodes.get(x-1).addChild(nodes.get(y-1));
}
//printTupleLengths(nodes);
ArrayList<Node> leaves = getLeaves(nodes);
for (Node leaf : leaves) {
leaf.setTupleLengths(k);
}
//printTupleLengths(nodes);
System.out.println (Node.tupleCount);
} catch (Exception e) {
System.out.println (e);
}
}
}
公共类解决方案{
静态类节点{
int索引,值,元组长度;
节点父节点;
布尔插管,有子代;
静态int-tupleCount=0;
节点(int索引,int值){
这个指数=指数;
这个值=值;
this.tupleLength=0;
this.parent=null;
this.inTuple=false;
this.hasChildren=false;
}
//在子对象和父对象之间添加边的步骤
void addChild(节点子节点){
this.hasChildren=true;
child.parent=这个;
}
无效设置长度(整数k){
//如果遇到根节点,请停止查找元组
if(this.parent==null)
返回;
//父节点位于元组中的条件
if((this.parent.indexthis.value)){
//如果父级已经是k元组的一部分,则无需继续,只需增加元组计数并返回
如果((this.parent.tupleLength>=this.tupleLength+1)&&(this.parent.tupleLength>=k)){
tupleCount++;
返回;
}
//如果父级满足元组条件,则将其添加到m-1元组以获得m-tuple
this.parent.tupleLength=this.tupleLength+1;
//将此条件写为任何(k+1)-元组将有2个k-元组等。
如果(this.parent.tupleLength>=k)
tupleCount++;
this.parent.inTuple=true;
this.parent.setTupleLengths(k);
}
否则{
//如果父级不在元组中,则开始寻找一个新的元组,该元组从元组开始覆盖父级的祖先。
如果(!this.parent.inTuple)
this.parent.setTupleLengths(k);
}
}
}
//获取所有叶节点的步骤
静态ArrayList getLeaves(ArrayList节点){
ArrayList叶子=新的ArrayList();
用于(节点:节点){
如果(!node.hasChildren)
添加(节点);
}
还叶;
}
//为所有节点打印从每个节点开始的元组的最大长度
静态void PrintTupleLength(ArrayList节点){
用于(节点:节点)
System.out.print(“(“+node.index+”、“+node.value+”、“+node.tupleLength+”);
System.out.println();
}
公共静态void main(字符串参数[]){
字符串fileName=“/home/user/Downloads/750ac6c427-input-750a1f8.txt.clean.txt”;
文件=新文件(文件名);
试一试{
扫描仪sc=新扫描仪(文件);
//int t=sc.nextInt();
int n=sc.nextInt(),k=sc.nextInt();
ArrayList节点=新的ArrayList();
对于(int i=0;按照我理解问题描述的方式,元组中的元素不必在树中直接连接。因此X_i
必须是X_(i+1)的祖先
,但不一定是直系祖先。按照我理解您的代码的方式,您只检查元素直接连接的元组。您的代码在示例中生成了正确的答案,因为示例中不包含断开连接的元组。按照我理解问题描述的方式,元组中的元素没有在树中直接连接。因此X_i
必须是X_(i+1)的祖先
,但不一定是直系祖先。按照我对代码的理解,您只检查元素直接连接的元组。您的代码在示例中给出了正确答案,因为示例中不包含断开连接的元组。