Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.2 kB
4
Indexable
Never
#include<iostream>

using namespace std;

int t,n,m,x;

class queue{
	int front; int rear; int arr[1000];
public:
	queue(){
		rear=-1;
		front=-1;
	}
	void push(int a){
		rear++;
		arr[rear]=a;
	}
	void pop(){
		front++;
	}
	int peek(){
		return arr[front+1];
	}
	bool isempty(){
		return (front==rear);
	}
	void reset(){
		rear=-1;
		front=-1;
	}
};

queue q;
int mang[1001][1000];
bool visited[1001],flag;

int bfs(int a){
	q.push(a);
	visited[a]=true;
	while(!q.isempty()){
		int b=q.peek();
		q.pop();
		for(int i=1;i<=mang[b][0];i++){
			if(visited[mang[b][i]]) return 1;
			else {
				visited[mang[b][i]]=true;
				q.push(i);
			}
		}
	}
	return 0;
}

int main(){
	freopen("input.txt","r",stdin);
	cin >> t;
	for(int tc=1;tc<=t;tc++){
		cin >> n;
		for(int i=1;i<=n;i++){
			cin >> mang[i][0];
			for(int j=1;j<=mang[i][0];j++) cin >> mang[i][j];
		}
		flag=false;
		for(int i=1;i<=n;i++){
			q.reset();
			for(int i=1;i<=n;i++) visited[i]=false;
			if(bfs(i)==1){
				flag=true;
				break;
			}
		}
		if(flag) cout << "Yes" << endl;
		else cout << "No" << endl;
	}


	return 0;
}
Leave a Comment