码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3974 Assign the task

时间:2015-08-04 09:18:36      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

Problem - 3974
http://acm.hdu.edu.cn/showproblem.php?pid=3974

Description:

There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all his subordinates are your subordinates as well. If you are nobody‘s boss, then you have no subordinates,the employee who has no immediate boss is the leader of whole company.So it means the N employees form a tree. 

The company usually assigns some tasks to some employees to finish.When a task is assigned to someone,He/She will assigned it to all his/her subordinates.In other words,the person and all his/her subordinates received a task in the same time. Furthermore,whenever a employee received a task,he/she will stop the current task(if he/she has) and start the new one. 

Write a program that will help in figuring out some employee’s current task after the company assign some tasks to some employee.
 

Input:

The first line contains a single positive integer T( T <= 10 ), indicates the number of test cases. 

For each test case: 

The first line contains an integer N (N ≤ 50,000) , which is the number of the employees. 

The following N - 1 lines each contain two integers u and v, which means the employee v is the immediate boss of employee u(1<=u,v<=N). 

The next line contains an integer M (M ≤ 50,000). 

The following M lines each contain a message which is either 

"C x" which means an inquiry for the current task of employee x 

or 

"T x y"which means the company assign task y to employee x. 

(1<=x<=N,0<=y<=10^9)
 

Output:

For each test case, print the test case number (beginning with 1) in the first line and then for every inquiry, output the correspond answer per line.
 

Sample Input:

1
5
4 3
3 2
1 3
5 2
5
C 3
T 2 1
C 3
T 3 2
C 3
 

Sample Output:

Case #1:
-1
1
2
 
题意:一个公司有n个职员,他们可能有自己的上司和下属,公司如果给其中一个职员分配任务,那么这个任务也会同时分配给他的下属,而且每个新任务分配时,此职员当前任务只能是新任务,现在要查询一些职员的当前任务是什么,若一直未分配任务则输出-1。
 
虽然此专题为线段树专题,但是由于该题用线段树较麻烦,且不便于理解,此次代码为并查集,并不是线段树,之后会再次更新)
 
#include<stdio.h>
#define N 50010
int f[N], vis[N], task[N], n; //f数组保存父节点,即上司
void Init()
{
    int i;
    for (i = 0; i <= n; i++)
    {
        f[i] = i;
        vis[i] = 0;
        task[i] = -1;
    }
}
int Find(int x)
{
    int r = x, v = vis[x], ans = task[x]; //v是任务序号,ans是任务
    while (f[r] != r)
    {
        if (v < vis[f[r]]) //如果找到更靠后的序号,那就更新任务,及其序号
        {
            ans = task[f[r]];
            v = vis[f[r]];
        }
        r = f[r]; //不要忘啦一直查找其父节点,直自其根节点是本身
    }
    return ans;
}
int main ()
{
    int T, m, i, a, b, k = 0, num; //num记录的是分配任务的序号,那么当前任务一定是最后分配的
    char s[10];
    scanf("%d", &T);
    while (T--)
    {
        k++;
        num = 1;
        scanf("%d", &n);
        Init();
        for (i = 1; i < n; i++)
        {
            scanf("%d%d", &a, &b);
            f[a] = b;
        }
        scanf("%d", &m);
        printf("Case #%d:\n", k);
        while (m--)
        {
            scanf("%s", s);
            if (s[0] == T)
            {
                scanf("%d%d", &a, &b);
                task[a] = b;
                vis[a] = num++; //每次分配任务,要记下序号
            }
            else if (s[0] == C)
            {
                scanf("%d", &a);
                printf("%d\n", Find(a));
            }
        }
    }
    return 0;
}

 

 

HDU 3974 Assign the task

标签:

原文地址:http://www.cnblogs.com/syhandll/p/4701001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!