QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#267761#7738. Equivalent RewritingPetroTarnavskyi#WA 2ms6680kbC++171.5kb2023-11-27 18:10:192023-11-27 18:10:19

Judging History

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

  • [2023-11-27 18:10:19]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:6680kb
  • [2023-11-27 18:10:19]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); i--)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int N = 1 << 17;
VI g[N];
int cnt[N];

void solve()
{
	int n, m;
	cin >> n >> m;
	vector<VI> a(n);
	VI lastInd(m, -1);
	FOR(i, 0, n)
	{
		int sz;
		cin >> sz;
		a[i].resize(sz);
		for(int& pos : a[i])
		{
			cin >> pos;
			pos--;
			lastInd[pos] = i;
		}
	}
	FOR(i, 0, n)
	{
		for(int pos : a[i])
		{
			if(lastInd[pos] == i)
				continue;
			g[i].PB(lastInd[pos]);
			cnt[lastInd[pos]]++;
		}
	}
	queue<int> q;
	FOR(i, 0, n)
		if(cnt[i] == 0)
			q.push(i);
	
	VI ans;
	while(SZ(q))
	{
		int v = q.front();
		ans.PB(v);
		q.pop();
		
		for(int to : g[v])
		{
			cnt[to]--;
			if(cnt[to] == 0)
				q.push(to);
		}
		g[v].clear();
	}
	FOR(i, 0, n)
	{
		assert(SZ(g[i]) == 0);
		assert(cnt[i] == 0);
	}
	bool ok = 0;
	FOR(i, 0, n)
		if(ans[i] != i)
			ok = 1;
	if(ok)
	{
		cout << "Yes\n";
		FOR(i, 0, n)
		{
			if(i > 0)
				cout << " ";
			cout << ans[i] + 1;
		}
		cout << "\n";
	}
	else
	{
		cout << "No\n";
	}
}


int main()
{
	ios::sync_with_stdio(0); 
	cin.tie(0);
	cout << fixed << setprecision(15);
	
	int t;
	cin >> t;
	while(t--)
	{
		solve();
	}

	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 6520kb

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: 2ms
memory: 6680kb

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)