이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define lesgooo ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl '\n'
#define int long long
vector<int> a, par, sum, sz;
int n, x, y, k;
int find(int u) {return par[u] = (u == par[u]? u : find(par[u]));}
int unionn(int u, int v)
{
u = find(u), v = find(v);
if (sum[u]+sum[v] > k) return 1;
if (sz[u] < sz[v]) swap(u, v);
sz[u]+=sz[v], par[v] = u, sum[u]+=sum[v];
return 0;
}
vector<vector<int>> edges, cur;
int anss = 1e18;
void solve(int ii, int rem)
{
if (ii == edges.size())
{
int ans =0 ;
for (int i = 0; i < n; i++) sum[i] = a[i], par[i] = i, sz[i] = 1;
for (int i = 0; i < cur.size(); i++) ans+=unionn(cur[i][0], cur[i][1]);
anss =min(anss, ans+rem);
return;
}
cur.push_back(edges[ii]);
solve(ii+1, rem);
cur.pop_back();
solve(ii+1, rem+1);
}
signed main()
{
lesgooo;
cin >> n >> k;
a.resize(n), par.resize(n), sum.resize(n), sz.resize(n);
for (int i =0 ; i < n; i++) cin >> a[i];
for (int i = 0; i < n-1; i++) cin >> x >> y, edges.push_back({--x, --y});
solve(0, 0);
cout << anss;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
paprike.cpp: In function 'void solve(long long int, long long int)':
paprike.cpp:22:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | if (ii == edges.size())
| ~~~^~~~~~~~~~~~~~~
paprike.cpp:26:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (int i = 0; i < cur.size(); i++) ans+=unionn(cur[i][0], cur[i][1]);
| ~~^~~~~~~~~~~~
# | 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... |