MarkDown挺清新的!
Problem C. 进程管理
题目描述
在操作系统中,进程管理是非常重要的工作,每个进程都有唯一的进程标识(PID)。每个进程都可以启动子进程,此时我们称它为其子进程的父进程,除了PID为0的进程之外,每个进程有且只有一个父进程,在这个任务中,你需要实时维护操作系统运行中的三个基本操作:
1.FORK PID1 PID2:标识为PID1的进程启动了一个标识为PID2的子进程。
2.KILL PID:结束标识为PID的进程。请注意,与此同时所有PID的子进程也将同时结束。如果PID是不存在或已经结束的进程,则不做任何操作。
3.QUERY PID:查询标识为PID的进程是否仍然存在。
在初始状态下,系统只开启了PID为0的进程,并且在任何情况下该进程不会结束。
输入格式
输入的第一行是一个整数T(T<=50),表示输入的数据组数。
每组测试数据的第一行是一个整数N(1<=N<=100),表示操作的数量。
没下来N行,每行按照上面的描述给出每个操作,输入保证所有的进程的PID都不相同,且一个进程结束后不会被重新启动,所有PID都是[1,100]之间的整数。
输出格式
5
FORK 0 1
QUERY 1
KILL 1
QUERY 1
QUERY 2
1
QUERY 0
输出样例
Yes
No
No
Yes
#include <cstdio>
#include <vector>
#define MAXSIZE 200
using namespace std;
typedef struct BiNode{
vector<int> sonList;
bool turnOn;
}*BiTree;
BiNode Tree[MAXSIZE];
BiNode& createNode(int index){
if (Tree[index].turnOn!=true){
BiNode &p=Tree[index];//****
p.turnOn=true;
p.sonList.clear();
return p;
}
}
void delTree(int index){//postOrder
if (Tree[index].turnOn!=false){
vector<int> &sonList=Tree[index].sonList;
//±éÀú×ÓÊ÷
for (int i=0;i<sonList.size();i++){
delTree(sonList[i]);
}
//visit
Tree[index].sonList.clear();
Tree[index].turnOn=false;
}
}
int main(){
int t,n;
int father,son;
char operation[20];
scanf("%d",&t);
while (t--){
//input
scanf("%d",&n);
//initiate
for (int i=0;i<MAXSIZE;i++){
Tree[i].turnOn=false;
Tree[i].sonList.clear();
}
Tree[0].turnOn=true;
//operate
while (n--){
scanf("%s",operation);
switch(operation[0]){
case 'F':
//;
scanf("%d%d",&father,&son);
createNode(son);
Tree[father].sonList.push_back(son);
break;
case 'K':
scanf("%d",&father);
delTree(father);
break;
case 'Q':
scanf("%d",&father);
printf("%s\n",Tree[father].turnOn?"Yes":"No");
break;
}
}
}
return true;
}