HDU-4714-贪心
Tree2cycle
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 3091 Accepted Submission(s): 719
tree with N nodes and N-1 edges is given. To connect or disconnect one
edge, we need 1 unit of cost respectively. The nodes are labeled from 1
to N. Your job is to transform the tree to a cycle(without superfluous
edges) using minimal cost.
A cycle of n nodes is defined as
follows: (1)a graph with n nodes and n edges (2)the degree of every node
is 2 (3) each node can reach every other node with these N edges.
In
the first line of each test case, there is a single integer N(
3<=N<=1000000 ) - the number of nodes in the tree. The following
N-1 lines describe the N-1 edges of the tree. Each line has a pair of
integer U, V ( 1<=U,V<=N ), describing a bidirectional edge (U,
V).
4
1 2
2 3
2 4
In the sample above, you can disconnect (2,4) and then connect (1, 4) and
(3, 4), and the total cost is 3.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#pragma comment(linker, "/STACK:102400000,102400000")
int first[],tot;
struct Edge
{
int v,next;
}e[];
void add(int u,int v){
e[tot].v=v;
e[tot].next=first[u];
first[u]=tot++;
}
int aaa;
int dfs(int u,int fa)
{
int s=;
for(int i=first[u];~i;i=e[i].next){
int v=e[i].v;
if(v==fa) continue;
s+=dfs(v,u);
}
if(s>){
aaa+=s-;
if(u==) aaa--;
return ;
}
else {
return ;
}
}
int main()
{
int n,i,j,k,u,v;
int t;
cin>>t;
while(t--){aaa=;
memset(first,-,sizeof(first));
tot=;
scanf("%d",&n);
for(i=;i<n;++i){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(,);
cout<<aaa+aaa+<<endl;
}
return ;
}
HDU-4714-贪心的更多相关文章
- HDU 4714 Tree2cycle:贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意: 给你一棵树,添加和删除一条边的代价都是1.问你将这棵树变成一个环的最小代价. 题解: 贪 ...
- Hdu 5289-Assignment 贪心,ST表
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 4714 Tree2cycle DP 2013杭电热身赛 1009
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 Tree2cycle Time Limit: 15000/8000 MS (Java/Other ...
- hdu 4803 贪心/思维题
http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么? G++ AC C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...
- hdu 4714 树+DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 本来想直接求树的直径,再得出答案,后来发现是错的. 思路:任选一个点进行DFS,对于一棵以点u为 ...
- hdu 1735(贪心) 统计字数
戳我穿越:http://acm.hdu.edu.cn/showproblem.php?pid=1735 对于贪心,二分,枚举等基础一定要掌握的很牢,要一步一个脚印走踏实 这是道贪心的题目,要有贪心的意 ...
- hdu 4974 贪心
http://acm.hdu.edu.cn/showproblem.php?pid=4974 n个人进行选秀,有一个人做裁判,每次有两人进行对决,裁判可以选择为两人打分,可以同时加上1分,或者单独为一 ...
- hdu 4982 贪心构造序列
http://acm.hdu.edu.cn/showproblem.php?pid=4982 给定n和k,求一个包含k个不相同正整数的集合,要求元素之和为n,并且其中k-1的元素的和为完全平方数 枚举 ...
- HDU 4714 Treecycle(树形dp)
http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意:给出一棵树,删除一条边和添加一条边的代价都是1,现在要把这棵树变成环,求需要花的最小代价. 思路: ...
- HDU 2307 贪心之活动安排问题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2037 今年暑假不AC Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- 模块讲解----json模块(跨平台的序列化与反序列化)
一.json的特点 1.只能处理简单的可序列化的对象:(字典,列表,元祖) 2.json支持不同语言之间的数据交互:(python - go,python - java) 二.使用场景 1.玩 ...
- 工作笔记——限定input上传文件对话框中能选取的文件的格式
原文:http://www.dengzhr.com/frontend/1059 input[file]标签的accept属性可用于指定上传文件的 MIME类型 . 例如,想要实现默认上传图片文件的代码 ...
- android 弹出带按钮的对话框
package com.example.helloworld; import android.os.Bundle;import android.app.Activity;import android. ...
- jQuery动态效果学习笔记
资料来源 W3Cschool 1.元素的显示与隐藏 1.1显示元素show() 语法 $(selector).show(speed,callback); 显示已经设置隐藏的元素 1.2隐藏元素hide ...
- Log4net 自定义字段 写入Oracle 使用ODP.NET Managed驱动
一.环境说明: 开发工具:vs2010 ,数据库:oracle 11g ,版本:log4net的目前最新版本1.2.13.0 : Oracle.ManagedDataAccess.dll ...
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) D - Dynamic Problem Scoring
地址:http://codeforces.com/contest/807/problem/D 题目: D. Dynamic Problem Scoring time limit per test 2 ...
- Ubuntu16.04安装MySQLdb
buntu 系统下进行的操作 首先安装了pip工具 1 sudo apt-get install python-pip 然后使用 1 sudo pip install mysql-python 安 ...
- 如何交叉编译Python到ARM-Linux平台(转)
源: 如何交叉编译Python到ARM-Linux平台
- 20145314郑凯杰《信息安全系统设计基础》第9周学习总结 PART B
20145314郑凯杰<信息安全系统设计基础>第9周学习总结 PART B 明确教材学习目标 注意每个系统调用的参数.返回值,会查帮助文档 阅读教材,完成课后练习(书中有参考答案),考核: ...
- LabVIEW之Vision基础 (一)之软件
一.软件准备 NI LabVIEW软件视觉开发必备软件 1.开发平台:LabVIEW 2015Chinese 32位中文版 链接:http://pan.baidu.com/s/1eRGmFVc 2.N ...