比赛 随便比赛 评测结果 AAAATTTTTT
题目名称 永无乡 最终得分 40
用户昵称 健康铀 运行时间 12.370 s
代码语言 C++ 内存使用 8.63 MiB
提交时间 2024-08-27 20:58:20
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m;
int q;
int t;
int rp[N], cnt[N];
vector <int> f[N];
unordered_map <int, int> mp;

inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    return x * f;
}

void cz(int u, int fa)
{
	for (auto v : f[u])
	{
		if (v == fa) continue;
		cnt[++t] = rp[v];
		cz(v, u);
	}
}
int main(){
    freopen("bzoj_2733.in","r",stdin);
    freopen("bzoj_2733.out","w",stdout);
	n = read();
	m = read();
	for (register int i = 1; i <= n; i++)
	{
		rp[i] = read();
		mp[rp[i]] = i;
	}
	for (register int i = 1, u, v; i <= m; i++)
	{
		u = read();
		v = read();
		f[u].push_back(v);
		f[v].push_back(u);
	}
	q = read();
	while (q--)
	{
		char op;
		int x, y;
		cin >> op;
		x = read();
		y = read();
		if (op == 'Q')
		{
			t = 0;
			cnt[++t] = rp[x];
			cz(x, 0);
			sort(cnt + 1, cnt + t + 1);
			if (t < y) printf("-1\n");
			else printf("%d\n", mp[cnt[y]]);
		}
		if (op == 'B')
		{
			f[x].push_back(y);
			f[y].push_back(x);
		}
	}
	return 0;
}