QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#441767#8530. Cowntact Tracingarbuzick#73.913043 588ms198344kbC++205.2kb2024-06-14 17:56:572024-06-14 17:56:58

Judging History

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

  • [2024-06-14 17:56:58]
  • 评测
  • 测评结果:73.913043
  • 用时:588ms
  • 内存:198344kb
  • [2024-06-14 17:56:57]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long

using namespace std;

constexpr int maxn = 1e5 + 5, inf = 1e9 + 7;

vector<int> g[maxn];

int mn_dist[maxn];

// int dp1[maxn][maxn], dp2[maxn][maxn];

deque<int> dp1[maxn], dp2[maxn];

int used[maxn];

int dp_ans[maxn];

int t = 0;

int get(deque<int> &dq, int pos) {
    if (pos < dq.size()) {
        return dq[pos];
    }
    return 0;
}

void dfs(int v, int nights, int prv = -1LL) {
    used[v] = t;
    vector<int> kids;
    for (auto u : g[v]) {
        if (used[u] < t) {
            dfs(u, nights, v);
            kids.push_back(u);
        }
    }
    sort(kids.begin(), kids.end(), [&](int a, int b) {
        return dp1[a].size() > dp1[b].size();
    });
    if (kids.empty()) {
        dp1[v].push_back(inf);
        if (mn_dist[v] > nights) {
            dp1[v][0] = 1;
        }
        dp2[v].push_back(0);
        dp_ans[v] = dp1[v][0];
        return;
    }
    swap(dp1[v], dp1[kids[0]]);
    dp1[v].push_front(inf);
    if (dp1[v].size() > nights + 1) {
        dp1[v].pop_back();
    }
    if (mn_dist[v] > nights) {
        dp1[v][0] = 1;
        for (auto u : kids) {
            if (nights == 0) {
                dp1[v][0] += dp_ans[u];
            } else {
                dp1[v][0] += get(dp2[u], nights - 1);
            }
        }
    }
    for (int d = 1; d < (int)dp1[v].size(); ++d) {
        int sum = 0;
        for (auto u : kids) {
            if (u == kids[0]) {
                continue;
            }
            if (d == nights) {
                sum += dp_ans[u];
            } else {
                if (dp2[u].size() <= nights - d - 1) {
                    break;
                }
                sum += get(dp2[u], nights - d - 1);
            }
        }
        dp1[v][d] += sum;
        // cout << "!" << sum << ' ';
        if (d == nights) {
            sum += dp_ans[kids[0]];
        } else {
            sum += get(dp2[kids[0]], nights - d - 1);
        }
        // cout << sum << endl;
        for (auto u : kids) {
            if (u == kids[0]) {
                continue;
            }
            if (d == nights) {
                sum -= dp_ans[u];
            } else {
                // cout << u << ' ' << nights - d - 1 << ' ' << dp2[u].size() << ' ' << get(dp2[u], nights - d - 1) << endl;
                sum -= get(dp2[u], nights - d - 1);
            }
            if (dp1[u].size() <= d - 1) {
                break;
            }
            // if (sum == -1) {
            //     cout << "!" << v << ' ' << u << endl;
            // }
            dp1[v][d] = min(dp1[v][d], get(dp1[u], d - 1) + sum);
            if (d == nights) {
                sum += dp_ans[u];
            } else {
                sum += get(dp2[u], nights - d - 1);
            }
        }
    }
    swap(dp2[v], dp2[kids[0]]);
    dp2[v].push_front(0);
    if (dp2[v].size() > nights + 1) {
        dp2[v].pop_back();
    }
    for (auto u : kids) {
        dp2[v][0] += dp_ans[u];
    }
    for (int d = 1; d < (int)dp2[v].size(); ++d) {
        for (int i = 1; i < (int)kids.size(); ++i) {
            dp2[v][d] += get(dp2[kids[i]], d - 1);
            if (dp2[kids[i]].size() < d - 1) {
                break;
            }
        }
    }

    for (int d = 1; d < (int)dp1[v].size(); ++d) {
        dp1[v][d] = min(dp1[v][d], dp1[v][d - 1]);
    }
    dp_ans[v] = dp1[v].back();
    dp2[v][0] = min(dp2[v][0], dp1[v].back());
    for (int i = 1; i < (int)dp2[v].size(); ++i) {
        dp2[v][i] = min(dp2[v][i], dp2[v][i - 1]);
    }
}

void solve() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    for (int i = 0; i < n; ++i) {
        mn_dist[i] = inf;
    }
    for (int i = 0; i < n - 1; ++i) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        if (s[a] == '1' && s[b] == '1') {
            g[a].push_back(b);
            g[b].push_back(a);
        } else {
            mn_dist[a] = 1;
            mn_dist[b] = 1;
        }
    }
    queue<int> q;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '1' && mn_dist[i] == 1) {
            q.push(i);
        }
    }
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (auto u : g[v]) {
            if (mn_dist[u] > mn_dist[v] + 1) {
                mn_dist[u] = mn_dist[v] + 1;
                q.push(u);
            }
        }
    }
    int qs;
    cin >> qs;
    while (qs--) {
        int nights;
        cin >> nights;
        for (int i = 0; i < n; ++i) {
            if (s[i] == '1') {
                dp1[i].clear();
                dp2[i].clear();
            }
        }
        t++;
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            if (s[i] == '1' && used[i] < t) {
                dfs(i, nights);
                if (dp_ans[i] >= inf) {
                    ans = -1;
                    break;
                }
                ans += dp_ans[i];
            }
        }
        cout << ans << '\n';
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

詳細信息

Test #1:

score: 4.34783
Accepted
time: 20ms
memory: 139020kb

input:

5
11111
1 2
2 3
3 4
4 5
6
5
4
3
2
1
0

output:

1
1
1
1
2
5

result:

ok 6 lines

Test #2:

score: 4.34783
Accepted
time: 32ms
memory: 141008kb

input:

10
1111111111
1 2
2 3
2 4
2 5
2 6
6 7
7 8
8 9
9 10
11
0
1
2
3
4
5
6
7
8
9
10

output:

10
3
2
1
1
1
1
1
1
1
1

result:

ok 11 lines

Test #3:

score: 4.34783
Accepted
time: 35ms
memory: 138776kb

input:

5
11100
1 2
2 3
3 4
4 5
6
0
1
2
3
4
5

output:

3
1
1
-1
-1
-1

result:

ok 6 lines

Test #4:

score: 4.34783
Accepted
time: 27ms
memory: 138764kb

input:

10
1101111111
6 4
6 7
4 3
4 2
7 9
9 5
5 1
2 8
2 10
11
2
9
5
0
3
6
10
8
7
1
4

output:

2
-1
-1
9
-1
-1
-1
-1
-1
3
-1

result:

ok 11 lines

Test #5:

score: 4.34783
Accepted
time: 8ms
memory: 138776kb

input:

10
1101011111
7 8
8 2
2 10
10 9
9 4
8 1
10 6
6 3
3 5
11
4
8
6
0
5
3
7
9
10
1
2

output:

-1
-1
-1
8
-1
2
-1
-1
-1
3
2

result:

ok 11 lines

Test #6:

score: 0
Time Limit Exceeded

input:

100000
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:


result:


Test #7:

score: 0
Time Limit Exceeded

input:

100000
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:


result:


Test #8:

score: 4.34783
Accepted
time: 434ms
memory: 180804kb

input:

100000
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

100000
33334
33333
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

result:

ok 20 lines

Test #9:

score: 4.34783
Accepted
time: 35ms
memory: 138852kb

input:

400
11111111101111110011101111111111011111111111111111111111111111111111111111110111111111111111111101111111011111111101111111111111110111111111110110101111111111111111111111111011111111111111110111111111111011111111111111111111110111101111101110111111010011011111111111011111101111110110111111111111...

output:

-1
-1
11
-1
67
-1
12
20
29
363
17
39
-1
-1
-1
-1
-1
-1
-1
-1

result:

ok 20 lines

Test #10:

score: 4.34783
Accepted
time: 28ms
memory: 141116kb

input:

400
11101111111111111111111110110111111111111111011111111111111011111111111111111010111111111110110111011111101111111111111101111111111010111111111111111111111111111111111111101111111111111110111111111111111111111111111111111111111111101011111111111111111110111101111111111111110111111111111111101111...

output:

-1
-1
37
-1
20
4
-1
-1
138
69
-1
12
-1
3
-1
3
374
-1
-1
-1

result:

ok 20 lines

Test #11:

score: 4.34783
Accepted
time: 32ms
memory: 140852kb

input:

400
11010111101110111110111011010111101011011001111110101101000100011111110101011011011111011110111111111000011110101110011100111111101111101011101110111111111001100111111101101111011011101111111111011101110111111111111011111110111111000111111111111111111111010111101101111111111110111101101110011110...

output:

3
7
4
7
7
-1
-1
3
5
5
9
13
30
11
-1
61
18
-1
12
5

result:

ok 20 lines

Test #12:

score: 4.34783
Accepted
time: 44ms
memory: 139884kb

input:

6000
1110011111111111110111111111111111111011011111011110111111110111101011111111110111111110110111111111111111111111111111111111111111101111011111111111111111111011111111111111111111111111111111111111111111111110111111111111111111111111011111111111011111101101111111111111111111111111111111110111111...

output:

19
-1
22
18
28
-1
35
-1
33
5433
22
15
-1
41
19
-1
-1
-1
-1
22

result:

ok 20 lines

Test #13:

score: 4.34783
Accepted
time: 48ms
memory: 142028kb

input:

6000
1111111110011111111101111111111101111111111110011111111011111111111111111110110110011011111110111110111111101111111011110111111111101111111111111001111101110111110111111111111111111111111111111010111111111111111101111111110111111111111111111011111111111111111111111111101111111111010111110111111...

output:

-1
-1
-1
-1
-1
10
-1
45
10
-1
280
86
986
-1
-1
27
-1
-1
15
-1

result:

ok 20 lines

Test #14:

score: 4.34783
Accepted
time: 211ms
memory: 140448kb

input:

6000
1101111110111110101111111111111111111110100111111111111111110111101111111111111111111111111111111111111001111111111111111111111111111111111011111111011110111111110111111111110011111111111111111111111111111111110111111111101111111111100111111111110111110111111011011111110101111111111101111001111...

output:

29
4
5
20
3
27
6
3
7
4
6
-1
35
5
4
5
4
4
11
4

result:

ok 20 lines

Test #15:

score: 4.34783
Accepted
time: 588ms
memory: 158064kb

input:

100000
11111111101111111101111011100111111011111101000111011001111010011010111110101001110111111110111111110011111111111111110110111110111111110100111011111111111011111101111111001111101101111011010110110011010111111011111111111011111011110110110111111011111001100100011011111111100011010110111011110...

output:

189
-1
-1
-1
133
121
361
155
-1
-1
-1
-1
135
128
288
-1
-1
-1
12917
-1

result:

ok 20 lines

Test #16:

score: 4.34783
Accepted
time: 498ms
memory: 162616kb

input:

100000
11111100010011111111001101110011001111000111010011101100101100011101111101010011111111000101111111111111011110011110110111101111101101111011110111001110111111101101111111111101011110101111111111011110111110111111111111011101111111110110111011110011111111101110111011110111111111110001111011111...

output:

1
13015
10
-1
-1
-1
-1
-1
4
39
1
-1
-1
-1
1
1
-1
3609
-1
-1

result:

ok 20 lines

Test #17:

score: 0
Time Limit Exceeded

input:

100000
11111111111111111111111111111110101111111011111111111011111111111101111111101111111111111101111111111111111111111111111111101101111111111111111111101111101001111111111110111111110111111110111110111111111111111111111011111101111011101011111111111111111111111111110111111101111111110111101111110...

output:


result:


Test #18:

score: 4.34783
Accepted
time: 495ms
memory: 166480kb

input:

100000
11111111111111011111111111011101111111111111111111111111111101111001111111111010111110111111111110111111111011111111111011111111111111111111111111011111111111111111111111011111111111110011111110111111111110111111111101111111111111111111111111111101111111110111111101111101111111111111101111111...

output:

208
337
13
1
1
1
1
16218
-1
1
1
1
-1
8728
2
-1
-1
-1
1
-1

result:

ok 20 lines

Test #19:

score: 0
Time Limit Exceeded

input:

100000
11111111111111111111111111110111111111111001111111111111111111110111110111111111111111101111111111111111011110111111111111110011111111111110010111101100111111111111011111111111111111111011111110011111111101010111111111011111111110111111001111011111111111111111111111111111111111111111111111111...

output:


result:


Test #20:

score: 4.34783
Accepted
time: 553ms
memory: 166832kb

input:

100000
11111111111111111110101111111111111011110101111111111011111111110111111111111111011111111111100111111101111111111111101101011111111111111111111111111111111111111111111111111011111110111111111111111111011111111111111111111111111111111111111111111101111111101111111111111111111011101111111111111...

output:

209
119
2
-1
608
356
34069
-1
2
-1
-1
-1
69
2
-1
2
3
16623
2
-1

result:

ok 20 lines

Test #21:

score: 0
Time Limit Exceeded

input:

100000
00110111111011100111110111000111101111101001010100110101111001111111100011100110001111101100110110010011001011101011110111101011111111111110111101101111111111101110011111001111111111111101111111010111011010101111011110011111101101101010111011011111010110110010111100011010111101110010111110111...

output:


result:


Test #22:

score: 4.34783
Accepted
time: 446ms
memory: 198344kb

input:

100000
11011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

99999
49999
49998
49998
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1

result:

ok 20 lines

Test #23:

score: 0
Time Limit Exceeded

input:

99901
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:


result: