昨晚随便玩玩搞个div3结果浪翻了……

强烈谴责D题hack数据卡常

考虑到本题中所要求的最短距离不会大于2,所以我们可以把所有结点到$1$的距离通过对$3$取模分类,考虑到直接自顶向下贪心不满足局部最优解可以推出全局最优解,所以我们可以自下向上这样可以考虑到所有条件。我们处理出一个结点$x$所有儿子$y$的取模后的距离的最小值$dis$,那么一个结点向$1$连一条边的充要条件就是:

$dis == 0 && x != 1 && fa != 1$

时间复杂度$O(n)$。

Code:

#include <cstdio>
#include <cstring>
using namespace std; const int N = 2e5 + ; int n, ans = , tot = , head[N]; struct Edge {
int to, nxt;
} e[N << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ;
char ch = ;
int op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline void chkMin(int &x, int y) {
if(y < x) x = y;
} int dfs(int x, int fat) {
int dis = ;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
chkMin(dis, dfs(y, x));
} if(dis == && x != && fat != ) ans++;
return (dis + ) % ;
} int main() {
read(n);
for(int x, y, i = ; i < n; i++) {
read(x), read(y);
add(x, y), add(y, x);
}
dfs(, );
printf("%d\n", ans);
return ;
}

最新文章

  1. LintCode Interleaving String
  2. R语言-数据高级管理
  3. 坐标系统与投影变换及在ArcGIS桌面产品中的应用
  4. 使用rownum对oracle分页
  5. whoami 和 Who am i
  6. 图文详解远程部署ASP.NET MVC 5项目
  7. 在coding上添加ssh-key
  8. 黄聪:怎么清理win7、win8更新垃圾(winsxs目录清理)
  9. Oracle函数面试题
  10. 2013 年 —— Facebook 在开源方面的工作介绍
  11. c# 迭代器 与 集合 IEnumerable.GetEnumerator 方法
  12. C中嵌入SQL
  13. [自制操作系统] BMP格式文件读取&amp;图形界面系统框架/应用接口设计
  14. linux分析日志的一些常用方法
  15. Scikit-learn:数据预处理Preprocessing data
  16. Cortext-A7_i.MX 6ULL——多模式DDR控制器(MMDC)
  17. Vue+Django2.0 restframework打造前后端分离的生鲜电商项目(2)
  18. 查找常用字符(给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。)
  19. 我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=i5j7gwrxj9x5
  20. POJ.3710.Christmas Game(博弈论 树上删边游戏 Multi-SG)

热门文章

  1. L117
  2. mysql 开发标准规范
  3. stl_hash_map.h
  4. 树莓派视频监控 —— 使用 mjpg
  5. 3.20 内存及效率的一些总结 3.21 设置竖屏 3.22 CCLOG与CCLog区别
  6. 【整理】C++中的unique函数
  7. [SP16549]QTREE6
  8. C++语言对C的增强(2)—— const增强、枚举的增强
  9. python基础准备
  10. BZOJ4154:[IPSC2015]Generating Synergy