1. 程式人生 > >Godfather (樹形dp + 樹的重心)

Godfather (樹形dp + 樹的重心)

Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders.

Unfortunately, the structure of Chicago mafia is rather complicated. There are n persons known to be related to mafia. The police have traced their activity for some time, and know that some of them are communicating with each other. Based on the data collected, the chief of the police suggests that the mafia hierarchy can be represented as a tree. The head of the mafia, Godfather, is the root of the tree, and if some person is represented by a node in the tree, its direct subordinates are represented by the children of that node. For the purpose of conspiracy the gangsters only communicate with their direct subordinates and their direct master.

Unfortunately, though the police know gangsters’ communications, they do not know who is a master in any pair of communicating persons. Thus they only have an undirected tree of communications, and do not know who Godfather is.

Based on the idea that Godfather wants to have the most possible control over mafia, the chief of the police has made a suggestion that Godfather is such a person that after deleting it from the communications tree the size of the largest remaining connected component is as small as possible. Help the police to find all potential Godfathers and they will arrest them.

Input
The first line of the input file contains n — the number of persons suspected to belong to mafia (2 ≤ n ≤ 50 000). Let them be numbered from 1 to n.

The following n − 1 lines contain two integer numbers each. The pair ai, bi means that the gangster ai has communicated with the gangster bi. It is guaranteed that the gangsters’ communications form a tree.

Output
Print the numbers of all persons that are suspected to be Godfather. The numbers must be printed in the increasing order, separated by spaces.

Sample Input
6
1 2
2 3
2 5
3 4
3 6
Sample Output
2 3

相關推薦

Godfather 樹形dp + 重心

Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the

3107 Godfather樹形dp,找重心

題目:就是找樹的重心,可能存在多個,按編號從小到大輸出。一個水題,但是好幾個罰時....不能用vector存邊,會TLE. #include<iostream> #include<cstdio> #include<cstring> #in

POJ 3107 Godfather樹形DP重心

任重而道遠 Description Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all th

Tree Cutting 樹形dp + 重心問題變形

After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible cost, he sued Bess

2018.10.30 NOIP模擬 排列樹形dp+組合數學

傳送門 考試的時候亂搞過了。 其實題目就是讓你求拓撲排序方案數。 直接樹形 d p dp

Educational Codeforces Round 52F樹形DP,vector

nbsp 一個 bits spa sca 最小 i++ min turn #include<bits/stdc++.h>using namespace std;int n,k;vector<int>son[1000007];int dp[100000

COCI2014/2015 Contest#1 F KAMP樹形DP+轉移答案

題意 給定一棵 n n n 個節點的樹表示一個村莊,現在有

[BZOJ3167][Heoi2013]Sao樹形DP+組合數學

Address 洛谷P4099 BZOJ3167 Solution 定義狀態: f [ u

2016年ACM/ICPC瀋陽賽區 I題樹形dp+斜率優化

題目連結:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5642 題意:給你n個點,n-1條邊的樹。

ZJOI/Lnsyoj158/Luogu1131時態同步樹形DP水題【做題報告】

rt,是一道很水的樹形DP 題目描述 給出一棵有根樹,每條邊有一個通過時間。你可以進行一次“操作”,使得某條邊的通過時間增加一秒。 你需要讓根節點到每個葉節點的所需時間都相等,且“操作”次數最少。 葉節點被定義為除根節點外所有隻與一個點相連的節點。 輸入格式 第一行一個正整數NN,表示樹上的節點個數。

bzoj 1812 [IOI2005] riv 樹形dp,樹上揹包

這道題比較有難度。。。首先可以想到f[i][k]表示i點的子樹中用了k個伐木場。但是這顯然沒法轉移。那麼我們加上一維, f[i][j][k]表示i點的子樹內用了k個伐木場並且上一個用的位置為j,而當前點可選可不選(看第二維的狀態),之後用子樹更新即可 合併的時候和更新的時

Information Disturbing HDU - 3586樹形dp + 二分查詢

In the battlefield , an effective way to defeat enemies is to break their communication system.  The information department told you that there a

HDU 1561 The more, The Better樹形DP+01揹包

題目連結 題意(這好像是中文題,不會別的語言=_=||) 思路 我們以0為根節點向外建樹,以前置點為起點,該點為終點,該點價值為線的權值。 由於每個結點只有一個父節點,每點權值等價兩點之間線上的權值。

沒有上司的舞會樹形dp裸題

#include<iostream> #include<cstdio> #include<algorithm> #include<cstring>

hdu 1561 樹形dp+依賴揹包

ACboy很喜歡玩一種戰略遊戲,在一個地圖上,有N座城堡,每座城堡都有一定的寶物,在每次遊戲中ACboy允許攻克M個城堡並獲得裡面的寶物。但由於地理位置原因,有些城堡不能直接攻克,要攻克這些城堡必須先

Computer 樹形dp 經典題

題目:Computer A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 n

Anniversary party樹形dp第一步

There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. I

牛客國慶集訓派對Day3 B Tree樹形dp + 組合計數

題意有點繞,其實就是讓你求一個點能被多少個點集包含,同時這些點集內的點要相互連通。 首先,簡單來說,如果只是計算一個有根樹中任意一個點被多少個只包含它以及它的子樹的點的點集包含,那麼直接普通的樹上統計的trick就可以搞定。但是現在問題是,點集的點可以是其子

重心樹形DP

樹的質心的定義:以這個點為根,那麼所有的子樹的大小(不包括整個樹)不會超過整體大小的一半。 只需要每次比較max{d[son]},和N-d[node],每次儲存最大的值,再從最大的值裡面儲存最小的值,這樣的話從上往下搜的時候就會有一個上下子樹的大小關係變化

POJ-1655 重心樹形 DP

題目 題意 定義一個點的“平衡”值等於將這個點拆去後,形成的子樹中節點數的最大值。求一棵樹“平衡”值最小的點。 題解 這其實就是樹的重心的概念,通過樹形 dp 很容易解決。當去掉抹一點後,它下面的子樹的節點個數通過 dfs 可以得到,它上面的