#P15534. 【MYCOI R1】那猫猫城的集市

    ID: 14987 远端评测题 5000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>洛谷原创O2优化洛谷月赛离线处理

【MYCOI R1】那猫猫城的集市

Description

Cat City has nn markets, connected by n1n-1 bidirectional roads such that any two markets can reach each other via some paths. Each market sells two types of goods au,bua_u, b_u (aubua_u \neq b_u).

Now Xiao Meng, a cat, has planned QQ journeys. Each time, Xiao Meng will start from market uu and travel along the shortest path to market vv.

When passing through a market (including uu and vv), Xiao Meng will attempt to trade. If Xiao Meng already possesses one of the goods sold at that market, he will exchange it for the other good sold there; otherwise, he keeps his original goods.

Initially, Xiao Meng possesses a good of type xx. Find out what type of good Xiao Meng ends up with after the journey.

Input Format

The first line contains two positive integers n,Qn, Q.

The next two lines each contain nn positive integers. The ii-th number in the first line is aia_i, and the ii-th number in the second line is bib_i.

The following n1n-1 lines each contain two positive integers u,vu, v, indicating a road between market uu and market vv.

The following QQ lines each contain three positive integers u,v,xu, v, x, describing a journey.

Output Format

Output QQ lines, each containing a positive integer representing the type of good Xiao Meng finally possesses.

5 3
3 4 1 5 2
2 5 5 2 4
5 4
1 4
3 1
2 4
3 4 1
2 5 4
1 5 3
2
4
5

Hint

This problem uses bundled testing.

::cute-table{tuack} | Subtask | Constraints | Special Properties | Points | |:---------:|:---------------:|:----------------------:|:-----:| | Subtask 1 | N,Q1000N,Q \leq 1000 | The tree is a chain | 5 | | Subtask 2 | N,Q1000N,Q \leq 1000 | None | 15 | | Subtask 3 | N,Q106N,Q \leq 10^6 | i,ai=1,bi=2\forall i, a_i=1, b_i=2 | 10 | | Subtask 4 | N,Q106N,Q \leq 10^6 | The tree is a chain | 15 | | Subtask 5 | N,Q105N,Q \leq 10^5 | None | 25 | | Subtask 6 | N,Q106N,Q \leq 10^6 | None | 30 |

For 100%100\% of the data, 2N1062 \le N \le 10^6, 1Q1061 \le Q \le 10^6, 1ai,biN1 \le a_i, b_i \le N.

Please pay special attention to the time and memory limits.

Explanation of Example 1

As shown in the figure, for the first query: starting from market 33, because Xiao Meng possesses good 11, he exchanges it for 55. At market 22, there is no matching good, so no exchange occurs. At market 44, he exchanges it for good 22.

A Fast I/O Template Is Provided

namespace io
{
	char *p1,*p2,buf[100001];
	#define getchar() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
	template<typename T>
	inline typename __gnu_cxx::__enable_if<__is_integer<T>::__value,T>::__type read()
	{
		T sum=0;
		char ch;
		do ch=getchar();                               while(!isdigit(ch));
		do sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar(); while( isdigit(ch));
		return sum;
	}
	template<typename T>
	inline typename __gnu_cxx::__enable_if<__is_integer<T>::__value,void>::__type read(T& sum)
	{
		sum=0;
		char ch;
		do ch=getchar();                               while(!isdigit(ch));
		do sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar(); while( isdigit(ch));
	}
	#undef getchar
}

Usage

int n;
long long m;
io::read(n);
io::read(m);
int k = io::read<int>();
long long q = io::read<long long>();