Datasets:
Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- problems/DP-03/Atcoder/abc207_f/editorial.md +28 -0
- problems/DP-03/Atcoder/abc207_f/judge.json +15 -0
- problems/DP-03/Atcoder/abc207_f/meta.json +16 -0
- problems/DP-03/Atcoder/abc207_f/solution.cpp +68 -0
- problems/DP-03/Atcoder/abc207_f/statement.md +109 -0
- problems/DP-03/Atcoder/abc220_f/editorial.md +13 -0
- problems/DP-03/Atcoder/abc220_f/judge.json +15 -0
- problems/DP-03/Atcoder/abc220_f/meta.json +16 -0
- problems/DP-03/Atcoder/abc220_f/solution.cpp +31 -0
- problems/DP-03/Atcoder/abc220_f/statement.md +79 -0
- problems/DP-03/Atcoder/abc223_g/data/1.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/1.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/10.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/10.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/11.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/11.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/12.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/12.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/13.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/13.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/14.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/14.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/15.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/15.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/16.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/16.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/17.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/17.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/18.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/18.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/19.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/19.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/2.in +3 -0
- problems/DP-03/Atcoder/abc223_g/data/2.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/20.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/20.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/21.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/21.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/22.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/22.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/23.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/23.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/24.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/24.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/25.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/25.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/26.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/26.out +1 -0
- problems/DP-03/Atcoder/abc223_g/data/27.in +0 -0
- problems/DP-03/Atcoder/abc223_g/data/27.out +1 -0
problems/DP-03/Atcoder/abc207_f/editorial.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# F - Tree Patrolling (editorial)
|
| 2 |
+
|
| 3 |
+
Source: https://atcoder.jp/contests/abc207/editorial/2162 by en_translator
|
| 4 |
+
|
| 5 |
+
Transform the given tree into a rooted tree with an arbitrary root, and consider the following DP (Dynamic Programming).
|
| 6 |
+
|
| 7 |
+
- \(\text{dp}[i][j][k]:=(\)The number of choices of vertices out of the vertices of the subtree rooted at \(i\), such that when takahashi are placed at each of those vertices, there are exactly \(j\) vertices guarded by takahashi, where
|
| 8 |
+
|
| 9 |
+
- \(k = 0\) means that a takahashi is placed at Vertex \(i\),
|
| 10 |
+
- \(k = 1\) means that no takahashi is placed at Vertex \(i\), and Vertex \(i\) is not guarded by any takahashi, and
|
| 11 |
+
- \(k = 2\) means that no takahashi is placed at Vertex \(i\), and Vertex \(i\) is guarded by one or more takahashi.\()\)
|
| 12 |
+
|
| 13 |
+
The DP above naively requires \(O(N^2)\) computations when merging the DPs of subtrees. Since we need to merge them \(N-1\) times, the total computations are \(O(N^3)\), which does not fit in the time limit. However, for the DP of a subtree with \(X\) vertices, we can ignore \(j\) such that \(X \lt j\). Therefore, we can reduce to at most \(O(XY)\) the time required to merge the subtree with \(X\) vertices and the subtree with \(Y\) subtrees.
|
| 14 |
+
|
| 15 |
+
At a glance it may just seem improving the constant factor, but actually the total computation complexity of \(N-1\) merges has essentially been improved; specifically, it is \(O(N^2)\).
|
| 16 |
+
|
| 17 |
+
For example, the following precise analysis on complexity is possible:
|
| 18 |
+
|
| 19 |
+
Prepare an undirected graph \(G\) of \(N\) vertices and \(0\) edges, where Vertex \(i\) on the tree corresponds to the Vertex \(i\) in \(G\).
|
| 20 |
+
|
| 21 |
+
When merging a subtree \(A\) of \(X\) vertices and a subtree \(B\) of \(Y\) vertices, add an undirected edge from every vertex in \(A\) to every vertex in \(B\). The number of edges added is \(O(XY)\), the computational cost required to merge.
|
| 22 |
+
|
| 23 |
+
After all merges complete, \(G\) is a complete graph with \(O(N^2)\) number of edges, which is equal to the total sum of computational complexity required for all merge in the DP on tree described above .
|
| 24 |
+
|
| 25 |
+
Alternatively, one can hold the following two states independently as indices of DP:
|
| 26 |
+
|
| 27 |
+
- whether or not a takahashi is placed on Vertex \(i\), and
|
| 28 |
+
- whether or not Vertex \(i\) is guarded by one or more takahashi.
|
problems/DP-03/Atcoder/abc207_f/judge.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"time_limit_s": 3.0,
|
| 3 |
+
"time_limit_source": "task page",
|
| 4 |
+
"memory_mb": 1024,
|
| 5 |
+
"memory_source": "task page",
|
| 6 |
+
"mode": "exact",
|
| 7 |
+
"validator_flags": "case_sensitive",
|
| 8 |
+
"comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)",
|
| 9 |
+
"validator": null,
|
| 10 |
+
"tests_dir": "data",
|
| 11 |
+
"answer_suffix": ".out",
|
| 12 |
+
"n_sample": null,
|
| 13 |
+
"n_secret": 30,
|
| 14 |
+
"note": "no sample/secret split: the statement's samples are among the numbered tests"
|
| 15 |
+
}
|
problems/DP-03/Atcoder/abc207_f/meta.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task_id": "abc207_f",
|
| 3 |
+
"contest": "abc207",
|
| 4 |
+
"type": "ABC",
|
| 5 |
+
"letter": "F",
|
| 6 |
+
"title": "Tree Patrolling",
|
| 7 |
+
"url": "https://atcoder.jp/contests/abc207/tasks/abc207_f",
|
| 8 |
+
"difficulty": 2398,
|
| 9 |
+
"score": 600,
|
| 10 |
+
"time_limit_sec": 3.0,
|
| 11 |
+
"memory_limit_mb": 1024,
|
| 12 |
+
"n_tests": 30,
|
| 13 |
+
"editorial_url": "https://atcoder.jp/contests/abc207/editorial/2162",
|
| 14 |
+
"code_source": "model",
|
| 15 |
+
"pid": "A1620"
|
| 16 |
+
}
|
problems/DP-03/Atcoder/abc207_f/solution.cpp
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// abc207_f: F - Tree Patrolling
|
| 2 |
+
// Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc207/editorial/2162
|
| 3 |
+
// Judged AC on all 30 tests, max 0.04 s of the 3 s limit (submission 1).
|
| 4 |
+
|
| 5 |
+
#include <bits/stdc++.h>
|
| 6 |
+
#include <atcoder/modint>
|
| 7 |
+
using namespace std;
|
| 8 |
+
using mint = atcoder::modint1000000007;
|
| 9 |
+
using State = array<mint, 3>;
|
| 10 |
+
|
| 11 |
+
int main() {
|
| 12 |
+
ios::sync_with_stdio(false);
|
| 13 |
+
cin.tie(nullptr);
|
| 14 |
+
|
| 15 |
+
int N;
|
| 16 |
+
cin >> N;
|
| 17 |
+
vector<vector<int>> graph(N);
|
| 18 |
+
for (int i = 0; i < N - 1; ++i) {
|
| 19 |
+
int u, v;
|
| 20 |
+
cin >> u >> v;
|
| 21 |
+
--u;
|
| 22 |
+
--v;
|
| 23 |
+
graph[u].push_back(v);
|
| 24 |
+
graph[v].push_back(u);
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
// States: 0 = selected, 1 = unguarded, 2 = guarded but not selected.
|
| 28 |
+
function<vector<State>(int, int)> dfs = [&](int v, int parent) -> vector<State> {
|
| 29 |
+
vector<State> dp(2);
|
| 30 |
+
dp[1][0] = 1;
|
| 31 |
+
dp[0][1] = 1;
|
| 32 |
+
int size = 1;
|
| 33 |
+
|
| 34 |
+
for (int u : graph[v]) {
|
| 35 |
+
if (u == parent) continue;
|
| 36 |
+
vector<State> child = dfs(u, v);
|
| 37 |
+
int child_size = static_cast<int>(child.size()) - 1;
|
| 38 |
+
int total = size + child_size;
|
| 39 |
+
vector<State> next(total + 1);
|
| 40 |
+
|
| 41 |
+
for (int i = 0; i <= size; ++i) {
|
| 42 |
+
const auto &a = dp[i];
|
| 43 |
+
for (int j = 0; j <= child_size; ++j) {
|
| 44 |
+
const auto &b = child[j];
|
| 45 |
+
int k = i + j;
|
| 46 |
+
next[k][0] += a[0] * (b[0] + b[2]);
|
| 47 |
+
next[k][1] += a[1] * (b[1] + b[2]);
|
| 48 |
+
next[k][2] += a[2] * (b[0] + b[1] + b[2]);
|
| 49 |
+
if (k < total) {
|
| 50 |
+
// Selecting v newly guards an unguarded child root.
|
| 51 |
+
next[k + 1][0] += a[0] * b[1];
|
| 52 |
+
// Selecting the child root newly guards v.
|
| 53 |
+
next[k + 1][2] += a[1] * b[0];
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
dp.swap(next);
|
| 58 |
+
size = total;
|
| 59 |
+
}
|
| 60 |
+
return dp;
|
| 61 |
+
};
|
| 62 |
+
|
| 63 |
+
vector<State> answer = dfs(0, -1);
|
| 64 |
+
for (int k = 0; k <= N; ++k) {
|
| 65 |
+
cout << (answer[k][0] + answer[k][1] + answer[k][2]).val() << '\n';
|
| 66 |
+
}
|
| 67 |
+
return 0;
|
| 68 |
+
}
|
problems/DP-03/Atcoder/abc207_f/statement.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# F - Tree Patrolling
|
| 2 |
+
|
| 3 |
+
**Score**: 600 points
|
| 4 |
+
|
| 5 |
+
## Problem Statement
|
| 6 |
+
You have a tree with \( N \) vertices, numbered \( 1 \) through \( N \). The \( i \)-th edge connects Vertex \( u_i \) and Vertex \( v_i \).
|
| 7 |
+
|
| 8 |
+
You will choose some vertices (possibly none) and place a takahashi in each of them to guard the tree. A takahashi placed at Vertex \( x \) will guard \( x \) itself and the vertices directly connected to \( x \) by an edge.
|
| 9 |
+
|
| 10 |
+
There are \( 2^N \) ways to choose vertices for placing takahashi. In how many of them will there be exactly \( K \) vertices guarded by one or more takahashi?
|
| 11 |
+
|
| 12 |
+
Find this count and print it modulo \( (10^9 + 7) \) for each \( K = 0, 1, \ldots, N \).
|
| 13 |
+
|
| 14 |
+
## Constraints
|
| 15 |
+
- \( 1 \leq N \leq 2000 \)
|
| 16 |
+
- \( 1 \leq u_i < v_i \leq N \)
|
| 17 |
+
- The given graph is a tree.
|
| 18 |
+
- All values in input are integers.
|
| 19 |
+
|
| 20 |
+
## Input
|
| 21 |
+
Input is given from Standard Input in the following format:
|
| 22 |
+
```
|
| 23 |
+
N
|
| 24 |
+
u_1 v_1
|
| 25 |
+
u_2 v_2
|
| 26 |
+
...
|
| 27 |
+
u_{N-1} v_{N-1}
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
## Output
|
| 31 |
+
Print \( N + 1 \) lines. The \( i \)-th line should contain the answer for \( K = i - 1 \).
|
| 32 |
+
|
| 33 |
+
---
|
| 34 |
+
|
| 35 |
+
## Sample Input 1
|
| 36 |
+
```
|
| 37 |
+
3
|
| 38 |
+
1 3
|
| 39 |
+
1 2
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
## Sample Output 1
|
| 43 |
+
```
|
| 44 |
+
1
|
| 45 |
+
0
|
| 46 |
+
2
|
| 47 |
+
5
|
| 48 |
+
```
|
| 49 |
+
There are eight ways to choose vertices for placing takahashi, as follows:
|
| 50 |
+
- Place a takahashi at no vertices, guarding no vertices.
|
| 51 |
+
- Place a takahashi at Vertex \( 1 \), guarding all vertices.
|
| 52 |
+
- Place a takahashi at Vertex \( 2 \), guarding Vertices \( 1 \) and \( 2 \).
|
| 53 |
+
- Place a takahashi at Vertex \( 3 \), guarding Vertices \( 1 \) and \( 3 \).
|
| 54 |
+
- Place a takahashi at Vertices \( 1 \) and \( 2 \), guarding all vertices.
|
| 55 |
+
- Place a takahashi at Vertices \( 1 \) and \( 3 \), guarding all vertices.
|
| 56 |
+
- Place a takahashi at Vertices \( 2 \) and \( 3 \), guarding all vertices.
|
| 57 |
+
- Place a takahashi at all vertices, guarding all vertices.
|
| 58 |
+
|
| 59 |
+
---
|
| 60 |
+
|
| 61 |
+
## Sample Input 2
|
| 62 |
+
```
|
| 63 |
+
5
|
| 64 |
+
1 3
|
| 65 |
+
4 5
|
| 66 |
+
1 5
|
| 67 |
+
2 3
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
## Sample Output 2
|
| 71 |
+
```
|
| 72 |
+
1
|
| 73 |
+
0
|
| 74 |
+
2
|
| 75 |
+
5
|
| 76 |
+
7
|
| 77 |
+
17
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
---
|
| 81 |
+
|
| 82 |
+
## Sample Input 3
|
| 83 |
+
```
|
| 84 |
+
10
|
| 85 |
+
6 10
|
| 86 |
+
1 8
|
| 87 |
+
2 7
|
| 88 |
+
5 6
|
| 89 |
+
3 8
|
| 90 |
+
3 4
|
| 91 |
+
7 10
|
| 92 |
+
4 9
|
| 93 |
+
2 8
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
## Sample Output 3
|
| 97 |
+
```
|
| 98 |
+
1
|
| 99 |
+
0
|
| 100 |
+
3
|
| 101 |
+
8
|
| 102 |
+
15
|
| 103 |
+
32
|
| 104 |
+
68
|
| 105 |
+
110
|
| 106 |
+
196
|
| 107 |
+
266
|
| 108 |
+
325
|
| 109 |
+
```
|
problems/DP-03/Atcoder/abc220_f/editorial.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# F - Distance Sums 2 (editorial)
|
| 2 |
+
|
| 3 |
+
Source: https://atcoder.jp/contests/abc220/editorial/2705 by en_translator
|
| 4 |
+
|
| 5 |
+
Let us abbreviate \(\sum_{j=1}^{N}dis(i,j)\) to \(ans(i)\).
|
| 6 |
+
|
| 7 |
+
First, we compute \(dis(1,i)\) for each vertex \(i\). This can be computed with a Depth-First Search (DFS) in a total of \(O(N)\) time, so we can obtain \(ans(1)\).
|
| 8 |
+
|
| 9 |
+
Next, for a vertex \(s\) adjacent to Vertex \(1\), we consider the difference of \(ans(1)\) and \(ans(s)\). If a vertex \(j\) is included in a subtree under Vertex \(s\), then \(dis(s,j)=dis(i,j)-1\); if not, \(dis(s,j)=dis(i,j)+1\); so the following relation holds:
|
| 10 |
+
|
| 11 |
+
\(ans(s) = ans(1) + N - 2 \times sub(s)\) (where \(sub(s)\) is the size of subtree under Vertex \(s\))
|
| 12 |
+
|
| 13 |
+
Therefore, we can precalculate the size of subtree under each vertex and compute \(ans(i)\) from the nearest to Vertex \(1\) to the furthest, so that the problem is solved in a total of \(O(N)\) time.
|
problems/DP-03/Atcoder/abc220_f/judge.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"time_limit_s": 2.0,
|
| 3 |
+
"time_limit_source": "task page",
|
| 4 |
+
"memory_mb": 1024,
|
| 5 |
+
"memory_source": "task page",
|
| 6 |
+
"mode": "exact",
|
| 7 |
+
"validator_flags": "case_sensitive",
|
| 8 |
+
"comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)",
|
| 9 |
+
"validator": null,
|
| 10 |
+
"tests_dir": "data",
|
| 11 |
+
"answer_suffix": ".out",
|
| 12 |
+
"n_sample": null,
|
| 13 |
+
"n_secret": 25,
|
| 14 |
+
"note": "no sample/secret split: the statement's samples are among the numbered tests"
|
| 15 |
+
}
|
problems/DP-03/Atcoder/abc220_f/meta.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task_id": "abc220_f",
|
| 3 |
+
"contest": "abc220",
|
| 4 |
+
"type": "ABC",
|
| 5 |
+
"letter": "F",
|
| 6 |
+
"title": "Distance Sums 2",
|
| 7 |
+
"url": "https://atcoder.jp/contests/abc220/tasks/abc220_f",
|
| 8 |
+
"difficulty": 1304,
|
| 9 |
+
"score": 500,
|
| 10 |
+
"time_limit_sec": 2.0,
|
| 11 |
+
"memory_limit_mb": 1024,
|
| 12 |
+
"n_tests": 25,
|
| 13 |
+
"editorial_url": "https://atcoder.jp/contests/abc220/editorial/2705",
|
| 14 |
+
"code_source": "editorial",
|
| 15 |
+
"pid": "A1714"
|
| 16 |
+
}
|
problems/DP-03/Atcoder/abc220_f/solution.cpp
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// abc220_f: F - Distance Sums 2
|
| 2 |
+
// Code from the official editorial, https://atcoder.jp/contests/abc220/editorial/2705 (by en_translator)
|
| 3 |
+
// Judged AC on all 25 tests, max 0.39 s of the 2 s limit.
|
| 4 |
+
|
| 5 |
+
#include <bits/stdc++.h>
|
| 6 |
+
using namespace std;
|
| 7 |
+
|
| 8 |
+
int main(){
|
| 9 |
+
int n; cin >> n;
|
| 10 |
+
vector<vector<int>> g(n);
|
| 11 |
+
for(int i = 0; i < n - 1; i++) {
|
| 12 |
+
int a, b; cin >> a >> b;
|
| 13 |
+
g[a-1].push_back(b-1);
|
| 14 |
+
g[b-1].push_back(a-1);
|
| 15 |
+
}
|
| 16 |
+
vector<long long> sub(n,1), ans(n);
|
| 17 |
+
function<void(int, int, int)> dfs=[&](int i, int p, int d){
|
| 18 |
+
ans[0] += d;
|
| 19 |
+
for(int x : g[i]) if(x != p) {
|
| 20 |
+
dfs(x, i, d+1);
|
| 21 |
+
sub[i] += sub[x];
|
| 22 |
+
}
|
| 23 |
+
}; dfs(0, -1, 0);
|
| 24 |
+
function<void(int, int)> dfs2=[&](int i, int p){
|
| 25 |
+
for(int x : g[i]) if(x != p) {
|
| 26 |
+
ans[x] = ans[i] - 2 * sub[x] + n;
|
| 27 |
+
dfs2(x, i);
|
| 28 |
+
}
|
| 29 |
+
}; dfs2(0, -1);
|
| 30 |
+
for(long long x : ans) cout << x << endl;
|
| 31 |
+
}
|
problems/DP-03/Atcoder/abc220_f/statement.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# F - Distance Sums 2
|
| 2 |
+
|
| 3 |
+
**Score**: 500 points
|
| 4 |
+
|
| 5 |
+
## Problem Statement
|
| 6 |
+
Given is a tree with \(N\) vertices. The vertices are numbered \(1, 2, \ldots, N\), and the \(i\)-th edge is an undirected edge connecting Vertices \(u_i\) and \(v_i\).
|
| 7 |
+
|
| 8 |
+
For each integer \(i\,(1 \leq i \leq N)\), find \(\sum_{j=1}^{N}dis(i,j)\).
|
| 9 |
+
|
| 10 |
+
Here, \(dis(i,j)\) denotes the minimum number of edges that must be traversed to go from Vertex \(i\) to Vertex \(j\).
|
| 11 |
+
|
| 12 |
+
## Constraints
|
| 13 |
+
- \(2 \leq N \leq 2 \times 10^5\)
|
| 14 |
+
- \(1 \leq u_i < v_i \leq N\)
|
| 15 |
+
- The given graph is a tree.
|
| 16 |
+
- All values in input are integers.
|
| 17 |
+
|
| 18 |
+
## Input
|
| 19 |
+
Input is given from Standard Input in the following format:
|
| 20 |
+
```
|
| 21 |
+
N
|
| 22 |
+
u_1 v_1
|
| 23 |
+
u_2 v_2
|
| 24 |
+
...
|
| 25 |
+
u_{N-1} v_{N-1}
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
## Output
|
| 29 |
+
Print \(N\) lines. The \(i\)-th line should contain \(\sum_{j=1}^{N}dis(i,j)\).
|
| 30 |
+
|
| 31 |
+
## Sample Input 1
|
| 32 |
+
```
|
| 33 |
+
3
|
| 34 |
+
1 2
|
| 35 |
+
2 3
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
## Sample Output 1
|
| 39 |
+
```
|
| 40 |
+
3
|
| 41 |
+
2
|
| 42 |
+
3
|
| 43 |
+
```
|
| 44 |
+
We have:
|
| 45 |
+
- \(dis(1,1)+dis(1,2)+dis(1,3)=0+1+2=3\)
|
| 46 |
+
- \(dis(2,1)+dis(2,2)+dis(2,3)=1+0+1=2\)
|
| 47 |
+
- \(dis(3,1)+dis(3,2)+dis(3,3)=2+1+0=3\).
|
| 48 |
+
|
| 49 |
+
## Sample Input 2
|
| 50 |
+
```
|
| 51 |
+
2
|
| 52 |
+
1 2
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
## Sample Output 2
|
| 56 |
+
```
|
| 57 |
+
1
|
| 58 |
+
1
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
## Sample Input 3
|
| 62 |
+
```
|
| 63 |
+
6
|
| 64 |
+
1 6
|
| 65 |
+
1 5
|
| 66 |
+
1 3
|
| 67 |
+
1 4
|
| 68 |
+
1 2
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
## Sample Output 3
|
| 72 |
+
```
|
| 73 |
+
5
|
| 74 |
+
9
|
| 75 |
+
9
|
| 76 |
+
9
|
| 77 |
+
9
|
| 78 |
+
9
|
| 79 |
+
```
|
problems/DP-03/Atcoder/abc223_g/data/1.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/1.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
0
|
problems/DP-03/Atcoder/abc223_g/data/10.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/10.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
0
|
problems/DP-03/Atcoder/abc223_g/data/11.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/11.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
0
|
problems/DP-03/Atcoder/abc223_g/data/12.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/12.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
0
|
problems/DP-03/Atcoder/abc223_g/data/13.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/13.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
0
|
problems/DP-03/Atcoder/abc223_g/data/14.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/14.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
199999
|
problems/DP-03/Atcoder/abc223_g/data/15.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/15.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
80882
|
problems/DP-03/Atcoder/abc223_g/data/16.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/16.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
80835
|
problems/DP-03/Atcoder/abc223_g/data/17.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/17.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
80688
|
problems/DP-03/Atcoder/abc223_g/data/18.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/18.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
80557
|
problems/DP-03/Atcoder/abc223_g/data/19.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/19.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
79983
|
problems/DP-03/Atcoder/abc223_g/data/2.in
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
3
|
| 2 |
+
1 2
|
| 3 |
+
2 3
|
problems/DP-03/Atcoder/abc223_g/data/2.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
2
|
problems/DP-03/Atcoder/abc223_g/data/20.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/20.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
80568
|
problems/DP-03/Atcoder/abc223_g/data/21.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/21.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
80344
|
problems/DP-03/Atcoder/abc223_g/data/22.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/22.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
80930
|
problems/DP-03/Atcoder/abc223_g/data/23.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/23.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
80753
|
problems/DP-03/Atcoder/abc223_g/data/24.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/24.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
80697
|
problems/DP-03/Atcoder/abc223_g/data/25.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/25.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
14486
|
problems/DP-03/Atcoder/abc223_g/data/26.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/26.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
51168
|
problems/DP-03/Atcoder/abc223_g/data/27.in
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
problems/DP-03/Atcoder/abc223_g/data/27.out
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
17606
|