QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#698225#8140. Customs Controls 2lichenyu_acRE 4ms9972kbC++141.6kb2024-11-01 18:15:472024-11-01 18:15:51

Judging History

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

  • [2024-11-01 18:15:51]
  • 评测
  • 测评结果:RE
  • 用时:4ms
  • 内存:9972kb
  • [2024-11-01 18:15:47]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10,M=N*2;

int head[N],ver[M],nxt[M],tot=1;
void add(int x,int y){
    ver[++tot]=y,nxt[tot]=head[x],head[x]=tot;
}

int n,m;
int deg[N],val[N];

struct DSU{
    int fa[N];
    void init(){
        for(int i=1;i<=n*2;i++)fa[i]=i;
    }
    int find(int x){
        return fa[x]==x?x:fa[x]=find(fa[x]);
    }
}DSU;

void clear(){
    for(int i=1;i<=n*2;i++)
        head[i]=deg[i]=0;
    tot=1;
}

void topsort(){
    queue<int>q;
    for(int i=1;i<=n*2;i++)
        if(!deg[i])q.emplace(i),val[i]=1;
        else val[i]=0;
    while(q.size()){
        int x=q.front();q.pop();
        for(int i=head[x];i;i=nxt[i]){
            int y=ver[i];
            val[y]=max(val[y],val[x]+1);
            if(--deg[y]==0)q.emplace(y);
        }
    }
}

void solve(){
    scanf("%d%d",&n,&m);
    DSU.init(),clear();
    for(int i=1;i<=m;i++){
        int x,y;scanf("%d%d",&x,&y);
        int fx=DSU.find(x+n),fy=DSU.find(y);
        if(fx!=fy)DSU.fa[fx]=fy;
    }
    for(int i=1;i<=n;i++){
        if(DSU.find(i)==DSU.find(i+n))
            return puts("No"),void();
    }
    for(int i=1;i<=n;i++){
        int x=DSU.find(i),y=DSU.find(i+n);
        add(y,x),deg[x]++;
    }
    topsort();
    for(int i=1;i<=n*2;i++){
        if(deg[i])return puts("No"),void();
    }
    puts("Yes");
    for(int i=1;i<=n;i++){
        int x=DSU.find(i),y=DSU.find(i+n);
        printf("%d%c",val[x]-val[y]," \n"[i==n]);
    }
}

int main(){
    int T;scanf("%d",&T);
    while(T--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
3 3
1 2
1 3
2 3
8 9
1 2
1 3
1 4
2 5
3 6
4 7
5 8
6 8
7 8

output:

No
Yes
1 1 1 1 1 1 1 1

result:

ok ok (2 test cases)

Test #2:

score: 0
Accepted
time: 1ms
memory: 7896kb

input:

2
11 16
1 2
1 3
1 4
1 5
2 6
4 6
3 7
4 7
5 8
6 8
2 9
3 9
7 10
8 10
9 11
10 11
8 10
1 2
1 3
2 4
3 5
3 6
4 6
2 7
5 7
6 8
7 8

output:

Yes
1 1 1 1 2 1 2 1 3 1 1
No

result:

ok ok (2 test cases)

Test #3:

score: 0
Accepted
time: 1ms
memory: 7800kb

input:

1
8 10
1 2
1 3
2 4
3 5
3 6
4 6
2 7
5 7
6 8
7 8

output:

No

result:

ok ok (1 test case)

Test #4:

score: 0
Accepted
time: 1ms
memory: 7956kb

input:

1
11 16
1 2
1 3
1 4
1 5
2 6
4 6
3 7
4 7
5 8
6 8
2 9
3 9
7 10
8 10
9 11
10 11

output:

Yes
1 1 1 1 2 1 2 1 3 1 1

result:

ok ok (1 test case)

Test #5:

score: 0
Accepted
time: 1ms
memory: 6044kb

input:

1
3 3
1 2
1 3
2 3

output:

No

result:

ok ok (1 test case)

Test #6:

score: 0
Accepted
time: 1ms
memory: 9972kb

input:

1
15 24
1 3
1 7
1 6
1 12
3 11
3 5
3 13
3 14
3 8
7 11
7 13
7 14
11 2
11 10
6 9
5 9
13 9
14 4
8 4
2 4
10 4
9 4
12 15
4 15

output:

Yes
1 1 1 1 1 2 1 2 1 1 1 4 1 2 1

result:

ok ok (1 test case)

Test #7:

score: 0
Accepted
time: 1ms
memory: 7932kb

input:

10
20 40
1 8
1 11
1 19
8 7
8 16
8 15
8 14
8 4
8 17
11 5
11 6
11 2
11 3
7 6
7 2
7 3
16 9
16 12
15 9
15 12
14 9
4 18
4 10
5 13
5 10
6 18
6 10
2 13
2 18
3 18
3 10
9 13
9 10
12 13
12 10
19 20
17 20
13 20
18 20
10 20
20 30
8 19
19 12
5 12
5 10
10 4
18 4
18 14
14 6
15 6
15 7
7 3
17 3
17 2
2 16
9 16
9 11
1...

output:

Yes
1 1 1 2 1 1 1 1 1 1 2 1 1 1 1 1 3 1 4 1
No
Yes
1 1 1 4 4 1 2 1 2 2 3 3 4 2 2 1 1 1 1 1
Yes
1 3 4 4 3 1 2 5 3 1 1 1 2 1 1 1 2 1 2 1
Yes
1 3 1 1 1 1 3 1 1 1 3 1 1 2 2 3 1 1 1 1
Yes
1 1 1 1 3 1 1 1 1 2 1 1 3 4 1 3 2 1 1 1
Yes
1 2 1 1 2 2 1 1 2 3 2 1 1 3 1 2 5 1 1 1
No
Yes
1 1 3 1 3 2 2 1 1 1 3 2 3 ...

result:

ok ok (10 test cases)

Test #8:

score: 0
Accepted
time: 4ms
memory: 7992kb

input:

10
1919 3195
1888 1186
1186 519
1514 519
1514 859
859 1634
977 1634
977 185
185 1250
1103 1250
1103 463
463 1683
426 1683
426 1728
1728 1402
1612 1402
1612 1789
1789 857
586 857
586 1669
1669 1376
1833 1376
1833 1076
1076 749
733 749
733 551
551 217
1717 217
1717 862
862 319
96 319
96 479
479 1381
1...

output:

No
Yes
1 26 1 11 13 4 18 15 15 18 30 5 23 3 12 7 1 24 18 19 34 13 14 7 1 1 1 23 5 14 2 7 29 1 6 28 20 18 20 20 1 25 7 23 21 12 24 3 11 12 7 1 40 8 3 31 1 19 12 10 10 15 17 16 5 36 11 1 13 18 13 3 18 4 36 34 35 37 8 27 13 33 5 29 13 9 28 23 33 7 9 13 21 3 7 10 11 8 6 1 6 12 36 7 16 13 8 2 1 34 12 8 2...

result:

ok ok (10 test cases)

Test #9:

score: -100
Runtime Error

input:

1
181547 488264
1 172537
1 90998
1 88110
1 96832
1 114889
1 33910
1 88129
1 70671
1 63339
1 48928
1 87572
1 34438
1 159256
1 173984
1 91374
1 89583
1 47960
1 93777
1 44079
1 132241
1 85083
1 99617
1 160839
1 157126
1 178514
1 70706
1 13530
1 168869
1 29354
1 11630
1 123518
1 86921
1 19627
1 126118
1...

output:


result: