1032. Sharing (25)

#include <iostream>
#include <vector>

using namespace std;

struct node
{
	int address, next;
	char data;
}nod[100000];

int main()
{
	int begin[2], n;
	scanf("%d%d%d", &begin[0], &begin[1], &n);

	int i;
	node cur;

	for(i = 1; i <= n; i++)
	{
		scanf("%d %c %d", &cur.address, &cur.data, &cur.next);
		nod[cur.address] = cur;
	}

	vector<node> v[2];
	int size[2];

	for(i = 0; i <= 1; i++)
	{
		while(begin[i] != -1)
		{
			cur = nod[begin[i]];
			v[i].push_back(cur);
			begin[i] = cur.next;
		}

		size[i] = v[i].size();
	}

	int index[2];
	if(size[0] > size[1])
	{
		index[0] = size[0] - size[1];
		index[1] = 0;
	}
	else
	{
		index[0] = 0;
		index[1] = size[1] - size[0];
	}

	node n1, n2;
	int res = -1, j;

	for(i = index[0], j = index[1]; i <= size[0] - 1; i++, j++)
	{
		n1 = v[0][i];
		n2 = v[1][j];

		if(n1.address == n2.address)
		{
			res = n1.address;
			break;
		}
	}

	if(res == -1)
	{
		printf("-1\n");
	}
	else
	{
		printf("%05d\n", res);
	}

	system("pause");
	return 0;
}

 

posted on 2025-11-23 17:02  王景迁  阅读(0)  评论(0)    收藏  举报

导航