A
B

U522155 板垣 カノエ is WATCHING YOU std

U522155 板垣 カノエ is WATCHING YOU

#include<bits/stdc++.h>
#define int long long 
#define add(a,b) to[++ tot] = b,nxt[tot] = h[a],h[a] = tot
#define con putchar_unlocked(' ')
#define ent putchar_unlocked('\n')
#define Blue_Archive return 0
using namespace std;
constexpr int N = 1e5 + 3;
constexpr int M = 2e5 + 3;
constexpr int INF = 1e18;
constexpr char me[] = "終末なにしてますか?忙しいですか?救ってもらっていいですか?";

int n;
int st;
int cnt;
int tot;
int h[N];
int to[M];
int nxt[M];
int dis[N];
bool vis[N];

string s;

map<string,int> mp;

priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;

inline int read()
{
	int k = 0,f = 1;
	char c = getchar_unlocked();
	while(c < '0' || c > '9')
	{
		if(c == '-') f = -1;
		c = getchar_unlocked();
	} 
	while(c >= '0' && c <= '9') k = (k << 3) + (k << 1) + c - '0',c = getchar_unlocked();
	return k * f;
}

inline void write(int x)
{
	if(x < 0) putchar_unlocked('-'),x = -x;
	if(x > 9) write(x / 10);
	putchar_unlocked(x % 10 + '0');
}

inline void dijk()
{
	for(int i = 1;i <= cnt;i ++) dis[i] = INF;
	q.push({dis[st],st});
	vis[st] = 1;
	dis[st] = 0;
	while(!q.empty())
	{
		int u = q.top().second;
		q.pop();
		for(int i = h[u];i;i = nxt[i])
		{
			if(dis[to[i]] > dis[u] + 1)
			{
				dis[to[i]] = dis[u] + 1;
				if(!vis[to[i]])
				{
					q.push({dis[to[i]],to[i]});
					vis[to[i]] = 1;
				}
			}
		}
	}
}

signed main()
{
	// freopen("data.in","r",stdin);freopen("data.out","w",stdout);
	n = read();
	for(int i = 1,m,x,las;i <= n;i ++)
	{
		m = read();
		for(int j = 1;j <= m;j ++)
		{
			cin >> s;
			if(!mp[s]) mp[s] = x = ++ cnt;
			else x = mp[s];
			if(j != 1) add(x,las),add(las,x);
			las = x;
		}
	}
	cin >> s;
	st = mp[s];
	dijk();
	int q = read();
	while(q --)
	{
		cin >> s;
		write(dis[mp[s]] == INF ? -1 : dis[mp[s]]);ent;
	}
	Blue_Archive;
}
posted @ 2025-09-19 21:48  MyShiroko  阅读(27)  评论(2)    收藏  举报