CodeForces 1029E div3
第一道场上自己做出来的E题...虽然是div3,而且是原题...
当时做完ABC,D题没有思路就去怼E了,然后发现貌似原题?
事实上就是原题...
给个原题链接...
第二个我还写过题解...题解链接
其实就是一个贪心,首先深度在2以内的就不用管它了
对于深度大于2的点,我们显然只能拉一条边在他那里或者他的父亲那里,同样的,显然我们把那条边放在他父亲那里更划算,因为可以到达更多的点
所以一遍dfs处理出深度,然后按深度放入大根堆里面,每次取出一个点,对它的父亲的周围节点染色,遇到被染色过的直接continue掉就好
然后我当时没有给该节点的祖父染色...事实上是要染的...然后就WA8了5次,罚时爆炸...
复杂度$O(nlogn)$
#include <bits/stdc++.h>
#define ll long long
#define inf 1<<30
#define il inline
#define in1(a) read(a)
#define in2(a,b) in1(a),in1(b)
#define in3(a,b,c) in2(a,b),in1(c)
#define in4(a,b,c,d) in2(a,b),in2(c,d)
il void readl(ll &x){
x=;ll f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-f;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
x*=f;
}
il void read(int &x){
x=;int f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-f;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
x*=f;
}
using namespace std;
/*===================Header Template=====================*/
#define N 1000010
int n,head[N],cnt;
struct edge{
int to,nxt;
}e[N];
struct node {
int id,dep,fa;
bool operator < (const node &x)const {
return x.dep>dep;
}
}a[N];
void ins(int u,int v){
e[++cnt].to=v;e[cnt].nxt=head[u];head[u]=cnt;
e[++cnt].to=u;e[cnt].nxt=head[v];head[v]=cnt;
}
void dfs(int x,int f,int dep){
a[x].fa=f;
a[x].id=x;
a[x].dep=dep;
for(int i=head[x];i;i=e[i].nxt){
if(e[i].to==f)continue;
dfs(e[i].to,x,dep+);
}
}
bool vis[N];
priority_queue<node>q;
int main(){
in1(n);
for(int i=;i<n;i++){
int x,y;
in2(x,y);
ins(x,y);
}
dfs(,,);
for(int i=;i<=n;i++){
if(a[i].dep>)q.push(a[i]);
else vis[i]=;
}
int ans=;
while(!q.empty()){
node t=q.top();q.pop();
if(vis[t.id])continue;
ans++;
int f=t.fa;
vis[t.id]=vis[f]=;
for(int i=head[f];i;i=e[i].nxt){
vis[e[i].to]=;
}
}
printf("%d\n",ans);
return ;
}
CodeForces 1029E div3的更多相关文章
- Codeforces #550 (Div3) - G.Two Merged Sequences(dp / 贪心)
Problem Codeforces #550 (Div3) - G.Two Merged Sequences Time Limit: 2000 mSec Problem Description T ...
- codeforces #579(div3)
codeforces #579(div3) A. Circle of Students 题意: 给定一个n个学生的编号,学生编号1~n,如果他们能够在不改变顺序的情况下按编号(无论是正序还是逆序,但不 ...
- [Codeforces #615 div3]1294E Obtain a Permutation
Before the Beginniing 本文为 Clouder 原创文章,原文链接为Click,转载时请将本段放在文章开头显眼处.如进行了二次创作,请明确标明. 由本人转载于博客园. 题意分析 C ...
- Codeforces #624 div3 C
You want to perform the combo on your opponent in one popular fighting game. The combo is the string ...
- codeforces 1029E Tree with Small Distances【思维+贪心】 【非原创】
题目:戳这里 学习博客:戳这里 题意:给一个树加最少的边,使得1到所有点的距离小于等于2. 解题思路:分析样例3可以看出,如果一个点到1的距离大于2,那么建立1到该点的父亲节点的边将比直接与该点建边更 ...
- Twist the Permutation 数列的轮换题 Codeforces 776 div3
这是一道比较经典的将数列中的数字轮换的题目,我们先看题干: 题干分析:先浅浅地分析一下题目是要我们干什么,我们会默认有一个已经升序排序地1~n的排列,然后我们会给定一个新排列是在原有排列的基础上进行o ...
- 记一次神奇的codeforces
今天有一场codeforces的div3,时间挺合适,于是就想打.结果发现rating超过1600就不能报名.虽然shzr好久不打CF了而且很菜,但是毕竟还是到了1600的,于是和ZUTTER_一起用 ...
- CodeForces Round #527 (Div3) B. Teams Forming
http://codeforces.com/contest/1092/problem/B There are nn students in a university. The number of st ...
- 【赛时总结】◇赛时·V◇ Codeforces Round #486 Div3
◇赛时·V◇ Codeforces Round #486 Div3 又是一场历史悠久的比赛,老师拉着我回来考古了……为了不抢了后面一些同学的排名,我没有做A题 ◆ 题目&解析 [B题]Subs ...
随机推荐
- application实例
application详解及实例 application对象用来在多个程序或者是多个用户之间共享数据,用户使用的所有application对象都是一样的,这与session对象不同.服务器一旦启动,就 ...
- POJ2488:A Knight's Journey(dfs)
http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...
- 小希的迷宫(hdu1272 并查集)
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=259#problem/D Description 上次Gardon的迷宫城堡小希 ...
- mybatis中获取参数
1.${parameter}方式: parameter是数字时用:模糊查询%${parameter}%时用. 例:select * from account where userId = ${para ...
- R实现的最小二乘lsfit函数学习
1.源码 function (x, y, wt = NULL, intercept = TRUE, tolerance = 1e-, yname = NULL) { x <- as.matrix ...
- (C#) SQLite数据库连接字符串
最常用的:Data Source=filename;Version=3; 自增主键: Create test1( [id] integer PRIMARY KEY AUTOINCREMENT ,[n ...
- numpy中loadtxt 的用法
numpy中有两个函数可以用来读取文件,主要是txt文件, 下面主要来介绍这两个函数的用法 第一个是loadtxt, 其一般用法为 numpy.loadtxt(fname, dtype=, comme ...
- centos6更改密码
创建新用户 创建一个用户名为:zhangbiao [root@localhost ~]# adduser zhangbiao 为这个用户初始化密码,linux会判断密码复杂度,不过可以强行忽略: [r ...
- EntityFramework包含作用
System.Data.Entity.Infrastructure.DbQuery的引用需要加入上面那个包
- pbs 作业管理命令
PBS 提供4 条命令用于作业管理. (1) qsub 命令—用于提交作业脚本 命令格式: qsub [-a date_time] [-c interval] [-C directive_prefix ...