POJ 1634 Who's the boss?

题意:

一个员工A的直接上司是那些薪水大于A,并且身高>=A的人中薪水最少的一个。
主席CEO的薪水最高,且身高也是最高的。
有多组数据。
每组数据给出m个员工,和q个询问。
每个员工有id、薪水、身高。
对于每个询问,给出某个id,让你求该员工的直接上司的id和该员工的下属的个数。
若该员工是CEO,则他的上司的id为0。

先将员工按薪水从小到大排序,然后for循环一遍,求某一节点的直接父亲,顺带求节点的孩子个数。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <vector>
#include <map>

using namespace std;
const int maxn=30005;
int m,q;
int fa[maxn];  //存储父亲的编号
int head[maxn];
int num[maxn];  //存储以该节点为子树的包含的定点个数
int idx[1000000];  //存储id对映的编号,原先用map存储的,时间是3500ms,后改用数组,时间800+ms
int tot;
//map<int,int> maps;
struct Edge{
    int next,to;
}edge[maxn];
void add(int u,int v){
    edge[tot].next=head[u];
    edge[tot].to=v;
    head[u]=tot++;
}
struct Staff{
    int idx,id,salary,h;
    bool operator<(const Staff tmp)const{
        return salary<tmp.salary;
    }
}staff[maxn];

void init(){
    memset(head,-1,sizeof(head));
    for(int i=0;i<maxn;i++)
        num[i]=1;
    tot=0;
    //maps.clear();
    memset(fa,0,sizeof(fa));
}

int main()
{
    int t,id;
    staff[0].id=0;
    cin>>t;
    while(t--){
        init();
        scanf("%d%d",&m,&q);
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&staff[i].id,&staff[i].salary,&staff[i].h);
        }
        sort(staff+1,staff+m+1);
        for(int i=1;i<=m;i++){
            //maps[staff[i].id]=i;
            idx[staff[i].id]=i;
            for(int j=i+1;j<=m;j++){
                if(staff[j].salary>staff[i].salary && staff[j].h>=staff[i].h){
                    add(j,i);
                    fa[i]=j;
                    num[j]+=num[i];
                    break;
                }
            }
        }
        int u;
        for(int i=1;i<=q;i++){
            scanf("%d",&id);
            u=idx[id];
            //printf("%d %d
",staff[fa[u]].id,num[u]-1);
            printf("%d %d
",staff[fa[u]].id,num[u]-1);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/chenxiwenruo/p/3530183.html