Codeforces 1039D You Are Given a Tree (看题解)
You Are Given a Tree
感觉是个套路。。 怎么好像我没怎么见过啊。
k * t <= n 类似于这种, 对所有 k 求满足条件 t 的最大值, 那么答案不同的数量只有根号个。
如果随 k 的变化单调的话就可以二分优化啦。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0);using namespace std;const int N = 1e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1);template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}const int B = 1000;int n, k, cur, ans[N], fa[N]; vector<int> G[N]; int id[N], nid; int dp[N]; int mx[N][2];int calc(int k) {if(~ans[k]) return ans[k];for(int o = 1; o <= n; o++) {int u = id[o];dp[u] = mx[u][0] = mx[u][1] = 0;for (auto& v : G[u]) {if(v == fa[u]) continue;dp[u] += dp[v];if(mx[v][0] >= mx[u][0]) mx[u][1] = mx[u][0], mx[u][0] = mx[v][0];else if(mx[v][0] > mx[u][1]) mx[u][1] = mx[v][0];}if(mx[u][0] + mx[u][1] + 1 >= k) {dp[u]++;mx[u][0] = mx[u][1] = 0;} else {mx[u][0]++;mx[u][1]++;}}return ans[k] = dp[1]; }void dfs(int u, int f) {fa[u] = f;for(auto& v : G[u]) {if(v == f) continue;dfs(v, u);}id[++nid] = u; }void solve(int l, int r) {if(l > r) return;if(calc(l) == calc(r)) {for(int i = l + 1; i < r; i++) ans[i] = ans[l];return;}int mid = l + r >> 1;solve(l, mid); solve(mid + 1, r); }int main() {memset(ans, -1, sizeof(ans));scanf("%d", &n);for(int i = 1; i < n; i++) {int u, v; scanf("%d%d", &u, &v);G[u].push_back(v);G[v].push_back(u);}dfs(1, 0);solve(1, n);for(int i = 1; i <= n; i++) printf("%d\n", ans[i]);return 0; }/* */
转载于:https://www.cnblogs.com/CJLHY/p/10986732.html
Codeforces 1039D You Are Given a Tree (看题解)相关推荐
- Codeforces 773D Perishable Roads 最短路 (看题解)
Perishable Roads 智商题, 不会啊.. 贴个官方题解 https://codeforces.com/blog/entry/51883 #include<bits/stdc++.h ...
- Codeforces Round #646 (Div. 2)E. Tree Shuffling 题解(dfs)
题目链接 题目大意 给你一颗树,每一个节点有一个a[i],b[i],c[i]值,你要把b[i]变成c[i],b[i]和c[i]为[0,1],你操作的方法是,选择一个节点x,选择他的k个子树,然后进行交 ...
- Codeforces 311D Interval Cubing 数学 + 线段树 (看题解)
Interval Cubing 这种数学题谁顶得住啊. 因为 (3 ^ 48) % (mod - 1)为 1 , 所以48个一个循环节, 用线段树直接维护. #include<bits/stdc ...
- Codeforces 200A Cinema 并查集 + 思维 (看题解)
Cinema 感觉这个题好神啊... 首先如果 n 比 m 大, 我们先旋转90度. 我们要加入一个(x, y)的时候, 我们枚举答案所在的行离 x 的距离 g , 然后对于x + g 行来说 我们找 ...
- Educational Codeforces Round 104 (Rated for Div. 2)A-E题解
Educational Codeforces Round 104 (Rated for Div. 2)A-E题解 比赛链接:https://codeforces.ml/contest/1487 A题 ...
- 洛谷P2619 [国家集训队]Tree I 题解
洛谷P2619 [国家集训队]Tree I 题解 题目链接:P2619 [国家集训队]Tree I 题意: 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有 need\text{n ...
- Educational Codeforces Round 119 (Rated for Div. 2) EFG 题解
Solution 赛时切了 ABCDEG,不会 F. E 考虑使用若干个集合 S i S_i Si 维护各个数出现的位置,那么两个操作分别可以被抽象为: 在一个集合中加入一个数. 将集合 S x S ...
- Codeforces 600E. Lomsat gelral(Dsu on tree学习)
题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...
- Codeforces Round #316 (Div. 2) D. Tree Requests dfs序
题目链接: 题目 D. Tree Requests time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 Rom ...
- Codeforces 741 D - Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
D - Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 思路: 树上启发式合并 从根节点出发到每个位置的每个字符的奇偶性记为每个位 ...
最新文章
- 万字干货:如何从零开始构建企业级推荐系统?
- Python进阶【第五篇】函数式编程及某些特殊函数
- asp php java_ASP\JSP\PHP相比各有什么优缺点?
- 用了Dapper之后通篇还是SqlConnection,真的看不下去了
- 在服务器端生成Excel文件然后从服务器下载的本地的代码
- mavros 基于体轴坐标系下的无人机行人跟踪
- linux开启和关闭防火墙
- linux cpu监控方案,Linux性能优化和监控系列(二)分析CPU性能
- java引用队列_java的强引用、软引用、弱引用、幻象引用,引用队列总结
- 单片机定时器之改良版:时间轮定时器
- @RequestMapping测试各种访问方式
- 【气动学】基于matlab GUI外弹道仿真系统【含Matlab源码 1044期】
- 【不懂就问】ROST EA情感分析软件怎么操作
- 基于深度学习的视频修复算法
- 高通8155源码下载与Android源码编译
- 文通电脑版车牌识别软件,让违章驾车无处可躲
- ES9,ES10,ES11知识点
- ElasticSearch系列——Kibana,核心概念
- Python之文件操作(常用操作)
- Qt图形视图框架:视图增加标尺