【POJ 3107】 Godfather
【题目链接】
【算法】
这题描述有些繁琐,先简化一下题意 : 对于一棵无根树,删除一个节点,使得其余的联通块中,最大的联通块最小
那么,这题就很好做了
对这棵树进行一遍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的更多相关文章
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
- 【POJ 1125】Stockbroker Grapevine
id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...
随机推荐
- bzoj 1787 Meet 紧急集合
Meet 紧急集合 这个题是在脖子oj(清北某奆佬给起的名字)八中oj(大视野在线评测)上的. 给出bzoj链接. 这个题还是求最近公共祖先的问题. 而该题不同于别的题,它是需要求三个点的最近公共祖先 ...
- iis性能监控
文章:对于IIS上的应用程序池监控 文章:IIS并发连接数及性能优化
- 洛谷P1016 旅行家的预算
题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能行驶的距离D2.出发点每升汽油价格P和沿 ...
- [NOIP2003] 提高组 洛谷P1039 侦探推理
题目描述 明明同学最近迷上了侦探漫画<柯南>并沉醉于推理游戏之中,于是他召集了一群同学玩推理游戏.游戏的内容是这样的,明明的同学们先商量好由其中的一个人充当罪犯(在明明不知情的情况下),明 ...
- 【HDOJ6304】Chiaki Sequence Revisited(数学)
题意:给定一个序列a,定义a[1]=a[2]=1,a[n]=a[n-a[n-1]]+a[n-1-a[n-2]](n>=3),求该序列的前n项和是多少,结果对 1e9+7 取模 n<=1e1 ...
- PatentTips - Wear Leveling for Erasable Memories
BACKGROUND Erasable memories may have erasable elements that can become unreliable after a predeterm ...
- Codeforces Round #291 (Div. 2) B. Han Solo and Lazer Gun
因为是x,y均为整数因此对于同一直线的点,其最简分数x/y是相同的(y可以为0,这里不做除法)于是将这些点不断求最简分数用pair在set中去重即可. #include <cmath> # ...
- [Bzoj2039]小Z的袜子 (莫队算法模板题)
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 11866 Solved: 5318[Sub ...
- [Unit Testing] Mock an HTTP request using Nock while unit testing
When testing functions that make HTTP requests, it's not preferable for those requests to actually r ...
- ES文件浏览器 WIFI 查看电脑文件怎么弄
1 开启来宾账户 2 右击要共享的文件夹,添加Guest共享(如果只是要查看共享的资源,权限级别为读取即可) 3 共享之后,网络路径就是"\\"+你的计算机名+" ...