QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#633354 | #7738. Equivalent Rewriting | CUHK_723 | WA | 2ms | 9692kb | C++14 | 2.1kb | 2024-10-12 15:09:54 | 2024-10-12 15:09:55 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
char ch = getchar();
while(ch < '0' || ch > '9') {
ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x;
}
int T, n, m;
int a[100010], in[100010], out[100010], arga[100010];
vector<int> f[100010], e[100010];
void solve() {
queue<int> s;
for(int i = 1; i <= n; ++i) {
if(f[i].size() <= 1) continue;
for(int j = 1; j < f[i].size(); ++j) {
int x = f[i][j], y = f[i][j - 1];
swap(x, y);
e[x].push_back(y);
in[y] += 1;
out[x] += 1;
}
}
int num = 0;
queue<int> q;
for(int i = 1; i <= m; ++i) {
if(in[i] == 0 && out[i] == 0) {
s.push(i);
num += 1;
continue;
}
if(in[i] == 0) q.push(i);
}
num = m - num;
int ans = 0;
while(!q.empty()) {
int x = q.front();
q.pop();
ans += 1;
arga[ans] = x;
//cout << e[x].size() << endl;
for(int i = 0; i < e[x].size(); ++i) {
int y = e[x][i];
in[y] -= 1;
if(in[y] == 0) q.push(y);
}
}
//cout << num << " " << ans << endl;
if(num < m && num == ans) {
cout << "Yes" << endl;
int op = 0;
if(s.front() == 1) {
s.pop();
op = 1;
}
while(!s.empty()) {
cout << s.front() << " ";
s.pop();
}
for(int i = 1; i < num; ++i) cout << arga[i] << " ";
cout << arga[num];
if(op == 1) {
cout << " " << 1 << endl;
}
else cout << endl;
}
else if(num != ans) {
cout << "No" << endl;
}
else {
for(int i = 1; i <= num; ++i) {
if(arga[i] != i) {
break;
}
if(i == num) {
cout << "No" << endl;
return;
}
}
for(int i = 1; i < num; ++i) {
cout << arga[i] << " ";
}
cout << arga[num] << endl;
}
}
int main() {
T = read();
while(T) {
T -= 1;
m = read();
n = read();
int num, x;
for(int i = 1; i <= n; ++i) {
in[i] = 0;
out[i] = 0;
a[i] = 0;
f[i].clear();
e[i].clear();
}
for(int i = 1; i <= m; ++i) {
num = read();
for(int j = 1; j <= num; ++j) {
x = read();
a[x] = i;
f[x].push_back(i);
}
}
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 9692kb
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 Yes 0 1
result:
wrong answer Integer parameter [name=q_i] equals to 0, violates the range [1, 1] (test case 3)