现在的位置: 首页 > 综合 > 正文

【PAT Advanced Level】1032. Sharing (25)

2018年02月23日 ⁄ 综合 ⁄ 共 622字 ⁄ 字号 评论关闭

这题如果用C++标准库里的string的话,会超时,还是用int或者char数组来保存吧。

先遍历一遍,每个节点打上标记,再换一个头,遍历第二遍,如果碰到标记,就是所求。

#include <iostream>
#include <map>
#include <iomanip>
using namespace std;

struct node
{
	char s;
	int next;
	bool flag;
	node(char s, int n, bool f) : s(s), next(n), flag(f) {}
	node() {}
};

map<int, node> m;
int a, b, c;
int d, e;
char cc; 

int main()
{
//	freopen("a.txt", "r", stdin);
	scanf("%d%d%d", &a, &b, &c);
	for(int i = 0; i < c; i++)
	{
		scanf("%d%s%d", &d, &cc, &e);
		node n(cc, e, false);
		m[d] = n;
	}
	int tmp = b;
	while (tmp != -1)
	{
		m[tmp].flag = true;
		tmp = m[tmp].next;
	}
	tmp = a;
	while (tmp != -1)
	{
		if(m[tmp].flag == true)
			break;
		tmp = m[tmp].next;
	}
	if(tmp != -1)
	{
		cout.fill('0');
		cout<<setw(5)<<std::right<<tmp<<endl;
	}
	else cout<<tmp<<endl;
}

抱歉!评论已关闭.