AlgoUniversity
  • Go Back
Discussion
Mother Vertex :

Author

Lokesh

Difficulty Level : Easy

Submissions : 200

Asked In : Paytm

Marks :10

: 1 | : 0

You are given a directed graph with $$$N$$$ vertices and $$$M$$$ edges.

You are given an array B with dimensions $$$M * 2$$$, where each row denotes a directed edge from $$$B[i][0]$$$ to $$$B[i][1]$$$.

You need to find if there exists a mother vertex in the given graph. Return $$$1$$$ if it exists, otherwise $$$0$$$.

A mother vertex is defined as a vertex from which all the vertices in the graph are accessible by a directed path.

Input

The first line contains integers $$$N, M$$$. $$$1 \le N \le 5*10^5$$$ ,$$$1 \le M \le 10^6$$$.

The second line contains $$$M$$$ integers denoting the first row of Array $$$B.$$$, $$$1 \le B[i][0] \le N$$$

The third line contains $$$M$$$ integers denoting the second row of Array $$$B.$$$, $$$1 \le B[i][1] \le N$$$

Output

Return a single integer 1 if a mother vertex exists, otherwise 0.

Examples

Input
3 3
1 2 1
3 3 3
Output
0
Input
3 3
1 2 3
3 3 2
Output
1
Input
2 2
1 1
2 2
Output
1
Input
3 2
3 3
1 2
Output
1
Input
6 6
1 2 3 4 5 6
2 3 1 5 6 4
Output
0

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.