This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define For(i, a, b) for(int i = a, _b = b; i <= _b; ++i)
#define Ford(i, a, b) for(int i = a, _b = b; i >= _b; --i)
#define FileName "test"
#define ll long long
#define ld long double
#define ull unsigned long
#define Print(x) cerr << #x << "is " << x << '\n';
#define pb push_back
#define X first
#define Y second
//#define Karma
using namespace std;
template<typename T> inline void Cin(T &x)
{
char c;
T sign = 1;
x = 0;
for (c = getchar(); c < '0' || c > '9'; c = getchar())
if (c == '-') sign = -1;
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
x *= sign;
}
template <typename T> inline void Out(T x) {if(x > 9) Out(x / 10); putchar(x % 10 + '0');}
template <typename T> inline void Cout(T x, char c) {if(x < 0) putchar('-'); x = abs(x); Out(x); putchar(c);}
template <typename T, typename... Args> inline void Cin(T& a, Args&... args) {Cin(a);Cin(args...);}
template <typename T, typename... Args> inline void Cout(T a, char c, Args... args) {Cout(a, c);Cout(args...);}
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
const int N = 3e5 + 7;
int n, crew, u, v;
vector<int> a[N];
void DFS(int u, int par)
{
For(i, 0, a[u].size() - 1)
if(a[u][i] == par) swap(a[u][i], a[u][a[u].size() - 1]);
if(u > 1) a[u].pop_back();
if(a[u].empty()) return;
sort(a[u].begin(), a[u].end(), [](const int& x, const int& y)
{
return a[x].size() > a[y].size();
});
for(int v: a[u]) DFS(v, u);
}
int Build(int u)
{
int sum = 0;
for(int v: a[u]) sum += Build(v);
return max(0, sum + (int)a[u].size() - crew);
}
bool Chk(int mid)
{
crew = mid;
if(crew >= n) return 1;
return Build(1) == 0;
}
void Enter()
{
Cin(n);
For(i, 2, n)
{
Cin(u, v);
a[u].pb(v), a[v].pb(u);
}
DFS(1, 1);
}
void Solve()
{
int low = 1, high = n;
while(low <= high)
{
int mid = (low + high) >> 1;
if(Chk(mid)) high = mid - 1;
else low = mid + 1;
}
int res = n;
Ford(i, low + 1, low - 1) if(Chk(i)) res = i;
cout << res;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifdef Karma
freopen(FileName".inp", "r", stdin);
freopen(FileName".out", "w", stdout);
#endif // Karma
Enter();
Solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |