题意就是:公司有n个员工,关系有n-1个,T x y 代表把工作y交给员工x;
员工可以把工作交给下属;
本来是线段树的题,但是我真心看不懂,所以就找了个能看懂的方法了
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; #define N 50050 int f[N]; struct node { int task, order; }a[N]; int Query(int x) { int order = -1, ans = -1; while(x != 0) { if(a[x].order > order) { ans = a[x].task; order = a[x].order; } x = f[x]; } return ans; } int main() { int T, n, m, t=1, x, y; char s[10]; scanf("%d", &T); while(T--) { scanf("%d", &n); memset(a, -1, sizeof(a)); memset(f, 0, sizeof(f)); for(int i=1; i<=n-1; i++) { scanf("%d %d", &x, &y); f[x] = y; } scanf("%d", &m); printf("Case #%d:\n", t++); for(int i=1; i<=m; i++) { scanf("%s", s); if(s[0] == ‘C‘) { scanf("%d", &x); printf("%d\n", Query(x)); } else { scanf("%d %d", &x, &y); a[x].order = i;//工作的顺序; a[x].task = y; } } } return 0; }
时间: 2024-10-05 16:10:33