Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.8 kB
3
Indexable
import java.util.Scanner;

//class Queue{
//	static final int MAX_QUEUE = 10000;
//	String[] A = new String[MAX_QUEUE];
//	int rear;
//	int front;
//	public Queue(){
//		rear = -1;
//		front = 0;
//	}
//	public void enQueue(String x){
//		this.rear++;
//		A[rear] = x;
//	}
//	public String deQueue(){
//		this.front++;
//		return A[front];
//	}
//	public boolean is_Empty(){
//		if(rear == front) return false;
//		return true;
//	}
//	public boolean is_Full(){
//		if(rear == MAX_QUEUE) return true;
//		return false;
//	}
//	public String peek(){
//		return A[front];
//	}
//}

public class FindCycle {
	static int[] arr = new int[10000];
	static boolean[] vis = new boolean[10000];
	static int check = 0;
	static boolean rs = false;
	static int d = 0;
	static int node;
	public static void dfs(int x, int y){
		if(arr[y] == 0 ) return;
		if(check == arr[y]){
			rs = true;
			return;
		}
		dfs(y, arr[y]);	
	}
	public static void main(String agr[]){
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		for(int test_case = 1; test_case <= t; test_case++){
			node = sc.nextInt();
			int edgeNum = sc.nextInt();
			rs = false;
			for(int i = 0; i < vis.length; i++){
				vis[i] = false;
				arr[i] = 0;
			}
			for(int i = 0; i < edgeNum; i++){
				int x = sc.nextInt();
				int y = sc.nextInt();
				arr[x] = y;
			}
			for(int i = 1; i <= node; i++){
				check = i;
				dfs(i,arr[i]);
			}
			
			
			
			System.out.println("Case #" + test_case);
			if(rs){
				System.out.println("1");
			}else{
				System.out.println("0");
			}
		}
	}
}

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