【题目链接】

点击打开链接

【算法】

这题描述有些繁琐,先简化一下题意 : 对于一棵无根树,删除一个节点,使得其余的联通块中,最大的联通块最小

那么,这题就很好做了

对这棵树进行一遍DFS,求出每个节点为根的子树的大小(记为size),再求出删除节点后,子树中最大的联通块的大小(记为mx)

那么,删除一个点后(设这个点为x),最大的联通块就是max{n - size[x],mx[x]},求最小的即可

【代码】

此题用std :: vector存储邻接表会离奇Runtime Error,令笔者不解

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 100010
const int INF = 2e9; int i,n,u,v,ans = INF,tmp,tot;
vector< int > res;
int Head[MAXN],Next[MAXN],U[MAXN],V[MAXN],fa[MAXN],size[MAXN],mx[MAXN]; inline void addedge(int u,int v)
{
tot++;
U[tot] = u; V[tot] = v;
Next[tot] = Head[u];
Head[u] = tot;
}
inline void dfs(int x)
{
int i,y;
size[x] = ;
for (i = Head[x]; i; i = Next[i])
{
y = V[i];
if (fa[x] != y)
{
fa[y] = x;
dfs(y);
size[x] += size[y];
mx[x] = max(mx[x],size[y]);
}
}
} int main()
{ scanf("%d",&n);
for (i = ; i < n; i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
} dfs();
for (i = ; i <= n; i++)
{
tmp = max(mx[i],n-size[i]);
if (tmp < ans)
{
ans = tmp;
res.clear();
res.push_back(i);
} else if (tmp == ans) res.push_back(i);
} for (i = ; i < res.size() - ; i++) printf("%d ",res[i]);
printf("%d\n",res[res.size()-]); return ; }

【POJ 3107】 Godfather的更多相关文章

  1. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  2. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  3. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  4. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  5. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  6. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  7. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  8. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

  9. 【POJ 1125】Stockbroker Grapevine

    id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...

随机推荐

  1. BZOJ 1303: [CQOI2009]中位数图 【水题】

    给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. Input 第一行为两个正整数n和b ,第二行为1~n 的排列. Out ...

  2. PMU 简介

    目录 1:PMIC2:Battery管理3:功耗4:常见问题5:参考文献 PMIC[MT6322]    Source code structure Build option Battery char ...

  3. python-web apache mod_python 模块的安装

    安装apache 下载mod_python 编译安装 测试 下载mod_python,下载地址:mod_python 在GitHub 上面, 下载之后:目录结构如下: 安装依赖: #查找可安装的依赖 ...

  4. Es首页

    https://www.elastic.co/guide/en/elasticsearch/reference/index.html

  5. HDU 5512 Pagodas【博弈】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5512 题意: 给定集合,最初有两个数a,b,如果两个人依次使用集合中的元素相加减,如果得到的数均不在 ...

  6. Treasure Hunt--poj1066(最短路加判断线段的关系)

    http://poj.org/problem?id=1066 题目大意:有n条线段 他们都在这个房间里   最后有一个点代表起始位置 现在想通过墙出去  他只能爆破每个房间的中点的门   问最少的门通 ...

  7. 洛谷—— P1186 玛丽卡

    https://www.luogu.org/problem/show?pid=1186 题目描述 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长 ...

  8. [Bzoj3611][Heoi2014]大工程(虚树)

    3611: [Heoi2014]大工程 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 2000  Solved: 837[Submit][Status ...

  9. Java实现敏感词过滤代码

    原文:http://www.open-open.com/code/view/1445762764148 import java.io.BufferedReader; import java.io.Fi ...

  10. pip命令自动补全功能;设置代理;使用国内源

    这是pip自带的功能 执行的脚本 把脚本写入.zshrc或者profile等里面,执行source立即生效 设置代理: pip --proxy=http://username:password@pro ...