POI #Year2006 #妙妙题 #贪心
考虑从下往上按照拓扑序分层,对于每一层,这一层最多可以选择 \(min(2m,cnt)\) 个
考虑这个上界是否可以达到,这是一定可以的,通过将在下面结束的路径向上,可以做到每个点都被经过
所以直接统计
// Author: xiaruize
#ifndef ONLINE_JUDGE
#define debug(x) cerr << "On Line:" << __LINE__ << #x << "=" << x << endl
bool start_of_memory_use;
#else
#define debug(x)
#endif
#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
clock_t start_clock = clock();
#endif
// #define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define mms(arr, n) memset(arr, n, sizeof(arr))
#define rep(i, a, n) for (int i = (a); i <= (n); ++i)
#define per(i, n, a) for (int i = (n); i >= (a); --i)
int max(int a, int b)
{
if (a > b)
return a;
return b;
}
int min(int a, int b)
{
if (a < b)
return a;
return b;
}
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 1e6 + 10;
int n, m;
vector<int> g[N];
int cnt[N], dep[N];
int deg[N];
void solve()
{
cin >> n >> m;
rep(i, 1, n - 1)
{
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
deg[u]++;
deg[v]++;
}
queue<int> q;
rep(i, 1, n)
{
if (deg[i] == 1)
{
dep[i] = 1;
q.push(i);
}
}
while (!q.empty())
{
int x = q.front();
q.pop();
cnt[dep[x]]++;
for (auto v : g[x])
{
dep[v] = dep[x] + 1;
deg[v]--;
if (deg[v] == 1)
q.push(v);
}
}
int res = 0;
rep(i, 1, n)
{
res += min(cnt[i], m * 2);
// cerr << cnt[i] << ' ';
}
cout << res << endl;
}
#ifndef ONLINE_JUDGE
bool end_of_memory_use;
#endif
signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int testcase = 1;
// cin >> testcase;
while (testcase--)
solve();
#ifndef ONLINE_JUDGE
cerr << "Memory use:" << (&end_of_memory_use - &start_of_memory_use) / 1024.0 / 1024.0 << "MiB" << endl;
cerr << "Time use:" << (double)clock() / CLOCKS_PER_SEC * 1000.0 << "ms" << endl;
#endif
return 0;
}
标签:POI2006MET,int,dep,Subway,push,return,define,deg
From: https://www.cnblogs.com/xiaruize/p/18136795