QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#644733#7738. Equivalent RewritingAkTttWA 0ms3624kbC++141.9kb2024-10-16 15:18:062024-10-16 15:18:06

Judging History

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

  • [2024-10-16 15:18:06]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3624kb
  • [2024-10-16 15:18:06]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <map>
#include <queue>

using namespace std;
#define endl '\n'
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
const LL INFF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f,mod = 998244353;
#define debug(x) cout<< "[debug]" #x << " = " << x <<endl

int n, m;

void solve()
{
    cin >> n >> m;
    vector<int> v[n + 1], id(m + 1);
    vector<int> cnt(n + 1);
    map<PII, int> ma;

    for(int i = 1; i <= n; i++){
    	int len;
    	cin >> len;
    	for(int j = 1; j <= len; j++){
    		int x;
    		cin >> x;
    		if(!id[x])id[x] = i;
    		else{
    			if(!ma.count({id[x], i})){
    				v[id[x]].push_back(i);
    				ma[{id[x], i}] = 1;
    				cnt[i]++;
    			}
    		}
    	}
    }

    auto TopSort = [&](){
    	priority_queue<int, vector<int>, less<int>> q;
    	vector<int> d;
    	for(int i = 1; i <= n; i++){
    		if(!cnt[i]){
    			q.push(i);
    		}
    	}

    	while(q.size()){
    		auto t = q.top();
    		q.pop();

            d.push_back(t);
    		for(auto k : v[t]){
    			cnt[k]--;
    			if(!cnt[k]){
    				q.push(k);
    			}
    		}
    	}

    	if(d.size() == n){
    		bool ok = false;
    		for(int i = 0; i < n; i++){
    			if((i + 1) != d[i]){
    				ok = true;
    				break;
    			}
    		}

    		if(ok){
    			cout << "Yes" << endl;
                for(int i = 0; i < n; i++){
                    cout << d[i];
                    if(i != n - 1)cout << ' ';
                }
    			cout << endl;
                return ;
    		}
    	}
        cout << "No" << endl;
    };

    TopSort();
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    cin >> T;
    while(T--)
    {
        solve();
    }
}

详细

Test #1:

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

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: 3624kb

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 10 3 2 8 5 9 7 6 4

result:

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