QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#269753#7738. Equivalent RewritingUSI_Bruce12138#WA 0ms3832kbC++201.9kb2023-11-29 22:38:552023-11-29 22:38:55

Judging History

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

  • [2023-11-29 22:38:55]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3832kb
  • [2023-11-29 22:38:55]
  • 提交

answer

#include<bits/stdc++.h>

using ll = long long;
using ld = long double;

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

    std::vector<int> p(n);
    std::vector<std::vector<int>> b(m + 1);

    for (int i = 0; i < n; i++) {
        std::cin >> p[i];
        std::set<int> s;
        for (int j = 0; j < p[i]; j++) {
            int x;
            std::cin >> x;
            s.insert(x);
        }
        p[i] = s.size();
        for (auto x: s) {
            b[x].push_back(i);
        }
    }

    std::vector<int> d(n);
    std::vector<std::vector<int>> g(n);
    for (int i = 1; i <= m; i++) {
        if (b[i].empty()) {
            continue;
        }
        int x = b[i].back();
        for (int j = 0; j + 1 < b[i].size(); j++) {
            g[b[i][j]].push_back(x);
            d[x]++;
        }
    }

    std::queue<int> q;
    for (int i = 0; i < n; i++) {
        if (d[i] == 0) {
            q.push(i);
        }
    }

    std::vector<int> ans;
    while (q.size()) {
        auto u = q.front();
        q.pop();
        ans.push_back(u);
        for (auto v: g[u]) {
            d[v]--;
            if (d[v] == 0) {
                q.push(v);
            }
        }
    }

    for (int i = 0; i < n; i++) {
        if (d[i] != 0) {
            std::cout << "No\n";
            return;
        }
    }

    bool flag = false;
    for (int i = 0; i < n; ++i) {
        if (ans[i] != i)
            flag = true;
    }

    if (!flag) {
        std::cout << "No\n";
        return;
    }

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

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

    int tCase = 1;
    std::cin >> tCase;
    for (int i = 1; i <= tCase; ++i) {
        solve(tCase);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
1 3 2 
No
No

result:

ok OK. (3 test cases)

Test #2:

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

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:

No

result:

wrong answer jury found an answer but participant did not (test case 1)