unt311's blog

By unt311, history, 3 weeks ago, In English

Please share your dfs with lambda function code or improve mine.

        function<void()> dfs = [&](int a, int par, int depth) {
            vis[a] = true;
            if(depth > maxDepth){
                maxDepth = depth;
                farthestNode = a;
            }
            for(auto x: adj[a]){
                if(!vis[x])
                    dfs(x, a, 1 + dep);
            }
        };

I get the following error. error: no match for call to '(std::function<void()>) (long long int&, long long int&, long long int)'|

NOTE: changing a to &a and p to &p in parameter list does not make the error go away.

 
 
 
 
  • Vote: I like it
  • +8
  • Vote: I do not like it

»
3 weeks ago, # |
  Vote: I like it +5 Vote: I do not like it

I have #define int long long in the beginning of my code, in case you wonder where did long long come from.

»
3 weeks ago, # |
  Vote: I like it +17 Vote: I do not like it

Try function <void(int, int, int)> instead.

»
3 weeks ago, # |
  Vote: I like it +47 Vote: I do not like it

I like to write them like this:

vi col(n);
col[0] = 0;

auto dfs = [&](int u, int p, auto&& dfs) -> void {
	for (int v : adj[u])
		if (v != p) {
			col[v] = col[u] ^ 1;
			dfs(v, u, dfs);
		}
};

dfs(0, -1, dfs);