UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#214594#2681. How Many Paths?N1001748ms20356kbC++113.1kb2024-11-20 19:36:372024-11-20 23:03:09

answer

//
//  na 2681.cpp
//  Competitive Programming
//
//  Created by m2 on 2024/11/20.
//

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
#ifdef MAC_OS_VERSION_11_0
const int maxn = 105;
#else
const int maxn = 1e5 + 5;
#endif
const int maxm = 5e5 + 5;
int n, m, u[maxm], v[maxm];
inline void qread(int &var){
    var = 0;
    char reg = getchar();
    while(reg - 48 > 9u)
        reg = getchar();
    do{
        var = (var << 1) + (var << 3) + (reg ^ 48);
        reg = getchar();
    }while(reg - 48 < 10u);
}
vector<int> to[maxn], t2[maxn];
int st[maxn], stl, dfn[maxn], low[maxn], scc[maxn], sccsiz[maxn], sccn, cl;
bool inst[maxn];
void tarjan(int idx){
    inst[idx] = true;
    dfn[idx] = low[idx] = ++cl;
    st[++stl] = idx;
    for(int nxt: to[idx]){
        if(!dfn[nxt]){
            tarjan(nxt);
            low[idx] = min(low[idx], low[nxt]);
        }else{
            if(inst[nxt])
                low[idx] = min(low[idx], dfn[nxt]);
        }
    }
    if(dfn[idx] == low[idx]){
        ++sccn;
        do{
            inst[st[stl]] = false;
            scc[st[stl--]] = sccn;
            ++sccsiz[sccn];
        }while(st[stl + 1] != idx);
    }
}
bool mult[maxn];
void fill(int idx){
    if(mult[idx])
        return;
    mult[idx] = true;
    for(int nxt: t2[idx])
        fill(nxt);
}
int sol[maxn], que[maxn], qs, qe, deg[maxn];
inline void topo(){
    for(int i = 1; i <= n; ++i)
        for(int j: t2[i])
            ++deg[j];
    que[qe++] = scc[1];
    sol[scc[1]] = 1;
    int cur;
    while(qs < qe){
        cur = que[qs++];
        for(int nxt: t2[cur]){
            sol[nxt] = min(2, sol[nxt] + sol[cur]);
            if(!--deg[nxt])
                que[qe++] = nxt;
        }
    }
}
int main(){
    cin.tie(0)->sync_with_stdio(false);
    qread(n); qread(m);
    for(int i = 0; i < m; ++i){
        qread(u[i]); qread(v[i]);
        to[u[i]].push_back(v[i]);
    }
    tarjan(1);
//    cerr << "scc: ";
//    for(int i = 1; i <= n; ++i)
//        cerr << scc[i] << ' ';
//    cerr << endl;
    for(int i = 0; i < m; ++i)
        if(dfn[u[i]] && dfn[v[i]] && scc[u[i]] != scc[v[i]]){
            t2[scc[u[i]]].push_back(scc[v[i]]);
//            cerr << scc[u[i]] << " -> " << scc[v[i]] << endl;
        }
    for(int i = 1; i <= n; ++i)
        if(sccsiz[scc[i]] > 1)
            fill(scc[i]);
//    cerr << "mult: ";
//    for(int i = 1; i <= n; ++i)
//        cerr << boolalpha << mult[scc[i]] << noboolalpha << " ";
//    cerr << endl;
    topo();
//    cerr << "sol: ";
//    for(int i = 1; i <= n; ++i)
//        cerr << sol[scc[i]] << ' ';
//    cerr << endl;
    for(int i = 1; i <= n; ++i){
        if(!dfn[i]){
//            cout << 0 << ' ';
            putchar(48);
        }else if(mult[scc[i]]){
//            cout << 3 << ' ';
            putchar(51);
        }else{
//            cout << sol[scc[i]] << ' ';
            putchar(sol[scc[i]] ^ 48);
        }
        putchar(' ');
    }
    putchar('\n');
    fflush(stdout);
    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 94ms
memory: 15700kb

input:

50000 500000
27433 23782
1927 1159
20346 8794
27393 18828
40557 32372
29462 34217
31695 6752
2253 19...

output:

1 2 0 2 0 0 2 0 2 2 0 2 0 2 2 0 0 2 2 0 2 2 0 2 2 0 1 2 2 2 0 2 0 0 2 2 0 2 0 0 2 2 2 1 0 2 0 0 2 0 ...

result:

ok single line: '1 2 0 2 0 0 2 0 2 2 0 2 0 2 2 ... 0 1 1 2 2 0 2 0 2 2 0 0 0 0 2 '

Test #2:

score: 5
Accepted
time: 87ms
memory: 14124kb

input:

50000 500000
30894 37863
18739 43205
26461 20214
15741 957
38985 43365
15978 10327
7414 35300
17146 ...

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok single line: '1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '

Test #3:

score: 5
Accepted
time: 105ms
memory: 14956kb

input:

50000 500000
38020 5057
495 7556
33072 22521
28583 41495
7199 42249
37197 26482
34166 16408
33429 19...

output:

1 0 2 0 0 2 0 0 0 2 2 2 2 0 0 0 0 2 2 2 0 2 2 0 0 2 0 0 0 0 0 0 0 0 2 0 0 0 0 2 0 0 0 2 0 2 2 0 1 0 ...

result:

ok single line: '1 0 2 0 0 2 0 0 0 2 2 2 2 0 0 ... 0 0 2 1 0 0 0 0 0 0 2 0 2 1 0 '

Test #4:

score: 5
Accepted
time: 109ms
memory: 15936kb

input:

50000 500000
9177 925
34439 28325
7099 9560
21204 48849
36028 23762
6297 46964
38036 31855
6292 1832...

output:

1 2 2 2 0 2 0 2 2 0 1 0 2 2 2 0 2 0 2 0 0 2 2 2 0 2 0 0 2 2 0 2 2 0 0 2 0 0 2 2 2 0 0 0 2 0 2 0 2 2 ...

result:

ok single line: '1 2 2 2 0 2 0 2 2 0 1 0 2 2 2 ... 1 0 0 0 2 0 0 1 0 2 2 0 2 2 0 '

Test #5:

score: 5
Accepted
time: 79ms
memory: 14632kb

input:

50000 500000
30632 1595
20715 35227
44119 45259
47444 36877
6942 14652
2864 18557
42367 19431
38615 ...

output:

1 2 0 0 0 0 0 2 0 0 0 0 0 0 2 2 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 2 0 0 0 0 0 0 0 0 ...

result:

ok single line: '1 2 0 0 0 0 0 2 0 0 0 0 0 0 2 ... 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '

Test #6:

score: 5
Accepted
time: 79ms
memory: 14540kb

input:

50000 500000
33040 23079
35743 21197
49128 38373
36656 46198
17910 29433
45170 24921
28618 34080
100...

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 2 2 0 0 0 0 0 0 0 0 0 2 0 0 0 1 0 0 0 0 0 0 2 ...

result:

ok single line: '1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 '

Test #7:

score: 5
Accepted
time: 74ms
memory: 14412kb

input:

50000 500000
5226 27470
49597 10290
40326 34407
17000 37084
26244 45989
48273 4706
15981 19231
26219...

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok single line: '1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '

Test #8:

score: 5
Accepted
time: 93ms
memory: 15004kb

input:

50000 500000
16952 41617
2923 24354
4446 41320
8355 41065
23930 48553
5990 23289
30279 14635
16174 1...

output:

1 0 2 2 2 2 0 0 0 2 1 0 0 2 0 0 0 0 0 2 2 0 2 0 1 0 0 2 0 1 2 0 2 0 2 0 2 2 0 0 0 2 2 2 2 0 0 0 2 2 ...

result:

ok single line: '1 0 2 2 2 2 0 0 0 2 1 0 0 2 0 ... 0 0 2 2 2 0 0 1 2 1 0 2 0 2 0 '

Test #9:

score: 5
Accepted
time: 104ms
memory: 16184kb

input:

50000 500000
40140 17583
24758 1389
12331 1829
43385 23986
38136 28093
21088 33706
44519 38558
39908...

output:

1 2 0 2 2 0 0 2 0 1 2 2 2 2 2 2 0 2 0 2 2 0 2 0 2 2 0 0 2 0 2 2 0 0 2 2 0 2 2 2 2 2 2 2 2 2 2 0 0 2 ...

result:

ok single line: '1 2 0 2 2 0 0 2 0 1 2 2 2 2 2 ... 2 2 1 0 0 0 2 0 0 2 0 0 2 0 2 '

Test #10:

score: 5
Accepted
time: 81ms
memory: 14236kb

input:

50000 500000
26128 23615
14751 3541
9682 26585
39984 12641
32407 34611
40404 13491
49939 11363
19404...

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok single line: '1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '

Test #11:

score: 5
Accepted
time: 78ms
memory: 20304kb

input:

100000 500000
1 2
1 3
1 8
1 10
1 11
1 17
1 48
1 100
1 284
1 3021
1 7857
1 16097
1 18352
1 17333
1 81...

output:

1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 1 1 1 ...

result:

ok single line: '1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 ... 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 '

Test #12:

score: 5
Accepted
time: 86ms
memory: 20296kb

input:

100000 500000
1 2
1 4
1 5
1 7
1 8
1 13
1 15
1 587
1 6125
1 8728
1 1965
1 12132
1 8992
1 17819
1 1358...

output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

result:

ok single line: '1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... 3 3 3 3 3 3 3 3 3 3 3 0 0 3 3 '

Test #13:

score: 5
Accepted
time: 91ms
memory: 20292kb

input:

100000 500000
1 2
1 6
1 45
1 68
1 79
1 496
1 546
1 1119
1 1510
1 1665
1 3262
1 4727
1 4879
1 5430
1 ...

output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

result:

ok single line: '1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 '

Test #14:

score: 5
Accepted
time: 94ms
memory: 20356kb

input:

100000 500000
1 2
1 4
1 8
1 16
1 28
1 58
1 85
1 96
1 106
1 241
1 3315
1 3551
1 13
1 1969
1 12587
1 1...

output:

1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

result:

ok single line: '1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 ... 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 '

Test #15:

score: 5
Accepted
time: 80ms
memory: 20308kb

input:

100000 500000
1 2
1 7
1 13
1 15
1 18
1 74
1 277
1 652
1 1116
1 8926
1 9947
1 12793
1 17696
1 1742
1 ...

output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

result:

ok single line: '1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 '

Test #16:

score: 5
Accepted
time: 75ms
memory: 20276kb

input:

100000 500000
1 2
1 4
1 5
1 7
1 8
1 21
1 26
1 29
1 31
1 254
1 385
1 451
1 678
1 1278
1 2323
1 3775
1...

output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 ...

result:

ok single line: '1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 '

Test #17:

score: 5
Accepted
time: 95ms
memory: 20300kb

input:

100000 500000
1 2
1 15
1 44
1 60
1 183
1 365
1 582
1 1768
1 1793
1 6193
1 9585
1 12293
1 447
1 9529
...

output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 ...

result:

ok single line: '1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 '

Test #18:

score: 5
Accepted
time: 72ms
memory: 20308kb

input:

100000 500000
1 2
1 3
1 4
1 65
1 103
1 282
1 3710
1 5403
1 7559
1 4446
1 5382
1 5028
1 18244
1 5690
...

output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 2 1 1 1 2 1 1 1 1 1 2 ...

result:

ok single line: '1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 '

Test #19:

score: 5
Accepted
time: 81ms
memory: 20292kb

input:

100000 500000
1 2
1 6
1 310
1 510
1 643
1 667
1 4289
1 13736
1 15465
1 7212
1 8416
1 8424
1 11633
1 ...

output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 1 2 2 2 1 1 ...

result:

ok single line: '1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 '

Test #20:

score: 5
Accepted
time: 91ms
memory: 20288kb

input:

100000 500000
1 2
1 4
1 6
1 7
1 10
1 16
1 247
1 433
1 540
1 2050
1 2671
1 13116
1 9660
1 18620
1 107...

output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 ...

result:

ok single line: '1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 '