CSES - Datatähti Open 2021 - Results
Submission details
Task:Distances
Sender:fishy15
Submission time:2021-01-31 02:31:15 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void solve()':
input/code.cpp:39:5: error: 'function' was not declared in this scope
     function<void(int, int, bool)> dfs = [&](int v, int p, bool ok) {
     ^~~~~~~~
input/code.cpp:39:5: note: suggested alternative: 'union'
     function<void(int, int, bool)> dfs = [&](int v, int p, bool ok) {
     ^~~~~~~~
     union
input/code.cpp:39:33: error: expression list treated as compound expression in functional cast [-fpermissive]
     function<void(int, int, bool)> dfs = [&](int v, int p, bool ok) {
                                 ^
input/code.cpp:39:14: error: expected primary-expression before 'void'
     function<void(int, int, bool)> dfs = [&](int v, int p, bool ok) {
              ^~~~
input/code.cpp:49:5: error: 'dfs' was not declared in this scope
     dfs(0, 0, true);
     ^~~
input/code.cpp:49:5: note: suggested alternative: 'ffs'
     dfs(0, 0, true);
     ^~~
     ffs

Code

#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>

#define ll long long
#define ld long double
#define eps 1e-8
#define MOD 1000000007

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

// change if necessary
#define MAXN 1000000

using namespace std;

void solve() {
    int n; cin >> n;
    vector<vector<int>> adj(n);

    for (int i = 0; i < n - 1; i++) {
        int a, b; cin >> a >> b;
        a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    function<void(int, int, bool)> dfs = [&](int v, int p, bool ok) {
        if (ok) cout << v + 1 << ' ';
        for (int e : adj[v]) {
            if (e != p) {
                dfs(e, v, !ok);
            }
        }
        if (!ok) cout << v + 1 << ' ';
    };

    dfs(0, 0, true);
    cout << '\n';
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int t; cin >> t;
    while (t--) solve();

    return 0;
}