奇安信:笔试题(20190909)

结束进程树

输入很难处理,没有给定每行数字的个数。这里采用全盘接收了再按情况分割放入容器内。

#include 
#include 
#include 

using namespace std;

int kill_sub_process(vector<int> child, vector<int> parent, int start)
{
	deque<int> values;
	values.push_back(start);
	int res = 1;
	while(!values.empty())
	{
		int cur = values.front();
		values.pop_front();

		for(int pi=0; pi<parent.size(); pi++)
		{
			if(cur == parent[pi])
			{
				res += 1;
				values.push_back(child[pi]);
			}
		}
	}
	return res;
}


int main()
{
	vector<int> total;
	int val;
	while(cin>>val)
	{
		total.push_back(val);
	}

	int start = total[total.size()-1];
	total.pop_back();

	vector<int> child, parent;
	for(int ti=0; ti<total.size(); ti++)
	{
		if(ti < total.size()/2)
			child.push_back(total[ti]);
		else
			parent.push_back(total[ti]);
	}

	int res;
	res = kill_sub_process(child, parent, start);
	cout << res << endl;

	return 0;
}

/*
3 1 5 21 10
0 3 3 1 5
5
*/

(最近更新:2019年09月12日)

你可能感兴趣的:(PROGRAM)