QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#640434#7738. Equivalent Rewritingucup-team1769WA 0ms3588kbC++201.6kb2024-10-14 12:28:302024-10-14 12:28:31

Judging History

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

  • [2024-10-14 12:28:31]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3588kb
  • [2024-10-14 12:28:30]
  • 提交

answer

#include <bits/stdc++.h>

using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;

constexpr i64 inf = 1e18;

void solve() {
    int n, m;
    std::cin >> n >> m;

    std::vector adj(n, std::vector<int>()), nxt(n, std::vector<int>());
    std::vector<int> v(m, -1), in(n, 0);
    for (int i = 0; i < n; i++) {
        int p;
        std::cin >> p;
        adj[i].resize(p);
        for (int j = 0; j < p; j++) {
            std::cin >> adj[i][j];
            if (v[adj[i][j] - 1] != -1) {
                nxt[v[adj[i][j] - 1]].emplace_back(i);
                in[i]++;
            }
            v[adj[i][j] - 1] = i;
        }
    }

    std::vector<int> ans, vis(n, 0);
    auto dfs = [&](auto &&self, int x) -> void {
        if (!vis[x]) {
            vis[x] = 1;
            ans.emplace_back(x);
            for (int i = nxt[x].size() - 1; i >= 0; i--) {
                int y = nxt[x][i];
                // std::cout << x << " " << y << "\n";
                self(self, y);
            }
        }
    };

    for (int i = n - 1; i >= 0; i--) {
        if (!vis[i] && !in[i]) {
            dfs(dfs, i);
        }
    }

    std::vector<int> res(n);
    std::iota(res.begin(), res.end(), 0);
    if (res == ans) {
        std::cout << "No\n";
        return;
    }

    std::cout << "Yes\n";
    for (auto x : ans) {
        std::cout << x + 1 << " ";
    }
    std::cout << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3588kb

input:

3
3 6
3 3 1 5
2 5 3
2 2 6
2 3
3 1 3 2
2 3 1
1 3
2 2 1

output:

Yes
3 1 2 
No
No

result:

ok OK. (3 test cases)

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3552kb

input:

1
10 5
2 2 4
4 1 3 4 2
1 2
3 2 1 4
4 5 2 4 3
3 2 5 4
3 5 4 2
3 1 3 2
5 1 4 2 3 5
1 4

output:

Yes
1 2 5 8 9 10 6 7 4 3 

result:

wrong answer two transactions are not equivalent. (test case 1)