60分bfs求调,悬赏关注
查看原帖
60分bfs求调,悬赏关注
1288642
lmn985楼主2025/1/23 20:26
#include <bits/stdc++.h>
#define st first
#define nd second
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const int N=2e5+5;
int n,k,q,ans[N][105],T;
vector<pair<int,int> > G2[N];
vector<int> a[N];
struct node{
	int lh,lw;
	bool lflag;
	int lr;
};
queue<node> que;
void solve(){
	vector<vector<vector<int> > > vis;
	memset(ans,0,sizeof(ans));
	rep(i,0,N)G2[i].clear();
	cin>>n>>k>>q;
	rep(i,0,n){
		vector<int> xa(15);
		vector<vector<int> > la;
		a[i].clear();
		int len;
	    cin>>len;
	    rep(j,0,len)la.push_back(xa);
		rep(j,0,len){
			int x;
			cin>>x;
			a[i].push_back(x);
			G2[x].push_back(make_pair(i,j));
		}
		vis.push_back(la);
	}
	que.push({-1,-1,1,0});
	while(que.size()){
		int h=que.front().lh,w=que.front().lw,dep=que.front().lr;
		bool flag=que.front().lflag;
		int x=1;
		if(h!=-1)x=a[h][w];
		que.pop();
		if(flag){
			rep(i,0,G2[x].size() and dep+1<12){
				pair<int,int> P=G2[x][i];
				if(P.st==h)continue;
				que.push({P.st,P.nd,0,dep+1});
			}
		}
		else{
			int r=min((int)a[h].size(),w+k);
			rep(i,w+1,r){
				if(!vis[h][i][dep]){
					vis[h][i][dep]=ans[a[h][i]][dep]=1;
					que.push({h,i,1,dep});
				}
			}
		}
	}
	while(q--){
		int r,c;
	    cin>>r>>c;
		putchar(ans[c][r]+'0');
		putchar('\n');
	}
}
int main(){
	cin>>T;
	while(T--)solve();
    return 0;
}
2025/1/23 20:26
加载中...