首页 > 最大最小路
头像 Lambda_L
发表于 2026-03-31 01:16:14
思路正难则反我们定义一条路径是 “好路径”,当且仅当满足两个条件:条件 A:路径上点权的最小值 ≤ a条件 B:路径上点权的最大值 ≥ b直接求同时满足 A 和 B 的路径数比较困难。我们可以通过求它的反面来解决。根据容斥原理:满足 A 且满足 B 的路径数 = 总路径数 - 不满足 A 的路径数 展开全文
头像 AliLexiWalker
发表于 2026-03-31 08:51:39
好路径的条件是“既要有小于等于 a 的点,又要有大于等于 b 的点”,那就把不好的先删掉:要么整条路都 >a,要么整条路都 <b。 所以答案=总路径数-两种坏路径,再把重复减掉的“整条路都在 (a,b) 里”加回去;而“某种条件下的路径数”在树上就是把符合条件的点分连通块,每块贡献 sz 展开全文
头像 小男娘
发表于 2026-03-31 00:35:49
进行一个树形 DP记录向下路径是否包含共四种情况,DP 合并过程中统计答案 #include <array> #include <iostream> #include <vector> using namespace std; using ll = long l 展开全文
头像 牛客365435906号
发表于 2025-02-28 23:40:03
#include <iostream> #include <vector> #include <functional> using namespace std; using ll = long long; class UnionFind { public: 展开全文
头像 腌萝卜干
发表于 2026-03-31 12:54:07
容斥原理 + 并查集维护 #include <bits/stdc++.h> #define x first #define y second #define all(x) x.begin(), x.end() #define vec1(T, name, n, val) vector&l 展开全文
头像 olone
发表于 2026-03-31 17:08:39
import java.util.*; public class Main{ static Scanner in = new Scanner(System.in); static int[] fa = new int[500005]; static int[] siz = 展开全文
头像 阿彪b
发表于 2025-12-18 23:09:25
#include <bits/stdc++.h> using namespace std; struct node{ long long w,fu; }; long long n,aa,bb; int find(vector<struct node> &a,i 展开全文
头像 chenlan114
发表于 2026-03-31 17:11:18
#include<bits/stdc++.h> using namespace std; using ll=long long; struct UF{ UF* root; ll rank=0; ll cnt=1; }; UF* Root(UF* a){ 展开全文
头像 牛客286928113号
发表于 2025-08-26 05:25:54
#include<iostream> #include<vector> #include<algorithm> #include<map> #include<string> #include<cmath> #include< 展开全文
头像 此在Dasein
发表于 2026-03-31 04:11:17
1. 问题建模 给定一棵树 ,对于每一条简单路径 ,定义: 条件 : 条件 : 题目要求计算满足 的路径数量。 2. 算法:容斥原理 直接统计同时满足最小值和最大值界限的路径较为复杂。根据容斥原理,我们可以通过补集来简化计算。 令全集 为树上所有简单路径的集合。则: 根据容斥公式: 其中 展开全文

等你来战

查看全部