QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#116023#6507. Recover the StringjzhWA 1ms3548kbC++145.0kb2023-06-27 23:19:022023-06-27 23:19:04

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:19:04]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3548kb
  • [2023-06-27 23:19:02]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template<class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };

template<class T>
ostream &operator<<(ostream &os, const vector<T> &as) {
    const int sz = as.size();
    os << "[";
    for (int i = 0; i < sz; ++i) {
        if (i >= 256) {
            os << ", ...";
            break;
        }
        if (i > 0) { os << ", "; }
        os << as[i];
    }
    return os << "]";
}

template<class T>
void pv(T a, T b) {
    for (T i = a; i != b; ++i) cerr << *i << " ";
    cerr << endl;
}

template<class T>
bool chmin(T &t, const T &f) {
    if (t > f) {
        t = f;
        return true;
    }
    return false;
}

template<class T>
bool chmax(T &t, const T &f) {
    if (t < f) {
        t = f;
        return true;
    }
    return false;
}


int N, M;
vector<int> A, B;

vector<vector<int>> graph, hparg;

struct Str {
    int head, tail;
    int pre, suf;
};

int main() {
    for (int numCases; ~scanf("%d", &numCases);) {
        for (int caseId = 1; caseId <= numCases; ++caseId) {
            scanf("%d%d", &N, &M);
            A.resize(M);
            B.resize(M);
            for (int i = 0; i < M; ++i) {
                scanf("%d%d", &A[i], &B[i]);
                --A[i];
                --B[i];
            }

            graph.assign(N, {});
            hparg.assign(N, {});
            for (int i = 0; i < M; ++i) {
                graph[A[i]].push_back(B[i]);
                hparg[B[i]].push_back(A[i]);
            }

            vector<int> ds(N, -1);
            vector<vector<int>> uss(N + 1);
            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());
            assert(uss[L].size() == 1);
            uss.resize(L + 1);
// cerr<<"ds = "<<ds<<endl;
// cerr<<"uss = "<<uss<<endl;

            int V = 0;
            vector<Str> ss(8 * N + 1);
            vector<vector<int>> xss(N);
            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) {
                vector<pair<int, int>> ps;
                for (const int u: uss[d]) {
                    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());
                    const int V0 = V;
                    for (const auto &p: ps) {
                        const int x = V++;
                        ss[x] = Str{ss[p.first].head, ss[p.second].tail, p.first, p.second};
                        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 = "~";
            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) {
                        const int a = ss[y].head;
                        if (!tr[a]) tr[a] = c++;
                        s[i] = tr[a];
                        y = ss[y].suf;
                    }
                }
                chmin(ans, s);
            }
            puts(ans.c_str());
        }
#ifndef LOCAL
        break;
#endif
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
aba
aaaa

result:

wrong answer 3rd lines differ - expected: 'aaba', found: 'aaaa'