http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4045

题意:给一棵树。这棵树有n个节点,问你这个图能不能分成k个分块。这个分块内所有的节点都至少与一个该块中的其他节点有一条边相连。

题目分析:由于是无根树,所以可以随便选取一个结点作为根节点,然后dfs往下搜索,搜索过程不容易划分哪些结点和哪些结点是一块,而利用搜索到叶子结点之后往上回溯的过程,每累积n/k个点就划分一次,就很容易划分出结点块.这道题也主要是利用了dfs的回溯过程.(很多不容易正向解决的问题回溯起来很容易得到解决)

 #include <iostream>
#include <iomanip>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
#include <set>
#include<cstdio>
#include <cmath>
#include<cstring>
using namespace std;
typedef long long ll;
bool vis[];
int dp[];
vector<int>G[];
int dfs(int u,int k){
vis[u]=;
int t=;
for(int i=;i<G[u].size();i++){
if(!vis[G[u][i]]){
t+=dfs(G[u][i],k);
}
}
dp[u]=t;
if(dp[u]==k){
dp[u]=;
}
return dp[u];
}
void dfs_ans(int u,int fa){
if(dp[u]==&&u!=fa)return;
vis[u]=;
if(u!=fa){
printf(" %d",u);
}
for(int i=;i<G[u].size();i++){
if(!vis[G[u][i]]){
dfs_ans(G[u][i],u);
}
}
}
int main(){
//freopen("in.txt","r",stdin);
ios_base::sync_with_stdio();
cin.tie(NULL);
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
// cout <<n<<k<<endl;
for(int i=;i<=n;i++)G[i].clear();
memset(vis,,sizeof(vis));
memset(dp,,sizeof(dp));
for(int i=;i<n-;i++){
int a,b;
cin>>a>>b;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(,k);
memset(vis,,sizeof(vis));
//for(int i=0i<n;i++){if(dp[i]==0){dp[i]=1;printf("%d",i);dfs(i,k,i);dp[i]=0;}
if(dp[]==){
printf("YES\n");
for(int i=;i<=n;i++){
if(dp[i]==){
printf("%d",i);
dfs_ans(i,i);
printf("\n");
} }
}
else{
printf("NO\n");
}
}
return ;
}

[zoj4045][思维+dfs]的更多相关文章

  1. CF D. Fair(思维+DFS)

    http://codeforces.com/contest/987/problem/D 题目大概: 给出一个n个城镇m条边的图,给出每个城镇拥有的特产(可能多个城镇有相同特产).有k种不同特产. 要求 ...

  2. ZOJ 4124 拓扑排序+思维dfs

    ZOJ - 4124Median 题目大意:有n个元素,给出m对a>b的关系,问哪个元素可能是第(n+1)/2个元素,可能的元素位置相应输出1,反之输出0 省赛都过去两周了,现在才补这题,这题感 ...

  3. Codeforces542E Playing on Graph 思维+DFS+BFS

    解法参考https://www.cnblogs.com/BearChild/p/7683114.html这位大佬的,这位大佬讲得很好了. 这道题还是有一定的思维的. 直接贴代码: #include&l ...

  4. Trips CodeForces - 1037E(思维dfs)

    题意: 就是几个人去旅游,组队的条件是对于某个队员 队里至少有两个是他的朋友,每天早晨都会有一对新人成为朋友 解析: 用set标记互为朋友 a[i] b[i] 表示在第i天早晨 u和v成为朋友 先求最 ...

  5. #537 (Div. 2) Creative Snap (思维+dfs)

    https://codeforces.com/contest/1111/problem/C 横坐标1..2^n对应着2^n个复仇者的基地,上面有k个复仇者(位置依次给出).你是灭霸你要用以下方法消灭这 ...

  6. 【2018 ICPC亚洲区域赛沈阳站 L】Tree(思维+dfs)

    Problem Description Consider a un-rooted tree T which is not the biological significance of tree or ...

  7. hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs

    题目传送门 题目描述:给出一颗树,每条边都有权值,然后列出一个n的全排列,对于所有的全排列,比如1 2 3 4这样一个排列,要算出1到2的树上距离加2到3的树上距离加3到4的树上距离,这个和就是一个排 ...

  8. HDU-4705 Y(思维+dfs树)

    Input 4 1 2 1 3 1 4 Output 1 题意:给你一颗树,选择一个三个点构成的集合,使得这三个点不在一条直线上(意思就是 从一个点出发,用一条不回头的线不能将这三个点连起来)问一共有 ...

  9. CodeM 美团资格赛 思维 dfs

    链接:https://www.nowcoder.com/acm/contest/138/C来源:牛客网 世界杯就要开始啦!真真正正的战斗从淘汰赛开始,现在我们给出球队之间的胜负概率,来预测每支球队夺冠 ...

随机推荐

  1. 机器学习---笔记----numpy和math包中的常用函数

    本文只是简单罗列一下再机器学习过程中遇到的常用的数学函数. 1. math.fabs(x): 返回x的绝对值.同numpy. >>> import numpy >>> ...

  2. ftp上传操作

    采用 :FtpWebRequest 进行操作ftp. 1.代码上传文件必须是被动模式  UsePassive=false 2.最好采用二进制传输 UseBinary=true 注意缓冲区大小,还有注意 ...

  3. 菜鸟翻译:国外的一个关于.net core的学习系列 第一天(安装并运行.NET core 到windox系统里面)

    原文地址: Day 1 - Installing and Running .NET Core on a Windows Box 免责声明:我不是.NET Core 的团队成员.我使用的工具是公开可用的 ...

  4. linux下Mysql多实例实现

    什么是MySQL多实例 MySQL多实例就是在一台机器上开启多个不同的服务端口(如:3306,3307),运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的服务:: My ...

  5. Linux文件系统命令 cp

    命令名:cp 功能:拷贝文件,把一个文件的内容拷贝到另外一个文件中去. eg: cp source_file dist_file renjg@renjg-HP-Compaq-Pro--MT:~$ cp ...

  6. C#窗体换肤

    Form1.cs using System;using System.Collections.Generic;using System.ComponentModel;using System.Data ...

  7. winform 点击控件拖动窗体

    private Point mPoint = new Point(); private void 选择控件_MouseDown(object sender, MouseEventArgs e) { m ...

  8. Python 依赖关系

    class Person: def play(self, tools): # 通过参数的传递把另外一个类的对象传递进来 tools.run() print("很开心, 我能玩儿游戏了&quo ...

  9. foreman ubuntu16快速安装

    Quickstart Guide The Foreman installer is a collection of Puppet modules that installs everything re ...

  10. [转]C++智能指针简单剖析

    C++智能指针简单剖析  https://www.cnblogs.com/lanxuezaipiao/p/4132096.html 导读 最近在补看<C++ Primer Plus>第六版 ...