QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#249844#6559. A Tree and Two EdgesNicolas125841WA 20ms7724kbC++174.7kb2023-11-12 16:32:452023-11-12 16:32:45

Judging History

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

  • [2023-11-12 16:32:45]
  • 评测
  • 测评结果:WA
  • 用时:20ms
  • 内存:7724kb
  • [2023-11-12 16:32:45]
  • 提交

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;

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);

    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) {
        vector<int> cnts(n, 0);

        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 = 2;
                else if(cnts[i] == 4)
                    type = 1;
            }
        }

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

            type = -1;
        }else if(type == 1){
            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]) 
                        fill(c, pa.first, n);   
            };

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

                    int tm = 1;

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

            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);
                }
            }  
        }
    });

    int color = 1;

    const function<void (int, int, int)> fill = [&](const int c, const int n, const int p) {
        if(colors[n] != 0)
            colors[n] = -1;
        else    
            colors[n] = c;

        for (auto pa : ed[n])
            if (pa.first != p && !marks[pa.first])
                fill(c, pa.first, n);   
    };
    
    for(int i = 0; i < n; i++)
        if(marks[i])
            fill(color++, i, -1);

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

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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: -100
Wrong Answer
time: 20ms
memory: 7724kb

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:

3
3
3
3
3
3
1
3
3
1
3
3
3
3
3
3
1
3
3
3
3
3
3
3
3
3
3
3
3
3
1
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
1
3
1
1
3
3
3
3
3
3
1
3
3
3
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
3
3
3
3
3
3
1
3
3
3
3
3
3
3
3
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
3
3
3
...

result:

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