题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974
出现在窝bin的线段树专题里…第一时间想的是记录入度找出根节点,然后标记深度转换到线段树中。但是发现并不会这个姿势…于是直接深搜更新了…发现没有退化成链的数据。于是水水地过去了………………
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 20 using namespace std; 21 22 #define fr first 23 #define sc second 24 #define pb(a) push_back(a) 25 #define Rint(a) scanf("%d", &a) 26 #define Rll(a) scanf("%I64d", &a) 27 #define Rs(a) scanf("%s", a) 28 #define FRead() freopen("in", "r", stdin) 29 #define FWrite() freopen("out", "w", stdout) 30 #define Rep(i, len) for(int i = 0; i < (len); i++) 31 #define For(i, a, len) for(int i = (a); i < (len); i++) 32 #define Cls(a) memset((a), 0, sizeof(a)) 33 #define Clr(a, x) memset((a), (x), sizeof(a)) 34 #define Full(a) memset((a), 0x7f7f, sizeof(a)) 35 36 #define lrt rt << 1 37 #define rrt rt << 1 | 1 38 const int maxn = 50010; 39 int n, m; 40 int in[maxn]; 41 int depth[maxn]; 42 bool vis[maxn]; 43 vector<int> G[maxn]; 44 int q[maxn], front, tail; 45 int task[maxn]; 46 47 void bfs(int rt) { 48 front = tail = 0; 49 int lv = 0; 50 depth[rt] = lv++; q[tail++] = rt; vis[rt] = 1; 51 while(front < tail) { 52 int u = q[front++]; 53 Rep(i, G[u].size()) { 54 if(!vis[G[u][i]]) { 55 vis[G[u][i]] = 1; 56 depth[G[u][i]] = lv; 57 q[tail++] = G[u][i]; 58 } 59 } 60 lv++; 61 } 62 } 63 64 void update(int rt, int p) { 65 task[rt] = p; 66 Rep(i, G[rt].size()) { 67 update(G[rt][i], p); 68 } 69 } 70 71 int main() { 72 // FRead(); 73 int T, _ = 1; 74 Rint(T); 75 int u, v; 76 char cmd[6]; 77 while(T--) { 78 Rint(n); 79 Rep(i, n+2) G[i].clear(); 80 Cls(depth); Cls(vis); Cls(in); Clr(task, -1); 81 Rep(i, n-1) { 82 Rint(u); Rint(v); 83 G[v].push_back(u); in[u]++; 84 } 85 int rt = -1; 86 For(i, 1, n+1) if(!in[i]) rt = i; 87 bfs(rt); 88 Rint(m); 89 printf("Case #%d: ", _++); 90 while(m--) { 91 Rs(cmd); 92 if(cmd[0] == 'C') { 93 Rint(u); 94 printf("%d ", task[u]); 95 } 96 if(cmd[0] == 'T') { 97 Rint(u); Rint(v); 98 update(u, v); 99 } 100 } 101 } 102 return 0; 103 }