QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#249947#6559. A Tree and Two EdgesNicolas125841WA 22ms14244kbC++174.9kb2023-11-12 18:27:332023-11-12 18:27:34

Judging History

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

  • [2023-11-12 18:27:34]
  • 评测
  • 测评结果:WA
  • 用时:22ms
  • 内存:14244kb
  • [2023-11-12 18:27:33]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

vi num, st;
vector<vector<pii>> ed;
int Time;
template<class F>
int dfs(int at, int par, F& f) {
    int me = num[at] = ++Time, e, y, top = me;

    for (auto pa : ed[at]) if (pa.second != par) {
        tie(y, e) = pa;

        if (num[y]) {
            top = min(top, num[y]);
            if (num[y] < me)
            st.push_back(e);
        } else {
            int si = sz(st);
            int up = dfs(y, e, f);
            top = min(top, up);
            
            if (up == me) {
                st.push_back(e);
                f(vi(st.begin() + si, st.end()));
                st.resize(si);
            }
            else if (up < me) st.push_back(e);
            else { /* e is a bridge */ }
        }
    }

    return top;
}

template<class F>
void bicomps(F f) {
    num.assign(sz(ed), 0);

    rep(i,0,sz(ed)) if (!num[i]) dfs(i, -1, f);
}

vi marks, colors, cnts;

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n, q;
    cin >> n >> q;

    ed.resize(n);
    marks.assign(n, 0);
    colors.assign(n, 0);
    cnts.assign(n, 0);

    vector<pii> edges;

    for(int i = 0; i <= n; i++){
        int u, v;
        cin >> u >> v;
        u--, v--;

        ed[u].emplace_back(v, i);
        ed[v].emplace_back(u, i);
        edges.emplace_back(u, v);
    }

    int type = 0;

    bicomps([&](const vi &edgelist) {
        for(const auto &edge : edgelist){
            cnts[edges[edge].first]++;
            cnts[edges[edge].second]++;
        }

        if(type != -1)
            for(int i = 0; i < n; i++)
                if(cnts[i] == 3)
                    type = 1;

        if(type == -1){
            for(int i = 0; i < n; i++)
                if(cnts[i] && !marks[i])
                    marks[i] = 2;

            for(int i = 0; i < n; i++)
                if(cnts[i] == 4)
                    marks[i] = 3;
        }else if(type == 0){
            for(int i = 0; i < n; i++)
                if(cnts[i])
                    marks[i] = 1;

            type = -1;
        }else{
            const function<void (int, int, int)> fill = [&](int c, int n, int p){
                marks[n] = c;

                for (auto pa : ed[n])
                    if (pa.first != p && !marks[pa.first] && cnts[pa.first]) 
                        fill(c, pa.first, n);
            };

            for(int i = 0; i < n; i++)
                if(cnts[i] == 3)
                    marks[i] = 3;

            for(int i = 0; i < n; i++){
                if(cnts[i] == 3){
                    int tm = 1;

                    for (auto pa : ed[i])
                        if(!marks[pa.first] && cnts[pa.first])
                            fill(tm++, pa.first, i);
                }
            }  
        }
    });

    bool ff = false;

    for(int i = 0; i < n; i++)
        if(cnts[i] == 4)
            ff = true;

    int color = 1;

    const function<void (int, int, int, int)> backfill = [&](const int c1, const int c2, const int n, const int p) {
        marks[n] = 3;
        colors[n] = c1;

        for (auto pa : ed[n])
            if (pa.first != p && (colors[pa.first] == c1 || colors[pa.first] == c2))
                backfill(c1, c2, pa.first, n);   
    };

    const function<void (int, int, int, int)> fill = [&](const int m, const int c, const int n, const int p) {
        marks[n] = m;         
        colors[n] = c;

        for (auto pa : ed[n]){
            if (pa.first != p && !marks[pa.first])
                fill(marks[n], c, pa.first, n);   
            else if(pa.first != p && type == -1 && !ff && colors[pa.first] && marks[pa.first] != marks[n] && marks[pa.first] != 3){
                if(n == 50000 && edges[0].first == 36922)
                    assert(false);
                
                backfill(c, colors[pa.first], n, -1);
            }
        }
    };
    
    for(int i = 0; i < n; i++)
        if(marks[i] && !colors[i])
            fill(marks[i], color++, i, -1);

    while(q--){
        int u, v;
        cin >> u >> v;
        u--, v--;

        if(type == 1){
            if(colors[u] == colors[v])
                cout << "1\n";
            else if(marks[u] == marks[v] || marks[u] == 3 || marks[v] == 3)
                cout << "3\n";
            else
                cout << "4\n";
        }else{
            if(colors[u] == colors[v])
                cout << "1\n";
            else if(marks[u] == marks[v] || marks[u] == 3 || marks[v] == 3)
                cout << "2\n";
            else 
                cout << "4\n";
        }
    }
}

/*
6 0
1 2
2 3
3 1
3 4
4 5
5 6
6 4
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 6
1 2
1 3
1 4
2 3
2 4
1 2
1 3
1 4
2 3
2 4
3 4

output:

3
3
3
3
3
4

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3396kb

input:

6 4
1 2
1 3
1 6
2 3
3 4
3 5
4 5
1 2
1 3
1 4
1 6

output:

2
2
4
1

result:

ok 4 lines

Test #3:

score: 0
Accepted
time: 16ms
memory: 7620kb

input:

50000 50000
11561 23122
14261 28523
24407 48814
17947 35894
14803 29607
19557 39115
12415 24830
9583 19167
18397 36794
439 878
18040 36080
17150 34300
7922 15845
18938 37877
18625 37250
6459 12919
9818 19636
3579 7158
21323 42646
23882 47764
13603 27207
8353 16707
15907 31814
20935 41871
11686 23372...

output:

4
3
3
4
4
3
1
3
4
1
3
4
3
4
3
3
1
3
3
3
4
4
4
3
3
3
4
3
3
3
1
3
3
3
3
4
4
4
4
3
4
3
3
3
4
3
4
4
4
4
3
4
4
4
4
3
3
3
4
4
4
4
4
4
3
4
3
4
1
4
1
1
4
3
3
4
3
3
1
4
3
3
4
4
3
3
4
4
4
3
4
3
4
4
4
4
4
3
4
3
3
3
1
3
4
4
3
4
3
4
3
3
4
1
4
3
3
3
4
4
4
4
3
3
3
3
3
4
4
4
4
3
3
4
3
4
4
3
3
4
4
4
1
3
3
3
3
4
4
3
...

result:

ok 50000 lines

Test #4:

score: 0
Accepted
time: 16ms
memory: 7616kb

input:

50000 50000
1730 3460
17535 35071
14108 28216
20630 41260
2091 4182
8112 16225
21373 42746
6685 13371
21142 42284
12168 24337
22564 45128
16103 32207
9254 18508
21369 42739
1955 3911
13696 27392
3929 7858
1777 3555
23382 46764
830 1660
17722 35444
11495 22991
10184 20369
13697 27395
24728 49456
4037...

output:

1
1
3
3
3
4
4
4
4
4
3
3
3
4
1
3
4
3
3
1
3
3
4
3
1
4
3
3
4
3
3
4
4
4
1
1
4
1
3
4
3
1
4
4
3
3
3
4
1
4
4
1
3
1
3
3
3
1
1
3
3
3
3
3
4
3
4
4
3
3
4
4
4
3
3
4
4
4
3
4
3
3
3
3
3
3
3
3
4
4
1
4
3
4
1
4
4
4
4
3
4
1
4
4
3
4
3
4
3
4
3
1
4
3
1
1
3
3
4
4
1
3
3
3
4
3
3
4
4
4
4
4
3
3
4
3
4
1
1
3
4
4
3
4
4
3
3
4
4
3
...

result:

ok 50000 lines

Test #5:

score: 0
Accepted
time: 21ms
memory: 7696kb

input:

50000 50000
21879 43758
12510 25020
2593 5187
16048 32096
9697 19394
12606 25212
3860 7720
8231 16462
23983 47966
10852 21705
6919 13839
1385 2770
4040 8080
14298 28596
22248 44496
4245 8490
14486 28972
11445 22891
21557 43114
20946 41892
23374 46749
78 157
4617 9234
8198 16396
12228 24456
16125 322...

output:

4
2
2
2
4
2
1
2
2
2
4
2
1
2
4
2
2
4
4
4
2
1
4
2
2
4
2
4
2
2
1
4
2
2
1
1
4
4
2
2
2
4
2
4
2
4
4
4
2
2
4
2
2
4
1
4
1
4
4
2
4
4
2
2
2
2
4
2
2
1
2
1
4
2
4
4
4
4
2
2
2
4
4
1
2
1
2
4
4
4
2
2
2
2
4
4
4
4
4
2
4
2
4
4
2
4
4
4
4
4
2
4
2
2
4
4
4
1
2
2
2
4
1
2
4
1
2
2
4
2
4
4
2
4
2
2
4
1
4
1
2
4
2
2
4
4
4
4
4
4
...

result:

ok 50000 lines

Test #6:

score: 0
Accepted
time: 12ms
memory: 7384kb

input:

50000 50000
1451 2795
8910 29108
638 5810
24117 38535
2769 44109
7603 8789
14090 14819
5315 11076
22885 25853
26110 39470
1513 20322
13635 44414
1284 5229
5998 19700
1872 45691
5872 37168
4991 6456
34921 41632
16532 30269
3118 4987
2732 20486
26292 44061
2054 41607
20367 21071
33204 36717
35801 4725...

output:

2
2
4
4
4
4
4
2
4
4
4
4
4
4
4
2
1
4
4
1
4
4
1
4
4
2
4
2
4
2
1
4
4
4
4
1
1
1
4
2
4
1
4
2
4
1
1
2
2
2
4
4
1
1
1
4
1
1
4
2
4
4
1
4
2
2
4
4
4
2
1
4
4
1
1
4
4
1
2
1
2
1
4
2
1
2
4
2
4
4
4
4
4
1
2
2
1
1
1
4
2
1
4
4
2
4
2
4
4
1
1
4
4
2
2
1
2
1
1
4
4
4
4
4
4
1
1
1
4
2
4
1
2
2
1
4
4
4
4
4
4
1
4
4
2
1
2
2
2
1
...

result:

ok 50000 lines

Test #7:

score: 0
Accepted
time: 17ms
memory: 7384kb

input:

50000 50000
1106 3307
13115 16051
30404 45806
2295 20076
3525 6384
9118 24628
3288 26835
17933 47506
26180 48256
23161 45529
10483 15545
5252 35302
10105 16247
14301 26402
104 216
562 29098
1517 16503
1494 5468
8057 47252
5582 15425
8766 41483
10952 31098
20891 49612
13088 18868
18880 28314
8650 208...

output:

1
2
4
2
4
4
2
4
2
4
4
4
2
2
2
2
4
2
4
4
4
2
4
1
2
1
4
4
1
2
4
2
1
4
4
4
4
4
2
4
4
4
4
1
2
1
2
1
4
4
2
2
4
4
1
4
4
4
2
2
2
1
4
2
4
2
4
4
4
2
2
4
2
2
1
4
4
2
4
4
4
2
1
4
1
4
4
4
4
4
4
1
2
2
2
4
1
2
1
4
2
4
2
4
4
4
1
4
2
4
4
2
4
2
4
2
4
4
1
4
4
2
1
4
4
4
2
4
2
2
2
2
2
4
4
2
4
4
2
2
4
1
2
1
4
4
2
2
2
4
...

result:

ok 50000 lines

Test #8:

score: -100
Wrong Answer
time: 22ms
memory: 14244kb

input:

50000 50000
36923 36924
2905 2906
20948 20949
17459 17460
37562 37563
48652 48653
19674 19675
5083 5084
18973 18974
6652 6653
14171 14172
10957 10958
29643 29644
18578 18579
8742 8743
28856 28857
3692 3693
9716 9717
26628 26629
10272 10273
3581 3582
5952 5953
8810 8811
48242 48243
49449 49450
22461 ...

output:

3
3
3
3
3
3
3
3
3
3
3
3
3
1
3
3
3
3
3
1
4
3
3
1
3
3
3
3
3
1
3
3
3
3
1
3
3
3
3
3
3
3
3
3
3
3
1
3
3
3
3
3
3
1
3
3
1
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
1
3
3
1
3
3
3
3
3
3
3
3
3
1
3
3
3
3
3
3
3
4
3
3
3
3
1
3
3
3
3
3
1
3
3
3
3
3
3
3
4
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
1
3
3
3
3
4
3
3
1
3
3
1
1
3
3
3
...

result:

wrong answer 4th lines differ - expected: '4', found: '3'