国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

樹的重心

2019-11-10 17:14:05
字體:
來源:轉載
供稿:網友

方法:

1、存圖遍歷,dfs;2、記錄兩個值,maxsubtree:當前節點的最大子樹,d[x]:當前節點的子樹和(包括x);3、將當前節點x的maxsubtree再和x的祖先之和比較;4、更新重心和最小連通子樹;
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <vector>#define inf 1E9#define maxn 10001using namespace std;vector<int> tree[maxn];int n;int zx=0,minzs=inf;int d[maxn];void dfs(int x, int fa) {	d[x]=1;	int maxzs=0;	for(int i=0; i<tree[x].size(); ++i) {		int to=tree[x][i];		if(to!=fa) {			dfs(to,x);			maxzs=max(maxzs,d[to]);			d[x]+=d[to];		}	}	maxzs=max(maxzs,n-d[x]);	if(maxzs<minzs) {		minzs=maxzs;		zx=x;	}}int main() {	scanf("%d", &n);	for(int i=1; i<=n-1; ++i) {		int x,y;		scanf("%d%d", &x, &y);		tree[x].push_back(y);		tree[y].push_back(x);	}	dfs(1,0);	PRintf("%d %d", zx,minzs);	return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 崇左市| 玉屏| 清水县| 肥城市| 东光县| 会泽县| 湘阴县| 茶陵县| 枞阳县| 岳普湖县| 互助| 北辰区| 桐庐县| 德江县| 九龙城区| 韶关市| 中江县| 建瓯市| 福泉市| 罗甸县| 诸城市| 新田县| 望江县| 攀枝花市| 贵州省| 平安县| 建德市| 浮梁县| 革吉县| 江山市| 南投市| 长岭县| 乐平市| 灌云县| 双桥区| 东兴市| 锦州市| 浠水县| 沧州市| 扎赉特旗| 长宁县|