QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#368287 | #3100. Meetings 2 | 0x3b800001 | 0 | 3ms | 13152kb | C++14 | 3.2kb | 2024-03-26 23:00:10 | 2024-03-26 23:00:10 |
Judging History
answer
#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <vector>
#include <map>
int n;
std::vector<int> E[200007];
int fa[200007], sz[200007], maxsz[200007], dep[200007];
std::map<std::pair<int, int>, int> Sz;
void dfs(int u, std::unordered_set<int>& vertex, std::vector<int>& vs) {
vs.push_back(u), sz[u] = 1, maxsz[u] = 0;
for (int v : E[u]) {
if (v != fa[u] && vertex.find(v) != vertex.end()) {
fa[v] = u;
dep[v] = dep[u] + 1;
dfs(v, vertex, vs);
sz[u] += sz[v];
maxsz[u] = std::max(maxsz[u], sz[v]);
}
}
maxsz[u] = std::max(maxsz[u], int(vertex.size()) - sz[u]);
}
int findG(std::unordered_set<int> vertex) {
std::vector<int> tmp;
int t = *vertex.begin();
dfs(t, vertex, tmp);
for (int i : vertex) {
if (maxsz[i] < maxsz[t]) {
t = i;
}
}
return t;
}
int lowbit(int x) { return x & (-x); }
int bit[200007];
int query(int x) {
if (x == 0) {
return 0;
}
return std::max(bit[x], query(x - lowbit(x)));
}
void modify(int x, int d) {
if (x > n) {
return;
}
bit[x] = std::max(bit[x], d);
modify(x + lowbit(x), d);
}
void modify0(int x) {
if (x > n) {
return;
}
bit[x] = 0;
modify0(x + lowbit(x));
}
int ans[200007];
void Divide_Conquer(std::unordered_set<int> vertex) {
int root = findG(vertex);
std::vector<std::vector<int>> sons;
for (int i : E[root]) {
if (vertex.find(i) != vertex.end()) {
std::vector<int> st;
fa[i] = root, dep[i] = 1;
dfs(i, vertex, st);
sons.push_back(st);
}
}
int szr = n - vertex.size() + 1;
for (int t = 0; t < 2; ++t) {
for (auto i : sons) {
for (int j : i) {
int S = sz[j];
int d = query(n + 1 - S);
if (d) {
ans[S] = std::max(ans[S], 1 + dep[j] + d);
}
ans[std::min(S, szr)] = std::max(ans[std::min(S, szr)], dep[j] + 1);
}
for (int j : i) {
int S = sz[j];
modify(n + 1 - S, dep[j]);
}
}
for (auto i : sons) {
for (int j : i) {
int S = sz[j];
modify0(n + 1 - S);
}
}
std::reverse(sons.begin(), sons.end());
}
for (auto i : sons) {
Divide_Conquer(std::unordered_set<int>(i.begin(), i.end()));
}
}
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
std::cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
std::cin >> u >> v;
E[u].push_back(v), E[v].push_back(u);
}
std::unordered_set<int> t;
for (int i = 1; i <= n; ++i) {
t.insert(i);
}
std::vector<int> s;
fa[1] = 0, dfs(1, t, s);
for (int i = 2; i <= n; ++i) {
Sz[{fa[i], i}] = sz[i];
Sz[{i, fa[i]}] = n - sz[i];
}
Divide_Conquer(t);
fa[1] = 0, dfs(1, t, s);
int r = 0;
for (int i = 1; i <= n; ++i) {
r = std::max(r, std::min(sz[i], n - sz[i]));
}
ans[r] = std::max(ans[r], 2);
for (int i = n; i >= 1; --i) {
ans[i] = std::max(ans[i], 1);
ans[i] = std::max(ans[i], ans[i + 1]);
}
for (int i = 1; i <= n; ++i) {
if (i & 1) {
std::cout << "1\n";
} else {
std::cout << ans[i >> 1] << '\n';
}
}
return 0;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 4
Accepted
time: 2ms
memory: 12424kb
input:
1
output:
1
result:
ok single line: '1'
Test #2:
score: 0
Accepted
time: 2ms
memory: 12168kb
input:
2 2 1
output:
1 2
result:
ok 2 lines
Test #3:
score: 0
Accepted
time: 2ms
memory: 11848kb
input:
3 1 3 3 2
output:
1 3 1
result:
ok 3 lines
Test #4:
score: 0
Accepted
time: 0ms
memory: 13020kb
input:
14 12 14 12 9 14 2 12 6 12 7 6 4 3 4 1 4 12 8 13 1 10 12 11 6 6 5
output:
1 7 1 5 1 3 1 3 1 2 1 2 1 2
result:
ok 14 lines
Test #5:
score: 0
Accepted
time: 2ms
memory: 11092kb
input:
14 10 14 3 10 14 13 1 3 3 5 3 11 12 14 14 6 11 8 2 3 7 8 9 7 1 4
output:
1 8 1 6 1 5 1 4 1 2 1 1 1 1
result:
ok 14 lines
Test #6:
score: 0
Accepted
time: 3ms
memory: 13152kb
input:
14 8 13 13 10 13 7 6 8 5 7 10 4 9 5 12 8 6 2 4 11 5 1 9 3 4 14
output:
1 8 1 6 1 5 1 4 1 2 1 1 1 1
result:
ok 14 lines
Test #7:
score: 0
Accepted
time: 2ms
memory: 11660kb
input:
14 9 2 9 8 3 8 14 9 13 3 1 2 5 2 9 6 4 2 4 11 10 6 12 10 7 9
output:
1 7 1 5 1 3 1 2 1 2 1 1 1 1
result:
ok 14 lines
Test #8:
score: 0
Accepted
time: 0ms
memory: 12252kb
input:
15 10 7 15 7 14 7 15 11 6 15 10 8 14 2 4 8 15 1 3 6 4 13 1 9 5 2 8 12
output:
1 8 1 6 1 4 1 4 1 3 1 2 1 1 1
result:
ok 15 lines
Test #9:
score: -4
Wrong Answer
time: 2ms
memory: 11012kb
input:
16 11 8 11 10 10 1 11 2 15 8 13 10 9 2 2 4 8 6 2 7 3 7 12 9 6 16 9 14 12 5
output:
1 8 1 6 1 4 1 4 1 3 1 2 1 2 1 2
result:
wrong answer 10th lines differ - expected: '2', found: '3'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%