Модуль: Calculation of asymptotic complexity


Задача

8/9

Calculation of asymptotics - 8

Задача

For the code below, find the asymptotics:
#include <bits/stdc++.h>
using namespace std;

vector < vector<int> > g;
vector <int> color;

void dfs(int v , int p)
{
color[v] = 1;
for (int i = 0; i < g[v].size(); i++ )
{
int to = g[v][i];
if (to == p)
continue;
if (color[to] == 1)
{
cout << "YES";
exit(0);
}
if (color[to] == 0)
dfs(to, v);
}
color[v] = 2;
}

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

g.resize(n);
color.resize(n);

for (int i = 0; i < m; i++)
{
cin >> a >> b;
a--; b--;
g[a].push_back(b);
g[b].push_back(a);
}

dfs(0, -1);
cout << "NO";
return 0;
}
 
1) O(n)            2) O(m)          3) O(n+m)      4) O(nm)

Выберите правильный ответ, либо введите его в поле ввода

Комментарий учителя