QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#516509#7738. Equivalent Rewritinglearn_cuteWA 12ms99880kbC++141.6kb2024-08-12 17:42:012024-08-12 17:42:01

Judging History

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

  • [2024-08-12 17:42:01]
  • 评测
  • 测评结果:WA
  • 用时:12ms
  • 内存:99880kb
  • [2024-08-12 17:42:01]
  • 提交

answer

#include <bits/stdc++.h>

#define endl "\n"

using namespace std;
using LL = long long;
using PII = pair<int, int>;

const int N = 2e6 + 10;

int idx;
int deg[N];
vector<int> G[N], H[N];
int ans[N], a[N];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i <= max(n, m) + 10; i++)
        H[i].clear(), G[i].clear(), deg[i] = 0;
    idx = 0;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        for (int j = 1; j <= x; j++) {
            int u;
            cin >> u;
            G[u].push_back(i);
        }
    }
    for (int i = 1; i <= m; i++) {
        if (G[i].empty())
            continue;
        int lst = G[i].back();
        for (auto v : G[i]) {
            if (v != lst) {
                H[v].push_back(lst);
                deg[lst]++;
            }
        }
    }
    priority_queue<int> q;
    for (int i = 1; i <= n; i++)
        if (!deg[i])
            q.push(i);
    while (!q.empty()) {
        int u = q.top();
        q.pop();
        ans[++idx] = u;
        for (auto v : H[u]) {
            deg[v]--;
            if (!deg[v])
                q.push(v);
        }
    }
    for (int i = 1; i <= n; i++) {
        if (ans[i] == i) {
            cout << "No\n";
            return;
        }
    }
    assert(idx == n);
    cout << "Yes\n";
    for (int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    cout << endl;
}

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

    int tt; // read(tt);
    cin >> tt;
    while (tt--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 12ms
memory: 99880kb

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: 8ms
memory: 99792kb

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)