QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#351035#6729. Unrooted TrieDream_56Compile Error//C++142.9kb2024-03-11 13:08:392024-03-11 13:08:40

Judging History

你现在查看的是最新测评结果

  • [2024-03-11 13:08:40]
  • 评测
  • [2024-03-11 13:08:39]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using li = long long;
using pli = pair<long long, int>;

struct Graph {
public:
    int n, m;
    vector<int> head, to, nxt;
    vector<char> val;
    Graph(int n, int m): head(n + 5), to((m << 1) + 5), val((m << 1) + 5), nxt((m << 1) +5) {}
    void adde(int x, int y, char w) {
        to[tot] = y, val[tot] = w, nxt[tot] = head[x], head[x] = tot, tot++;
        swap(x, y);
        to[tot] = y, val[tot] = w, nxt[tot] = head[x], head[x] = tot, tot++;
    }
private:
    int tot = 2;
};

void solve() {
    int n; cin >> n;
    Graph Tree(n, n - 1);
    for (int i = 1; i < n; ++i) {
        int x, y; 
        char w;
        cin >> x >> y >> w;
        Tree.adde(x, y, w);
    }

    int point = -1, x, y;
    for (int i = 1; i <= n; ++i) {
        vector<vector<int>> ch(26);
        for (int ei = Tree.head[i]; ei; ei = Tree.nxt[ei]) {
            int v = Tree.to[ei], val = Tree.val[ei] - 'a';
            ch[val].push_back(v);
            if (ch[val].size() > 2) {
                cout << 0 << '\n';
                return;
            } else if (ch[val].size() == 2) {
                if (point != -1) {
                    cout << 0 << '\n';
                    return;
                } else {
                    // cout << ch[val][0] << " " << ch[val][1] << '\n';
                    point = i;
                    x = ch[val][0], y = ch[val][1];
                    // cout << point << " ! " << x << " " << y << '\n';
                }
            }
        }
    }

    if (point == -1) {
        cout << n << '\n';
        return
    }

    int dfnid = 0;
    vector<int> bgn(n + 5), fin(n + 5);
    auto dfs = [&](auto self, int u, int fa) -> void {
        bgn[u] = ++dfnid;
        for (int ei = Tree.head[u]; ei; ei = Tree.nxt[ei]) {
            int v = Tree.to[ei];
            if (v == fa) 
                continue;
            self(self, v, u);
        }
        fin[u] = dfnid;
    };
    dfs(dfs, 1, 0);

    // cout << point << " " << x << " " << y << '\n';

    if (bgn[x] > bgn[y]) 
        swap(x, y);
    // cout << point << " " << x << " " << y << '\n';
    // cout << bgn[x] <//< " " << fin[x] << " " << bgn[y] << " " << fin[y] << '\n';
    vector<int> f(n + 5);
    if (bgn[point] < bgn[x] and bgn[point] < bgn[y]) {
        f[1]++, f[n + 1]--;
        f[bgn[x]]--, f[fin[x] + 1]++;
        f[bgn[y]]--, f[fin[y] + 1]++;
    } else {
        f[bgn[point]]++, f[fin[point] + 1]--;
        f[bgn[y]]--, f[fin[y]+1]++;
    }

    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        // cout << f[i] << " ";
        f[i] += f[i - 1];
        // cout << f[i] << '\n';
        if (f[i] == 0) {
            ++ans;
        }
    }
    cout << ans << '\n';
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int T; cin >> T;
    while (T--) {
        solve();
    }
    
}

Details

answer.code: In function ‘void solve()’:
answer.code:57:5: error: expected primary-expression before ‘}’ token
   57 |     }
      |     ^
answer.code:56:15: error: expected ‘;’ before ‘}’ token
   56 |         return
      |               ^
      |               ;
   57 |     }
      |     ~