奇安信:笔试题(20190909)

结束进程树

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

#include <iostream>
#include <vector>
#include <deque>

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日)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值