标签:
Problem - 3974
http://acm.hdu.edu.cn/showproblem.php?pid=3974
Description:
Input:
Output:
Sample Input:
Sample Output:
#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; }
标签:
原文地址:http://www.cnblogs.com/syhandll/p/4701001.html