Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Graphs

Path Between Two Nodes

Easy · Graphs · Asked at Amazon, Microsoft, Google, TCS, Infosys

Given an undirected graph, print YES if there is a path from node s to node t, otherwise NO.

Input

First line: n m. Next m lines: u v. Last line: s t.

Output

YES or NO.

Constraints

BFS or DFS from s, or put every edge into a disjoint-set union and check whether s and t end up in the same set.

Example 1

Input

3 3
1 2
2 3
3 1
1 3

Output

YES

Example 2

Input

6 5
1 2
1 3
4 5
5 6
4 6
1 6

Output

NO

Hints

  1. Only connectivity matters, not the path itself.
  2. A BFS from s visits exactly the nodes reachable from s.
  3. A disjoint-set union (DSU) answers the same question without building an adjacency list.

Approach

Union the two ends of every edge in a DSU (with path compression). Afterwards s and t are connected exactly when find(s) == find(t). BFS from s and checking whether t was visited is equally good.

Complexity: O((n + m) α(n)) time · O(n) space

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

vector<int> parent;
int find(int x) {
    while (parent[x] != x) x = parent[x] = parent[parent[x]];
    return x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    parent.resize(n + 1);
    iota(parent.begin(), parent.end(), 0);
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        parent[find(u)] = find(v);
    }
    int s, t;
    cin >> s >> t;
    cout << (find(s) == find(t) ? "YES" : "NO") << "\n";
    return 0;
}

Python solution

import sys

data = list(map(int, sys.stdin.read().split()))
n, m = data[0], data[1]
parent = list(range(n + 1))


def find(x):
    while parent[x] != x:
        parent[x] = parent[parent[x]]
        x = parent[x]
    return x


for i in range(m):
    u, v = data[2 + 2 * i], data[3 + 2 * i]
    parent[find(u)] = find(v)
s, t = data[2 + 2 * m], data[3 + 2 * m]
print("YES" if find(s) == find(t) else "NO")

Java solution

import java.io.*;

public class Main {
    static int[] parent;

    static int find(int x) {
        while (parent[x] != x) x = parent[x] = parent[parent[x]];
        return x;
    }

    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        in.nextToken();
        int m = (int) in.nval;
        parent = new int[n + 1];
        for (int i = 0; i <= n; i++) parent[i] = i;
        for (int i = 0; i < m; i++) {
            in.nextToken();
            int u = (int) in.nval;
            in.nextToken();
            int v = (int) in.nval;
            parent[find(u)] = find(v);
        }
        in.nextToken();
        int s = (int) in.nval;
        in.nextToken();
        int t = (int) in.nval;
        System.out.println(find(s) == find(t) ? "YES" : "NO");
    }
}

Solve Path Between Two Nodes online · All practice problems