QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#227354#2766. Unique CitiesCamillus0 212ms36908kbC++205.6kb2023-10-27 13:02:102023-10-27 13:02:10

Judging History

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

  • [2023-10-27 13:02:10]
  • 评测
  • 测评结果:0
  • 用时:212ms
  • 内存:36908kb
  • [2023-10-27 13:02:10]
  • 提交

answer

#include <bits/stdc++.h>

using ll = long long;
using namespace std;

vector<int> g[200500];
int c[200500];

namespace simple_st {

    struct Info {
        int max = -1e7;
        int cnt_max = 0;
        int second_max = -1e7;

        Info() = default;
        Info(int d) {
            max = d;
            cnt_max = 1;
        }
    };

    Info operator+(const Info &A, const Info &B) {
        Info C;
        if (A.max > B.max) {
            C.max = A.max;
            C.cnt_max = A.cnt_max;
            C.second_max = max(A.second_max, B.max);
        } else if (B.max > A.max) {
            C.max = B.max;
            C.cnt_max = B.cnt_max;
            C.second_max = max(B.second_max, A.max);
        } else {
            C.max = A.max;
            C.cnt_max = A.cnt_max + B.cnt_max;
            C.second_max = A.max;
        }
        return C;
    }

    static constexpr int size = 1 << 18;

    Info info[size * 2 - 1];
    int tag[size * 2 - 1];

    void apply(int x, int v) {
        info[x].max += v;
        info[x].second_max += v;
        tag[x] += v;
    }

    void push(int x) {
        if (tag[x]) {
            apply(x * 2 + 1, tag[x]);
            apply(x * 2 + 2, tag[x]);
            tag[x] = 0;
        }
    }

    void set(int i, int v, int x = 0, int lx = 0, int rx = size) {
        if (rx - lx == 1) {
            info[x] = Info(v);
            return;
        }

        push(x);

        if (i < (lx + rx) / 2) {
            set(i, v, x * 2 + 1, lx, (lx + rx) / 2);
        } else {
            set(i, v, x * 2 + 2, (lx + rx) / 2, rx);
        }

        info[x] = info[x * 2 + 1] + info[x * 2 + 2];
    }

    void add(int l, int r, int v, int x = 0, int lx = 0, int rx = size) {
        if (l <= lx && rx <= r) {
            apply(x, v);
            return;
        }

        if (l >= rx || lx >= r) {
            return;
        }

        push(x);

        add(l, r, v, x * 2 + 1, lx, (lx + rx) / 2);
        add(l, r, v, x * 2 + 2, (lx + rx) / 2, rx);

        info[x] = info[x * 2 + 1] + info[x * 2 + 2];
    }

    int find_max(int x = 0, int lx = 0, int rx = size) {
        if (rx - lx == 1) {
            return lx;
        }

        push(x);

        if (info[x * 2 + 1].max == info[x].max) {
            return find_max(x * 2 + 1, lx, (lx + rx) / 2);
        } else {
            return find_max(x * 2 + 2, (lx + rx) / 2, rx);
        }
    }
} // namespace simple_st

vector<int> et;
int et_num[200500];
int tail[200500];

void set_dist(int u, int p = -1, int d = 0) {
    et_num[u] = et.size();
    et.push_back(u);

    if (g[u].size() == 1) {
        simple_st::set(et_num[u], d);
    }

    for (int v : g[u]) {
        if (v != p) {
            set_dist(v, u, d + 1);
        }
    }

    tail[u] = et.size();
}

multiset<int> taill[200500];
int ans[200500];

bool contains(int l, int r, int x) {
    for (int i = l; i <= r; i++) {
        if (c[et[i]] == x) {
            return true;
        }
    }
    return false;
}

int count(int l, int r) {
    multiset<int> Q;
    for (int i = l; i <= r; i++) {
        Q.insert(c[et[i]]);
    }
    return Q.size();
}

void dfs(int u, int p = -1) {

    if (simple_st::info[0].cnt_max == 1) {
        int k = et[simple_st::find_max()];

        int diff = (simple_st::info[0].max - simple_st::info[0].second_max);

        int x = count(et_num[k] - diff + 1, et_num[k]);

        if (g[u].size() == 1) {
            for (int v : taill[u]) {
                if (!contains(et_num[k] - diff + 1, et_num[k], v)) {
                    x += 1;
                }
            }
        }

        ans[u] = x;
    } else if (g[u].size() == 1) {
        ans[u] = taill[u].size();
    }

    for (int v : g[u]) {
        if (v != p) {
            simple_st::add(0, et.size(), 1);
            simple_st::add(et_num[v], tail[v], -2);
            dfs(v, u);
            simple_st::add(0, et.size(), -1);
            simple_st::add(et_num[v], tail[v], 2);
        }
    }
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
#endif

    int n, m;
    cin >> n >> m;

    if (n == 2) {
        cout << "1  1\n";
        return 0;
    }

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

        g[u].push_back(v);
        g[v].push_back(u);
    }

    for (int u = 1; u <= n; u++) {
        cin >> c[u];
    }

    int root = 1;
    for (int u = 1; u <= n; u++) {
        if (g[u].size() > 1) {
            root = u;
        }

        if (g[u].size() == 1) {

            int last = -1;
            int v = u;

            while (true) {
                if (g[v].size() > 2) {
                    break;
                }

                int cnt = 0;
                for (int k : g[v]) {
                    if (k != last) {
                        cnt += 1;
                    }
                }

                if (cnt == 1) {
                    for (int k : g[v]) {
                        if (k != last) {
                            last = v;
                            v = k;
                            break;
                        }
                    }
                } else {
                    break;
                }

                taill[u].insert(c[v]);
            }
        }
    }

    set_dist(root);
    dfs(root);

    for (int u = 1; u <= n; u++) {
        cout << ans[u] << "\n";
    }
    return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 22172kb

input:

2 1
1 2
1 1

output:

1  1

result:

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

Subtask #2:

score: 0
Wrong Answer

Test #33:

score: 0
Wrong Answer
time: 167ms
memory: 33056kb

input:

115391 1
32067 50006
1710 5850
21016 66364
72998 34367
24783 10670
49950 93666
81835 81234
53480 68963
87357 43320
93905 30509
72528 92224
520 100511
54804 2917
58490 23858
93643 87530
90737 65205
60740 110812
9553 90266
70028 67222
108045 88982
35584 110286
53518 21733
108735 26404
108228 109796
92...

output:

1
1
0
1
0
1
1
0
1
1
1
1
1
1
1
1
1
0
1
0
1
0
1
1
1
1
1
1
3
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
0
1
1
2
1
1
1
0
1
1
1
0
1
1
1
0
1
0
0
1
1
0
1
0
0
1
0
2
1
1
1
1
1
1
1
1
0
0
1
1
0
1
2
1
1
0
1
1
0
0
2
0
2
1
2
1
1
1
4
1
0
1
0
0
0
0
1
1
1
1
1
0
1
1
1
1
0
1
1
0
1
0
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

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

Subtask #3:

score: 0
Wrong Answer

Test #50:

score: 0
Wrong Answer
time: 212ms
memory: 36908kb

input:

157976 157976
20171 157173
44732 54119
107845 121149
109200 110309
82678 108206
89140 64200
36916 128759
3966 123760
92978 105716
43700 146126
14924 3429
107721 36095
94906 78173
97162 29552
119574 39605
25145 138492
16622 99431
60281 7949
76176 136644
75716 91518
127987 110605
77999 110960
101187 5...

output:

0
0
2
2
3
1
2
1
0
0
1
0
4
0
2
0
6
1
0
0
0
4
1
0
0
0
0
2
0
1
1
0
0
0
0
0
2
0
1
1
0
3
1
1
0
0
0
2
3
0
0
0
0
1
0
4
2
1
1
0
0
2
1
1
0
1
0
1
0
0
0
0
0
1
1
0
0
5
1
0
0
4
1
0
0
0
0
0
0
1
1
0
0
0
0
0
1
0
0
0
1
0
1
0
4
0
4
1
0
1
2
0
0
0
0
1
0
0
1
0
0
1
7
0
1
0
1
0
0
4
1
0
1
0
1
0
4
0
0
0
0
0
10
0
0
0
2
1
1
7...

result:

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

Subtask #4:

score: 0
Skipped

Dependency #1:

0%