Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.2 kB
2
Indexable
Never
#include<iostream>

using namespace std;

int t,n,m,x;

class queue{
	int front; int rear; int arr[1005];
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[1005][1005];
bool visited[1005],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(mang[b][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 j=1;j<=n;j++) visited[j]=false;
			if(bfs(i)==1){
				flag=true;
				break;
			}
		}
		cout << "Case #" << tc << endl;
		if(flag) cout << "Yes" << endl;
		else cout << "No" << endl;
	}


	return 0;
}
Leave a Comment