Submission #8092442
Source Code Expand
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define ull unsigned long long #define __ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) const int maxn = 2020; int n, k; vector<int> v[maxn]; queue<int> q, p; bool vis[maxn]; vector<int> vv; int bfs(int x, int s, bool op) { while (!q.empty())q.pop(); while (!p.empty())p.pop(); int ct = 0; q.push(x); ++ct; if (op)vv.push_back(x); vis[x] = 1; for (int i = 0; i <= s; ++i) { while (!p.empty()) { int x = p.front(); p.pop(); for (int j = 0; j < v[x].size(); ++j) { if (vis[v[x][j]] == 0) { vis[v[x][j]] = 1; q.push(v[x][j]); if (op) vv.push_back(v[x][j]); ++ct; } } } swap(p, q); } return ct; } int dp(int x, int s) { vis[x] = 1; int ct = 0; int tot = 0; for (int i = 0; i < v[x].size(); ++i) { int a1 = bfs(v[x][i], s - 1, 1); for (auto j:vv) { vis[j] = 0; } vv.clear(); int a2 = bfs(v[x][i], s, 0); tot = max(tot, a2 - a1); ct += a1; } return ct + tot + 1; } int main() { __; cin >> n >> k; for (int i = 1; i < n; ++i) { int x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } if (k % 2 == 0) { int ans = 0; for (int i = 1; i <= n; ++i) { memset(vis, 0, sizeof vis); ans = max(ans, bfs(i, k / 2, 0)); vv.clear(); } cout << n - ans << endl; } else { int ans = 0; for (int i = 1; i <= n; ++i) { ans = max(ans, dp(i, k / 2)); } cout << n - ans << endl; } return 0; }
Submission Info
Submission Time | |
---|---|
Task | C - Shorten Diameter |
User | vjudge3 |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 1862 Byte |
Status | WA |
Exec Time | 59 ms |
Memory | 384 KB |
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, sample-01.txt, sample-02.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
000.txt | AC | 1 ms | 256 KB |
001.txt | AC | 1 ms | 256 KB |
002.txt | AC | 1 ms | 256 KB |
003.txt | AC | 1 ms | 256 KB |
004.txt | AC | 1 ms | 256 KB |
005.txt | AC | 1 ms | 256 KB |
006.txt | AC | 1 ms | 256 KB |
007.txt | WA | 1 ms | 256 KB |
008.txt | WA | 1 ms | 256 KB |
009.txt | WA | 2 ms | 384 KB |
010.txt | WA | 2 ms | 384 KB |
011.txt | WA | 2 ms | 384 KB |
012.txt | WA | 6 ms | 384 KB |
013.txt | AC | 2 ms | 384 KB |
014.txt | AC | 2 ms | 384 KB |
015.txt | AC | 18 ms | 384 KB |
016.txt | WA | 2 ms | 384 KB |
017.txt | AC | 13 ms | 384 KB |
018.txt | AC | 2 ms | 384 KB |
019.txt | WA | 1 ms | 256 KB |
020.txt | WA | 2 ms | 384 KB |
021.txt | AC | 26 ms | 384 KB |
022.txt | AC | 2 ms | 384 KB |
023.txt | WA | 1 ms | 256 KB |
024.txt | AC | 4 ms | 384 KB |
025.txt | AC | 1 ms | 256 KB |
026.txt | WA | 3 ms | 384 KB |
027.txt | WA | 2 ms | 384 KB |
028.txt | WA | 59 ms | 384 KB |
029.txt | AC | 18 ms | 384 KB |
030.txt | WA | 2 ms | 384 KB |
031.txt | AC | 2 ms | 384 KB |
sample-01.txt | AC | 1 ms | 256 KB |
sample-02.txt | AC | 1 ms | 256 KB |