QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#594887#7738. Equivalent Rewritingzhangboju#WA 0ms5660kbC++141.7kb2024-09-28 11:00:012024-09-28 11:00:11

Judging History

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

  • [2024-09-28 11:00:11]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:5660kb
  • [2024-09-28 11:00:01]
  • 提交

answer

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

const int MAXN=1e5+10;

struct edge {
    int v,nxt;
}e[MAXN<<1];

int n,m,sz,cnt;
int pre[MAXN],h[MAXN],ind[MAXN],ans[MAXN];
queue<int> q;

void output() {
    for(int i=1;i<=n;i++) {
        printf("%d ",ans[i]);
    }
    puts("");
}

void addedge(int u,int v) {
    e[++cnt].v=v;
    e[cnt].nxt=h[u];
    h[u]=cnt;
    ind[v]++;
}

void solve() {
    cin>>n>>m;
    for(int i=1;i<=n;i++) {
        int k;
        scanf("%d",&k);
        while(k--) {
           int x;
           scanf("%d",&x);
            if(pre[x]) {
                addedge(i,pre[x]);
            }
            pre[x]=i;
        }
    }

    for(int i=1;i<=n;i++) {
        if(ind[i]==0) {
            q.push(i);
            sz++;
        }
    }

    while(!q.empty()) {
        if(sz>1) {
            puts("Yes");
            int u=q.front();
            q.pop();
            int v=q.front();
            q.pop();
            for(int i=1;i<=n;i++) {
                ans[i]=i;
            }
            swap(ans[u],ans[v]);
            output();
            return ;
        }
        
        int u=q.front();
        sz--;
        q.pop();

        for(int i=h[u];i;i=e[i].nxt) {
            int v=e[i].v;
            ind[v]--;
            if(!ind[v]) {
                q.push(v);
                sz++;
            }
        }
    }
    puts("No");
    return ;
}

void clear() {
    for(int i=1;i<=n;i++) {
        ans[i]=ind[i]=h[i]=pre[i]=0;
    }
    while(!q.empty()) {
        q.pop();
    }
    sz=n=m=cnt=0;
}

int main() {
    int t;
    cin>>t;
    while(t--) {
        solve();
        clear();
    }
}

详细

Test #1:

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

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

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)