QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#116025#6507. Recover the StringjzhWA 1ms3436kbC++143.0kb2023-06-27 23:23:482023-06-27 23:23:49

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-27 23:23:49]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3436kb
  • [2023-06-27 23:23:48]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
int n, m;
struct Str {
    int head, tail;
    int pre, suf;
};

int main() {
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        cin >> n >> m;
        vector<vector<int>> graph(n + 1), hparg(n + 1);
        for (int i = 0; i < m; ++i) {
            int u, v;
            cin >> u >> v;
            u--, v--;
            graph[u].push_back(v);
            hparg[v].push_back(u);
        }
        vector<int> ds(n + 5, -1);
        vector<vector<int>> uss(n + 5);
        for (int u = 0; u < n; ++u)
            if (hparg[u].empty()) {
                uss[ds[u] = 1].push_back(u);
            }
        for (int d = 1; d < n; ++d) {
            for (const int u: uss[d]) {
                for (const int v: graph[u]) {
                    if (!~ds[v]) {
                        uss[ds[v] = d + 1].push_back(v);
                    }
                }
            }
        }
        const int L = *max_element(ds.begin(), ds.end());
        int V = 0;
        vector<Str> ss(9 * n + 1);
        vector<vector<int>> xss(n + 5);
        for (const int u: uss[1]) {
            const int x = V++;
            ss[x] = Str{x, x, -1, -1};
            xss[u].push_back(x);
        }
        for (int d = 2; d <= L; ++d) {
            for (const int u: uss[d]) {
                vector<pair<int, int>> ps;
                auto check = [&](int vL, int vR) -> void {
                    for (const int xL: xss[vL])
                        for (const int xR: xss[vR]) {
                            if (ss[xL].suf == ss[xR].pre) {
                                ps.emplace_back(xL, xR);
                            }
                        }
                };
                if (hparg[u].size() == 1) {
                    check(hparg[u][0], hparg[u][0]);
                } else if (hparg[u].size() == 2) {
                    check(hparg[u][0], hparg[u][1]);
                    check(hparg[u][1], hparg[u][0]);
                } else {
                    assert(false);
                }
                sort(ps.begin(), ps.end());
                ps.erase(unique(ps.begin(), ps.end()), ps.end());
                for (auto [sl, sr]: ps) {
                    const int x = V++;
                    auto src = Str{ss[sl].head, ss[sr].tail, sl, sr};
                    ss.push_back(src);
                    xss[u].push_back(x);
                }
                sort(xss[u].begin(), xss[u].end());
                xss[u].erase(unique(xss[u].begin(), xss[u].end()), xss[u].end());
            }
        }
        string ans = "~";
        assert(uss[L].size() == 1);
        for (const int x: xss[uss[L][0]]) {
            string s(L, '?');
            char c = 'a';
            vector<char> tr(L, 0);
            int y = x;
            for (int i = 0; i < L; ++i) {
                int a = ss[y].head;
                if (!tr[a]) tr[a] = c++;
                s[i] = tr[a];
                y = ss[y].suf;
            }
            ans = min(ans, s);
        }
        cout << ans << "\n";
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3436kb

input:

3
1 0
5 6
2 4
2 5
5 3
4 3
1 5
1 4
8 11
1 2
1 4
1 6
2 5
3 4
3 6
4 5
4 7
5 8
6 7
7 8

output:

a
aaa
aaab

result:

wrong answer 2nd lines differ - expected: 'aba', found: 'aaa'