> For the complete documentation index, see [llms.txt](https://soumyajit4419.gitbook.io/ds-algo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soumyajit4419.gitbook.io/ds-algo/graph/4.is-it-a-tree.md).

# 4.Is it a tree?

**An undirected graph is tree if it has following properties.** \
**1) There is no cycle.** \
**2) The graph is connected.**

## Solution I:(Using BFS)

```cpp
#include <bits/stdc++.h>

using namespace std;

bool bfs(int node, vector<int> v[], vector<bool> &vs, vector<int> &parent)
{
    queue<int> q;
    q.push(node);
    vs[node] = 1;
    parent[node] = node;

    while (!q.empty())
    {
        int x = q.front();
        q.pop();

        for (auto itr = v[x].begin(); itr != v[x].end(); itr++)
        {
            if (vs[*itr] == 0)
            {
                q.push(*itr);
                vs[*itr] = 1;
                parent[*itr] = x;
            }
             // If an adjacent is visited and not parent of current 
            else if (vs[*itr] == 1 && parent[x] != *itr)
            {
                return false;
            }
        }
    }
    return true;
}

int main()
{
    int n, m;
    cin >> n >> m;

    vector<int> v[n + 1];
    vector<bool> vs(n + 1);
    vector<int> parent(n + 1);
    for (int i = 1; i <= m; i++)
    {
        int a, b;
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    bool x = bfs(1, v, vs, parent);
    if (!x)
    {
        cout << "NO";
    }
    else
    {
        int res = 0;
        for (int i = 1; i <= n; i++)
        {
            if (vs[i] == 0)
            {
                res = 1;
                break;
            }
        }
        if (res == 1)
        {
            cout << "NO";
        }
        else
        {
            cout << "YES";
        }
    }

    return 0;
}

```

## Solution II: (Using Disjoint sets)

```cpp
#include <bits/stdc++.h>

using namespace std;

int findPar(vector<int> &v, int node)
{
    if (v[node] == -1)
    {
        return node;
    }

    v[node] = findPar(v, v[node]);
    return v[node];
}

bool uni(vector<int> &v, vector<int> &rank, int a, int b)
{
    int parA = findPar(v, a);
    int parB = findPar(v, b);
    if (parA == parB)
    {
        return false;
    }

    if (rank[parA] > rank[parB])
    {
        v[parB] = parA;
    }
    else if (rank[parB] > rank[parA])
    {
        v[parA] = parB;
    }
    else
    {
        v[parA] = parB;
        rank[parB]++;
    }

    return true;
}

int main()
{
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> nodes;
    vector<int> v(n + 1, -1);
    vector<int> rank(n + 1, 0);
    for (int i = 1; i <= m; i++)
    {
        int a, b;
        cin >> a >> b;

        nodes.push_back({a, b});
    }

    int res = 0;
    for (auto itr = nodes.begin(); itr != nodes.end(); itr++)
    {
        pair<int, int> p = *itr;
        bool x = uni(v, rank, p.first, p.second);
        if (!x)
        {
            res = 1;
            break;
        }
    }

    if (res != 1)
    {
        int count = 0;
        for (int k = 1; k <= n; k++)
        {
            if (v[k] == -1)
            {
                count++;
            }

            if (count > 1)
            {
                res = 1;
                break;
            }
        }
    }

    if (res == 1)
    {
        cout << "NO";
    }
    else
    {
        cout << "YES";
    }

    return 0;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://soumyajit4419.gitbook.io/ds-algo/graph/4.is-it-a-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
