【2018 ICPC亚洲区域赛沈阳站 L】Tree(思维+dfs)
Problem Description
Consider a un-rooted tree T which is not the biological significance of tree or plant, but a tree as an undirected graph in graph theory with n nodes, labelled from 1 to n. If you cannot understand the concept of a tree here, please omit this problem.
Now we decide to colour its nodes with k distinct colours, labelled from 1 to k. Then for each colour i = 1, 2, · · · , k, define Ei as the minimum subset of edges connecting all nodes coloured by i. If there is no node of the tree coloured by a specified colour i, Ei will be empty.
Try to decide a colour scheme to maximize the size of E1 ∩ E2 · · · ∩ Ek, and output its size.
Input
The first line of input contains an integer T (1 ≤ T ≤ 1000), indicating the total number of test cases.
For each case, the first line contains two positive integers n which is the size of the tree and k (k ≤ 500) which is the number of colours. Each of the following n - 1 lines contains two integers x and y describing an edge between them. We are sure that the given graph is a tree.
The summation of n in input is smaller than or equal to 200000.
Output
For each test case, output the maximum size of E1 ∩ E1 ... ∩ Ek.
Sample Input
Sample Output
1 0 1
题意:
有一棵N个点的无根树,以及K种颜色,先用这K种颜色染色。将连通相同颜色的点所需要的最少数量的边作为一个集合(其实也就类似于一种最小生成树),然后将所有颜色所形成的集合树做一个交集,现在要找到一种染色方案,使得这个交集最大。若不使用某种颜色,那么该颜色的边集为空集。
思路:
题意真的好难读懂呀!看了半天也没思路!我一开始往点的方向思考,实则不行,必须要往边的方向想才对。
在给某一种颜色染色时,最好是只染最外边的两个点,因为要省出一些点给其他颜色,有空集颜色出现肯定是不好的情况。对于每一条边,它的两点分别延伸出去两棵子树,所以只要看每一条边所对应的两棵子树大小,是否都大于等于K即可。若满足条件,则必在交集之中,最后深搜遍历就好了!
#include<bits/stdc++.h>
#define MAX 200005
using namespace std;
typedef long long ll;
int n,k,sum,num[MAX];
vector<int>ve[MAX];
void dfs(int u,int pre) //pre 表示上一个点
{
for(int i=;i<ve[u].size();i++)
{
int v=ve[u][i];
if(v!=pre) //为了使dfs不往回走
{
dfs(v,u); //先遍历至叶子结点
num[u]+=num[v]; //一层层结点数往上加
if(num[v]>=k&&n-num[v]>=k) //从尾部开始判断!!
sum++;
}
}
}
int main()
{
int i,T;
ios::sync_with_stdio(false);
cin>>T;
while(T--)
{
cin>>n>>k;
for(i=;i<=n;i++)
{
ve[i].clear();
num[i]=;
}
for(i=;i<n;i++)
{
int x,y;
cin>>x>>y;
ve[x].push_back(y);
ve[y].push_back(x);
}
sum=;
dfs(,-);
cout<<sum<<endl;
}
return ;
}
【2018 ICPC亚洲区域赛沈阳站 L】Tree(思维+dfs)的更多相关文章
- 【2018 ICPC亚洲区域赛徐州站 A】Rikka with Minimum Spanning Trees(求最小生成树个数与总权值的乘积)
Hello everyone! I am your old friend Rikka. Welcome to Xuzhou. This is the first problem, which is a ...
- 【2018 ICPC亚洲区域赛南京站 A】Adrien and Austin(博弈)
题意: 有一排n个石子(注意n可以为0),每次可以取1~K个连续的石子,Adrien先手,Austin后手,若谁不能取则谁输. 思路: (1) n为0时的情况进行特判,后手必胜. (2) 当k=1时, ...
- 【2017 ICPC亚洲区域赛沈阳站 K】Rabbits(思维)
Problem Description Here N (N ≥ 3) rabbits are playing by the river. They are playing on a number li ...
- ACM-ICPC 2016亚洲区域赛(沈阳站)游记(滚粗记)
首发于QQ空间和知乎,我在这里也更一下. 前言 以前高中搞竞赛的时候,经常看到神犇出去比赛或者训练之后写游记什么的,感觉萌萌哒.但是由于太弱,就没什么心情好写.现在虽然还是很弱,但是抱着享受的心情 ...
- 2014ACM/ICPC亚洲区域赛牡丹江站汇总
球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...
- 【2013 ICPC亚洲区域赛成都站 F】Fibonacci Tree(最小生成树+思维)
Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do s ...
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-A ( ZOJ 3819 ) Average Score
Average Score Time Limit: 2 Seconds Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( ZOJ 3829 ) Known Notation
Known Notation Time Limit: 2 Seconds Memory Limit: 65536 KB Do you know reverse Polish notation ...
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy
Information Entropy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Information ...
随机推荐
- [LeetCode]26. Remove Duplicates from Sorted Array删除排序数组中的重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- WinSock2 API
title: WinSock2 API tags: [WinSock, 网络编程, WinSock2.0 API, 动态加载, WinSock 异步函数] date: 2018-07-21 10:36 ...
- cf1064E. Dwarves, Hats and Extrasensory Abilities(二分 交互)
题意 题目链接 \(n\)次操作,每次你给出一个点的坐标,系统会返回该点的颜色(黑 / 白),程序最后输出一条直线把所有黑点和白点分隔开 Sol 一个很直观的想法:首先询问\((dx, 0)\),然后 ...
- SharePoint 2013 - Designer Workflow
另一篇文章 SharePoint Designer - Workflow 1. 可以定义每个Stage的名称,并将Stage名称显示在工作流状态字段,相比于SP2010时仅有的in progress ...
- 安装lombok(eclipse)
下载 lombok.jar (https://projectlombok.org/download.html) 将 lombok.jar 放在eclipse安装目录下,和 eclipse.ini 文件 ...
- C语言买卖股票问题
遇到个简单的算法题,没有当场解出来,以后可以写伪代码表达思路. 数组中保存每天的股票价值,求买入卖出的时间和最大利润,比较好的解法如下: 伪代码: begin start day = 0; end d ...
- Python装饰器AOP 不定长参数 鸭子类型 重载(三)
1 可变长参数与关键字参数 *args代表任意长度可变参数 **kwargs代表关键字参数 用*args和**kwargs只是为了方便并没有强制使用它们. 缺省参数即是调用该函数时,缺省参数的值若未被 ...
- 【Leetcode】【Easy】Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- 启动eclipse出现“Error opening registry key 'software\Javasoft\Java Runtime Environment'”
启动eclipse出现“Error opening registry key 'software\Javasoft\Java Runtime Environment'”,“java was start ...
- NO.006-2018.02.11《卜算子·我住长江头》宋代:李之仪
卜算子·我住长江头_古诗文网(bǔ) 卜算子·我住长江头 宋代:李之仪 我住长江头,君住长江尾.日日思君不见君,共饮长江水. 我居住在长江上游,你居住在长江下游. 天天想念你却见不到你,共同喝着长江的 ...