RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-385375

EzioMercer's questions

Martin Hope
EzioMercer
Asked: 2023-04-28 19:08:53 +0000 UTC

如何找到具有一定权重的树中的路径数?

  • 5

Yandex 竞赛。问题 G. 树路径

问题链接: https: //contest.yandex.ru/contest/36783/problems/G

因为 您需要注册才能查看,所以为了方便我将文字写在这里


给定一个有根树的n顶点和一个数字X。每个顶点都有一个数字——它的权重

让我们称之为升序路径ai, p(a_i), p(p(a_i)), ...,其中p(a)是顶点的父级a。简单地说,上升路径是从某个顶点开始并向根移动(不一定到达它)的路径。路径可以由一个顶点组成

路径的权重是该路径上顶点的总权重。

查找具有权重的上升路径的数量X

限制:

  • 1 ≤ n ≤ 2 * 10^5
  • −10^9 ≤ X ≤ 10^9
  • p_i- 顶点父编号:0 ≤ p_i ≤ n − 1,或者−1如果它是根
  • w_i- 最高重量:−10^4 ≤ w_i ≤ 10^4

我是如何决定的:

我从数组(树)的末尾开始,并向总和添加权重。我开始循环,直到到达根并且总和不超过X,转到父级并重复之前的操作。循环结束后,我看和是否完全相等X,如果是,则将计数器加 1,否则什么都不做。在移动到下一个顶点之前,我重置总和

代码(交互式):

class Vertex {
    constructor(weight, parent) {
        this.weight = weight;
        this.parent = parent;
    }
}

function getNumberOfUpgoingPaths(tree, x) {
    let count = 0;
    let sum = 0;
    
    for (let i = tree.length - 1; i >= 0; --i) {
        const vertex = tree[i];
        let parentIndex = vertex.parent;
        
        sum += vertex.weight;
        
        while(parentIndex !== -1 && sum < x) {
            const parentVertex = tree[parentIndex];
            
            sum += parentVertex.weight;
            
            parentIndex = parentVertex.parent;
        }
        
        if (sum === x) ++count;
        
        sum = 0;
    }
    
    return count;
}

// Обработка входных данных

const inputs = document.querySelector('#inputs');

const startAlgorithm = () => {
  console.clear();
  
  const tree = [];
  const [[countOfvertexes, x], ...vertexes] = inputs.value.split('\n').map(x => x.split(' ').map(Number));
  
  vertexes.forEach(vertex => tree.push(new Vertex(vertex[1], vertex[0])))
  
  console.log(getNumberOfUpgoingPaths(tree, x));
}

startAlgorithm();

inputs.addEventListener('input', startAlgorithm);
textarea {
  width: 90%;
  height: 400px;
}
<textarea id="inputs">6 3
-1 1
0 1
0 1
1 1
2 2
3 1</textarea>

如果要更改数据,则输入格式如下:

  • 第一行,前2个数字为顶点数和X(处理时忽略顶点数,但需要这样写,以免破坏数据输入格式)

  • 从第二行开始,列出顶点,其中左边的数字是父节点的数字,右边的数字是顶点的权重


问题:

在某些测试中(在 Yandex 中未显示在哪个测试中),它出现故障 - 返回错误结果。在我自己提出的测试中,该算法工作正常。请帮我找到推理中的错误,或者至少是一个测试用例,所有的东西都坏了,然后我想我可以自己修复错误

алгоритм
  • 1 个回答
  • 36 Views
Martin Hope
EzioMercer
Asked: 2022-08-17 22:41:26 +0000 UTC

为什么 JS 类不使用 private 关键字

  • 0

我一直在想:为什么private在 JS 类中,他们决定使用符号而不是通常的关键字#?

到目前为止我还没有真正寻找它,但是通过简单的搜索,他们基本上说它在 JS 中被使用,以及如何使用它的#更多private示例,但他们没有回答为什么?

今天我决定还是要找到答案,因为。我敢肯定我不是唯一一个问这个问题的人。好吧,在搜索中,我遇到了专门针对该主题的完整常见问题解答(英文)

javascript классы
  • 1 个回答
  • 84 Views
Martin Hope
EzioMercer
Asked: 2022-08-15 20:56:25 +0000 UTC

TS 中的私有不会转换为 JS 中的井号 (#)

  • 1

请解释一下为什么在TS类中,private为一个字段/方法(以下简称PM)指定时,转成JS文件后,#PM前没有符号?

@AlexeyTen评论说,在 TS 中它只是纯粹的“装饰性”,你不能只是去换另一个。并在 GitHub 上留下了讨论这个问题的链接

在那里,TS 开发负责人给出了以下答案。因为 我没听懂所有的单词,所以我用谷歌翻译器和Reverso服务为自己翻译,结果是这样的:

我们目前的计划是保留当前的私人行为。

原因:

  • 我们不会无缘无故地进行重大更改,也没有任何外部因素使人们放弃编译时间private
  • 仅在编译时有非常好的隐私用例:
    • private可以从单元测试中读取字段(我意识到有些人觉得这很烦人,但它并不普遍。)
    • private字段在较低级别的脚本中具有更好的运行时性能
  • 很多人不喜欢语法#,所以为什么要强加给他们
  • 没有WeakMap(并非所有运行时都有),就没有很好的降级等价物来执行大量的隐私保护

至于变压器,为什么不呢?如果您有动力,它们很容易用正则表达式替换

经过长时间的讨论,其中一位作者开玩笑地举了这样一个例子,这让我更加困惑:

人权:

class Human {
 #rights;

 removeRights () {
   console.log("No");
 }
}

class Government {
 takeawayRights (human) {
   human.removeRights(); //No
   human.rights = undefined; //Not allowed
 }
}

人员特权:

class Human {
 private privileges;

 removePrivileges () {
   console.log("No");
 }
}

class Government {
 takeawayPrivileges (human) {
   human.removePriveleges(); //No
   human.privileges = undefined; //OK!
 }
}

事实证明,尽管是首相private,我仍然可以冷静地直接联系他,随心所欲地改变。

老实说,我在那里读了很多书,至少不完全,但是 + - 我完全理解它是如何工作private的,但我仍然不明白:为什么它会这样工作?private在 TS 中它不像#在 JS中那样工作的原因是什么

如果你能用手指解释我会很高兴:)

javascript классы
  • 1 个回答
  • 87 Views
Martin Hope
EzioMercer
Asked: 2022-08-09 16:32:07 +0000 UTC

环绕文本的边框

  • 1

是否可以在文本周围进行笔划,以便在第一种情况下它是_|- 形的,而在第二种情况下就像一个常规矩形?你不应该拘泥于文本和块的大小和位置,我只是为了清楚起见而把它放出来。明天图片可能会更低,在右边,甚至不存在 - 没关系。一行图片可以有 2 或 3 个块,预计不会更大。一般来说,有必要像文本一样,框架也环绕

.img {
  height: 100px;
  width: 200px;
  background-color: red;
  float: left;
  margin-right: 8px;
}

.text {
  margin-bottom: 8px;
}
<div class="container">
  <div class="img">IMG</div>
  <div class="text">
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus expedita, facilis, hic id ipsa ipsam laboriosam libero mollitia nam odio possimus recusandae sed similique tempore unde veniam vitae voluptatem voluptates.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus expedita, facilis, hic id ipsa ipsam laboriosam libero mollitia nam odio possimus recusandae sed similique tempore unde veniam vitae voluptatem voluptates.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus expedita, facilis, hic id ipsa ipsam laboriosam libero mollitia nam odio possimus recusandae sed similique tempore unde veniam vitae voluptatem voluptates.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus expedita, facilis, hic id ipsa ipsam laboriosam libero mollitia nam odio possimus recusandae sed similique tempore unde veniam vitae voluptatem voluptates.
  </div>
  <div class="text">
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus expedita, facilis, hic id ipsa ipsam laboriosam libero mollitia nam odio possimus recusandae sed similique tempore unde veniam vitae voluptatem voluptates.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus expedita, facilis, hic id ipsa ipsam laboriosam libero mollitia nam odio possimus recusandae sed similique tempore unde veniam vitae voluptatem voluptates.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus expedita, facilis, hic id ipsa ipsam laboriosam libero mollitia nam odio possimus recusandae sed similique tempore unde veniam vitae voluptatem voluptates.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus expedita, facilis, hic id ipsa ipsam laboriosam libero mollitia nam odio possimus recusandae sed similique tempore unde veniam vitae voluptatem voluptates.
  </div>
</div>

它应该看起来像这样: 在此处输入图像描述

非常希望解决方案是纯 CSS。如有必要,可以自行更改 HTML 结构,主显示并认为图片是与文本分开的块

UPD

在寻找解决方案时,我遇到了这样一个有趣的答案,但不幸的是,由于使用了内联元素,框架根据单词的大小非常扭曲,如果你开始缩小它,那么在某处单词和框架之间存在间隙,分为两部分:

.message {
  margin-bottom: 16px;
}

.wrapped {
   outline: 2px solid black;
}

.wrapped span {
    border: 1px solid white;
    background-color: white;
    position: relative;
    z-index: 1000;
}

.img {
  height: 100px;
  width: 200px;
  background-color: red;
  float: left;
  margin-right: 8px;
}

.img.right {
  float: right;
  margin-left: 8px;
}
<div>
  <div class="img">IMG</div>
  <div class="message">
    <span class="wrapped">
      <span>This is a potentially large paragraph of text, which may get wrapped onto several lines when displayed in the browser. I would like to be able to draw a minimal box round the span This is a potentially large paragraph of text, which may get wrapped onto several lines when displayed in the browser. I would like to be able to draw a minimal box round the span This is a potentially large paragraph of text, which may get wrapped onto several lines when displayed in the browser. I would like to be able to draw a minimal box round the span This is a potentially large paragraph of text, which may get wrapped onto several lines when displayed in the browser. I would like to be able to draw a minimal box round the span This is a potentially large paragraph of text, which may get wrapped onto several lines when displayed in the browser. I would like to be able to draw a minimal box round the span
      </span>
    </span>
  </div>
  <div class="img right">IMG</div>
  <div class="message">
    <span class="wrapped">
      <span>This is a potentially large paragraph of text, which may get wrapped onto several lines when displayed in the browser. I would like to be able to draw a minimal box round the span This is a potentially large paragraph of text, which may get wrapped onto several lines when displayed in the browser. I would like to be able to draw a minimal box round the span This is a potentially large paragraph of text, which may get wrapped onto several lines when displayed in the browser. I would like to be able to draw a minimal box round the span This is a potentially large paragraph of text, which may get wrapped onto several lines when displayed in the browser. I would like to be able to draw a minimal box round the span This is a potentially large paragraph of text, which may get wrapped onto several lines when displayed in the browser. I would like to be able to draw a minimal box round the span
      </span>
    </span>
  <div>
</div>

javascript css
  • 2 个回答
  • 121 Views
Martin Hope
EzioMercer
Asked: 2022-07-31 15:55:01 +0000 UTC

git列出所有子分支

  • 1

我需要遍历从分支分支的所有分支X并将主分支合并到每个分支中

在搜索信息时,我找到了完全相反问题的答案。有一个想法是为了在命令中git branch -v显示所有分支并查看谁具有相同的哈希值。但事实是这个项目非常大,100500个分支走遍所有分支不是一个选择。

另外,我可以说我为团队工作Fedora Linux 35,bash所以如果它们适用于这里,我也会很高兴。没有 UI 是非常可取的,即。您需要一个纯控制台命令的解决方案,当然,如果没有其他方法,可以忽略此规则))

git git-branch
  • 1 个回答
  • 93 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5