QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#782#322325#6772. Spicy Restaurant18953267621mshcherbaFailed.2024-08-18 20:30:592024-08-18 20:30:59

詳細信息

Extra Test:

Accepted
time: 0ms
memory: 54792kb

input:

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

output:

-1
2
1
1
0

result:

ok 5 lines

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#322325#6772. Spicy Restaurantmshcherba#AC ✓362ms62212kbC++201.3kb2024-02-06 20:39:052024-02-06 20:39:05

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int N = 1 << 17;
const int K = 103;

VI g[N];
int dist[K][N];

int main()
{
	ios::sync_with_stdio(0); 
	cin.tie(0);
	int n, m, que;
	cin >> n >> m >> que;
	VI w(n);
	for (int& wi : w)
		cin >> wi;
	FOR(i, 0, m)
	{
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		g[u].PB(v);
		g[v].PB(u);
	}
	FOR(sp, 0, K)
	{
		fill(dist[sp], dist[sp] + n, -1);
		queue<int> q;
		FOR(i, 0, n)
		{
			if (w[i] == sp)
			{
				q.push(i);
				dist[sp][i] = 0;
			}
		}
		while (!q.empty())
		{
			int v = q.front();
			q.pop();
			for (int to : g[v])
			{
				if (dist[sp][to] == -1)
				{
					q.push(to);
					dist[sp][to] = dist[sp][v] + 1;
				}
			}
		}
	}
	while (que--)
	{
		int p, a;
		cin >> p >> a;
		p--;
		int d = -1;
		FOR(sp, 1, a + 1)
		{
			if (dist[sp][p] == -1)
				continue;
			if (d == -1 || (dist[sp][p] < d))
				d = dist[sp][p];
		}
		cout << d << "\n";
	}
	return 0;
}