QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#351037#6729. Unrooted TrieDream_56Compile Error//C++142.3kb2024-03-11 13:13:152024-03-11 13:13:16

Judging History

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

  • [2024-03-11 13:13:16]
  • 评测
  • [2024-03-11 13:13:15]
  • 提交

answer

#include <bits/stdc++.h>
#define MAXN ((int) 1e5)
using namespace std;

int n, ans;

vector<int> e[MAXN + 10];
vector<char> v[MAXN + 10];

int clk, bgn[MAXN + 10], fin[MAXN + 10];
int f[MAXN + 10];

void dfs(int sn, int fa) {
    bgn[sn] = ++clk;
    for (int fn : e[sn]) if (fn != fa) dfs(fn, sn);
    fin[sn] = clk;
}

void solve() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) e[i].clear(), v[i].clear();
    for (int i = 1; i < n; i++) {
        int x, y;
        char z[3];
        scanf("%d%d%s", &x, &y, z);
        e[x].push_back(y); v[x].push_back(z[0]);
        e[y].push_back(x); v[y].push_back(z[0]);
    }

    clk = 0; dfs(1, 0);
    memset(f, 0, sizeof(int) * (n + 3));
    for (int sn = 1; sn <= n; sn++) {
        vector<int> vec[26];
        for (int i = 0; i < e[sn].size(); i++) vec[v[sn][i] - 'a'].push_back(e[sn][i]);
        int t = -1;
        for (int i = 0; i < 26; i++) {
            // 相同的字母有至少三个
            if (vec[i].size() > 2) { printf("0\n"); return; }
            else if (vec[i].size() == 2) {
                // 有至少两对相同的字母
                if (t >= 0) { printf("0\n"); return; }
                t = i;
            }
        }
        // 没有相同字母,不影响答案
        if (t == -1) continue;

        int x = vec[t][0], y = vec[t][1];
        if (bgn[x] > bgn[y]) swap(x, y);
        if (bgn[sn] < bgn[x] && bgn[sn] < bgn[y]) {
            // sn 在 dfs 中是 x 和 y 的父节点
            // 只有 x 的子树和 y 的子树可能成为 trie 的根
            f[1]++; f[n + 1]--;
            f[bgn[x]]--; f[fin[x] + 1]++;
            f[bgn[y]]--; f[fin[y] + 1]++;
        } else {
            // x 在 dfs 中是 sn 的父节点
            // 除了 sn 的子树以外的地方可能成为 trie 的根
            f[bgn[sn]]++; f[fin[sn] + 1]--;
            // y 的子树可能成为 trie 的根
            f[bgn[y]]--; f[fin[y] + 1]++;
        }
    }

    ans = 0;
    for (int i = 1; i <= n; i++) {
        f[i] += f[i - 1];
        // 没有标记的位置可以成为 trie 的根
        if (f[i] == 0) ans++;
    }
    printf("%d\n", ans);
}

int main() {
    int tcase; scanf("%d", &tcase);
    while (tcase--) solve();
    return 0;
}
Made with Material for MkDocs

詳細信息

answer.code:79:1: error: ‘Made’ does not name a type
   79 | Made with Material for MkDocs
      | ^~~~
answer.code: In function ‘void solve()’:
answer.code:20:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   20 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
answer.code:25:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   25 |         scanf("%d%d%s", &x, &y, z);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
answer.code: In function ‘int main()’:
answer.code:75:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   75 |     int tcase; scanf("%d", &tcase);
      |                ~~~~~^~~~~~~~~~~~~~