【POJ 3107 】Godfather 【樹的重心】
Language:
Godfather
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7652 Accepted: 2701
Description
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
題意,給定一棵樹,求其中所有的重心序號。
糟心,用vector存圖,無限TLE,改為鄰接表就過了。有毒。。。
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
#define LL long long
const int MAXN = 5*1e5+10;
const int MAXM = 1e6;
const int inf = 0x3f3f3f3f;
struct Edge {
int from,to,nexts;
}edge[MAXN<<2];
int head[MAXN],top;
void init(){
memset(head,-1,sizeof(head));
top=0;
}
void addedge(int a,int b){
Edge e={a,b,head[a]};
edge[top]=e;head[a]=top++;
}
int n,m;
int dp[MAXN],num[MAXN];
void dfs(int now,int pre){
num[now]=1;
for(int i=head[now];i!=-1;i=edge[i].nexts){
int v=edge[i].to;
if(v==pre) continue;
dfs(v,now);
num[now]+=num[v];
dp[now]=max(dp[now],num[v]);
}
dp[now]=max(dp[now],n-num[now]);
}
int main(){
scanf("%d",&n);m=n-1;
int a,b;
init();
while(m--){
scanf("%d%d",&a,&b);
addedge(a,b);
addedge(b,a);
}
dfs(1,-1);
int sum=inf;
for(int i=1;i<=n; if(sum>dp[i]) sum=dp[i];
for(int i=1;i<=n;i++)
if(sum==dp[i]) printf("%d ",i);
puts("");
return 0;
}
相關推薦
【POJ 3107 】Godfather 【樹的重心】
Language: Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7652 Accepted: 2701 Description Last y
【LCT維護基環內向樹森林】BZOJ4764 彈飛大爺
read 3.1 wap max limit spa register ostream turn 4764: 彈飛大爺 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 101 Solved: 52[Submit][Stat
【poj 2559 Largest Rectangle in a Histogram】【單調棧】
【連結】 【題意】 在一條水平線上有若干緊挨的矩形,求包含於這些矩形的並集內部的最大的矩形的面積(矩形個數<=1e5) 【分析】我們先考慮,若矩形的高度從左往右單調遞增,那麼答案顯而易見嘗試以每個矩形的高度為最終矩形的高度,並把寬度延伸到右邊界,得到一個矩形,
HDU 1075.What Are You Talking About【Map水題(字典樹)】【5月25】
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others) Total Submission(s): 1
【字尾陣列/SAM+邊分樹合併】LGP5115 Check,Check,Check one two!
【題目】 原題地址 給定一個字串 S S S,求
【101-Symmetric Tree(對稱樹)】
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmet
POJ 1655 +POJ 3107【求樹的重心】
樹的重心:樹的重心也叫樹的質心。找到一個點,其所有的子樹中最大的子樹節點數最少,那麼這個點就是這棵樹的重心,刪去重心後,生成的多棵樹儘可能平衡。 換句話說,刪除這個點後最大連通塊(一定是樹)的結點數最
【POJ - 1655】Balancing Act (樹的重心)
Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more
【POJ 1655】Balancing Act 【樹的重心】
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9241 Accepted: 3846 Description Consider a tree T with N
poj 2828 Buy Tickets 【線段樹點更新】
clu input family freopen rst cst href targe pri 題目:poj 2828 Buy Tickets 題意:有n個人排隊,每一個人有一個價值和要插的位置,然後當要插的位置上有人時全部的人向後移動一位當這個插入到這兒,假設沒有
【POJ 2482】 Stars in Your Window(線段樹+離散化+掃描線)
d+ opera algorithm ans som lov ble word wait 【POJ 2482】 Stars in Your Window(線段樹+離散化+掃描線) Time Limit: 1000MS M
POJ 3177--Redundant Paths【無向圖添加最少的邊成為邊雙連通圖 && tarjan求ebc && 縮點構造縮點樹】
when tab sub exp 無向圖 redundant -m term 一個點 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submis
hdu 1540/POJ 2892 Tunnel Warfare 【線段樹區間合並】
article 遞歸 線段 tdi != lines nor sample eight Tunnel Warfare Time Limi
POJ 3321 Apple Tree 【樹狀數組+建樹】
size stream typedef val nth beginning emp get 鄰接 題目鏈接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limi
【POJ - 2001 】Shortest Prefixes (字典樹,查詢重複字首區間)
題幹: A prefix of a string is a substring starting at the beginning of the given string. The prefixes of "carbon" are: "c", "ca", "car", "carb", "ca
【POJ 2104】【主席樹模板題】K-th Number
題意: 靜態詢問區間第K大問題。給出一個數組,然後多次詢問某一區間第K大數是多少。 思路: 典型的主席樹模板題。 所以就大致講一下靜態主席樹
Mayor's posters 【POJ - 2528】【線段樹:講解離散化後TLE的原因】
題目連結 題目是一道很好的題,他用很神奇的資料告訴了我們——少用map,會TLE的。。。所以,這道題怎樣避免N*log(N)的map??我們可以換成lower_bound()來進行優化,但是在此之前可以用unique()進行去重操作(自己懶,所以用已有的STL檔案)。 &n
【Roads in the North】【POJ - 2631】(樹的直徑)
題目: Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that t
Counting Black 【POJ - 1656】【二維線段樹+記憶體優化】
題目連結 這道題卡了記憶體,但是處理這個記憶體的方式卻也簡單,可以直接用short int來減少記憶體的使用,於是就可以用四叉樹——二維線段樹過了。 #include <iostream> #include <cstdio> #include &
Matrix 【POJ - 2155】【二維線段樹+永久化標記】
題目連結 挺好的一道題,一開始用lazy標記往下推,總是推不出樣例的正解,然後就去看了相關部落格,發現卻確實如此,在這裡是無法用lazy標記來層層推的,並且還會出現超記憶體的情況,所以,便改用了永久化標記來解這道題。 還有一件是,關於discuss裡的討論區有