poj 3107 树重心

http://acm.hust.edu.cn/vjudge/problem/18069

和poj 1655差不多:http://www.cnblogs.com/qlky/p/5780933.html

区别就是要记录所有的重心

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 50000+5
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f

#define ls (rt<<1)
#define rs (rt<<1|1)

int n,m;

int ptr = 1,head[MAXN],vis[MAXN];

int res,ans[MAXN],son[MAXN],num=0;

struct node
{
    int y,val,next;
}tree[MAXN<<1];

void add(int fa,int son)
{
    tree[ptr].y = son;
    tree[ptr].next = head[fa];
    head[fa] = ptr++;
}

void dfs(int root)
{
    vis[root] = 1;
    son[root] = 0;
    int tmp = 0;
    for(int i=head[root];i!=-1;i=tree[i].next)
    {
        int y = tree[i].y;
        if(vis[y]) continue;
        dfs(y);
        son[root] += son[y]+1;
        tmp = max(son[y]+1,tmp);
    }
    tmp = max(tmp,n-son[root]-1);
    if(tmp<res)
    {
        ans[0] = root;
        num = 1;
        res = tmp;
    }
    else if(tmp == res) ans[num++] = root;
}

int main()
{
    int i,j,t,kase=1;
    while(~sf("%d",&n))
    {
        mem(tree,0);
        mem(head,-1);
        mem(vis,0);
        mem(ans,0);
        res=INF;
        ptr = 1;
        num = 0;
        int x,y;
        for(i=1;i<n;i++)
        {
            sf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        dfs(1);
        sort(ans,ans+num);
        pf("%d",ans[0]);
        for(i=1;i<num;i++) pf(" %d",ans[i]);
        blank;
    }
    return 0;
}
时间: 2025-01-12 13:58:01

poj 3107 树重心的相关文章

POJ 3107 Godfather (树的重心)

题目地址:POJ 3107 还是裸的树的重心,只不过这个要求将所有的重心都输出.很简单. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include &

poj 3107 Godfather 求树的重心【树形dp】

poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxn = 50006; 7 const int INF = 1

poj 1741 树的点分治(入门)

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18205   Accepted: 5951 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an

poj 2763 树链剖分(单点更新,区间求值)

http://poj.org/problem?id=2763 Description After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional ro

poj 2299 树状数组求逆序数+离散化

http://poj.org/problem?id=2299 最初做离散化的时候没太确定但是写完发现对的---因为后缀数组学的时候,,这种思维习惯了吧 1.初始化as[i]=i:对as数组按照num[]的大小间接排序 2.bs[as[i]]=i:现在bs数组就是num[]数组的离散化后的结果 3.注意,树状数组中lowbit(i)  i是不可以为0的,0&(-0)=0,死循环... #include <cstdio> #include <cstring> #include

poj 3237 树链剖分(区间更新,区间查询)

http://poj.org/problem?id=3237 Description You are given a tree with N nodes. The tree's nodes are numbered 1 through N and its edges are numbered 1 through N ? 1. Each edge is associated with a weight. Then you are to execute a series of instruction

Buy Tickets+POJ+线段树单点更新的灵活运用

Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12927   Accepted: 6410 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue- The Lunar New Year wa

cogs 1583. [POJ 3237] 树的维护 树链剖分套线段树

1583. [POJ 3237] 树的维护 ★★★★   输入文件:maintaintree.in   输出文件:maintaintree.out   简单对比时间限制:5 s   内存限制:128 MB [题目描述] 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1到N-1.每一条边有一个权值.然后你要在树上执行一系列指令.指令可以是如下三种之一: CHANGE i v:将第i条边的权值改成v. NEGATE a b:将点a到点b路径上所有边的权值变成其相反数. QUERY a b

poj 1655 树的重心

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13178   Accepted: 5565 Description 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 m