Submission #990477
Source Code Expand
#include <string>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <deque>
#include <unordered_set>
#include <unordered_map>
#include <tuple>
using EdgeList = std::vector<std::vector<int>>;
using Stack = std::vector<std::tuple<int, int>>;
using RemoveSet = std::unordered_set<int>;
using CheckList = std::vector<bool>;
int countInDiameter (EdgeList const & e, Stack & stack, CheckList & checklist, int D, int start) {
stack.clear();
stack.emplace_back(start, 0);
int z = 0;
while (stack.size() > 0) {
int v;
int d;
std::tie(v, d) = stack.back();
stack.pop_back();
checklist[v] = false;
for (int v2 : e[v]) {
if (checklist[v2]) {
stack.emplace_back(v2, d + 1);
}
}
if (d <= D) {
z += 1;
}
}
return z;
}
int main () {
std::string line;
char * p;
std::getline(std::cin, line);
p = &line[0];
int N = std::strtol(p, &p, 10);
int K = std::strtol(p, &p, 10);
EdgeList E(N);
std::vector<std::tuple<int, int>> E2(N - 1);
for (int i = 0; i < N - 1; ++i) {
std::getline(std::cin, line);
p = &line[0];
int A = std::strtol(p, &p, 10) - 1;
int B = std::strtol(p, &p, 10) - 1;
E[A].emplace_back(B);
E[B].emplace_back(A);
E2.emplace_back(A, B);
}
Stack stack;
CheckList checklist(N, true);
int zmax = 0;
if (K % 2 == 0) {
for (int i = 0; i < N; ++i) {
std::fill(checklist.begin(), checklist.end(), true);
zmax = std::max(zmax, countInDiameter(E, stack, checklist, K / 2, i));
}
} else {
for (int i = 0; i < N - 1; ++i) {
std::fill(checklist.begin(), checklist.end(), true);
zmax = std::max(zmax, countInDiameter(E, stack, checklist, (K - 1)/ 2, std::get<0>(E2[i])));
std::fill(checklist.begin(), checklist.end(), true);
zmax = std::max(zmax, countInDiameter(E, stack, checklist, (K - 1)/ 2, std::get<1>(E2[i])));
}
}
std::cout << N - zmax << std::endl;
}
Submission Info
| Submission Time |
|
| Task |
C - Shorten Diameter |
| User |
toufu12345 |
| Language |
C++14 (GCC 5.4.1) |
| Score |
0 |
| Code Size |
2053 Byte |
| Status |
WA |
| Exec Time |
125 ms |
| Memory |
384 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
0 / 600 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
sample-01.txt, sample-02.txt |
| All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 000.txt |
WA |
3 ms |
256 KiB |
| 001.txt |
AC |
3 ms |
256 KiB |
| 002.txt |
AC |
3 ms |
256 KiB |
| 003.txt |
AC |
3 ms |
256 KiB |
| 004.txt |
AC |
3 ms |
256 KiB |
| 005.txt |
WA |
3 ms |
256 KiB |
| 006.txt |
WA |
3 ms |
256 KiB |
| 007.txt |
WA |
3 ms |
384 KiB |
| 008.txt |
WA |
3 ms |
256 KiB |
| 009.txt |
WA |
122 ms |
384 KiB |
| 010.txt |
WA |
77 ms |
384 KiB |
| 011.txt |
WA |
115 ms |
384 KiB |
| 012.txt |
WA |
125 ms |
384 KiB |
| 013.txt |
AC |
40 ms |
384 KiB |
| 014.txt |
AC |
59 ms |
384 KiB |
| 015.txt |
AC |
63 ms |
384 KiB |
| 016.txt |
WA |
77 ms |
384 KiB |
| 017.txt |
AC |
59 ms |
384 KiB |
| 018.txt |
AC |
4 ms |
256 KiB |
| 019.txt |
WA |
3 ms |
256 KiB |
| 020.txt |
WA |
26 ms |
256 KiB |
| 021.txt |
AC |
45 ms |
384 KiB |
| 022.txt |
AC |
12 ms |
384 KiB |
| 023.txt |
WA |
4 ms |
256 KiB |
| 024.txt |
AC |
9 ms |
256 KiB |
| 025.txt |
AC |
3 ms |
256 KiB |
| 026.txt |
WA |
91 ms |
384 KiB |
| 027.txt |
WA |
91 ms |
384 KiB |
| 028.txt |
WA |
94 ms |
384 KiB |
| 029.txt |
WA |
94 ms |
384 KiB |
| 030.txt |
WA |
71 ms |
384 KiB |
| 031.txt |
AC |
38 ms |
384 KiB |
| sample-01.txt |
AC |
3 ms |
256 KiB |
| sample-02.txt |
AC |
3 ms |
256 KiB |