POJ 1680 Fork() Makes Trouble
原题链接:http://poj.org/problem?id=1680
对这道题,我只能说:我不知道题目是什么意思,但是AC还是没有问题的。
看来题目半天没明白fork()怎么个工作,但是看样例(根据Loop)自己推出来是一个完全二叉树的前序遍历,而且PID非常的有规律,自己画图看看就知道了。
1 #include <stdio.h> 2 #include <string.h> 3 #define lson cur << 1 4 #define rson cur << 1 | 1 5 6 const int maxn = 50000; 7 8 struct node 9 { 10 int pid; 11 int loop; 12 int tag; 13 }tree[maxn]; 14 15 int n, i, k; 16 void build(int cur, int d) 17 { 18 if(d == n) 19 return; 20 tree[lson].loop = d; 21 tree[lson].pid = k; 22 tree[lson].tag = ++k; 23 build(lson, d+1); 24 tree[rson].loop = d; 25 tree[rson].tag = ++k; 26 tree[rson].pid = tree[cur].pid; 27 build(rson, d+1); 28 } 29 30 void dfs(int cur, int d, int flag) 31 { 32 if(cur >= maxn) return; 33 if(d+1000 == tree[cur].tag) 34 { 35 if(flag) 36 printf("Loop %d: Process ID=%d\n", tree[cur].loop, tree[cur].pid); 37 else 38 printf("Process ID=%d, A=%d\n", tree[cur].tag, (tree[cur].loop + 1) * 7); 39 return ; 40 } 41 dfs(lson, d, flag); 42 dfs(rson, d, flag); 43 } 44 45 int main() 46 { 47 int t; 48 scanf("%d", &t); 49 while(t--) 50 { 51 memset(tree, 0, sizeof tree); 52 k = 1000; 53 scanf("%d%d", &n, &i); 54 tree[1].loop = 0, tree[1].pid = k, tree[1].tag = ++k; 55 build(1, 1); 56 dfs(1, (i+1) >> 1, i & 1); 57 } 58 return 0; 59 }