이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
// #define int long long
// #define int unsigned long long
// #define ordered_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// #define ordered_multiset(T) tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>
void open_file(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
// const ll mod = 1e9 + 7;
// const ll mod = 998244353;
const ll inf = 1e9;
const ll biginf = 1e18;
const int maxN = 1e5 + 25;
set<pii> ers;
vector<int> g[maxN];
int par[maxN], mx[maxN], t[maxN];
void make_set(int n) {
for (int i = 1; i <= n; i++) {
par[i] = i; mx[i] = t[i];
}
}
int find_set(int v) {
if (par[v] == v)
return v;
return par[v] = find_set(par[v]);
}
int union_set(int x, int y) {
x = find_set(x);
y = find_set(y);
if (x == y) return 0;
int ans = mx[x] + mx[y];
par[y] = x; mx[x] = max(mx[x], mx[y]);
return ans;
}
void solve() {
int n;
cin >> n;
pii d[n + 1];
for (int i = 1; i <= n; i++) {
cin >> d[i].ff; d[i].sc = i; t[i] = d[i].ff;
} sort(d + 1, d + n + 1);
for (int i = 1; i < n; i++) {
int x, y; cin >> x >> y;
g[x].pb(y); g[y].pb(x);
} ll ans = 0;
vector<pii> ord;
for (int i = n; i >= 1; i--) {
for (int x : g[d[i].sc]) {
if (ers.find({d[i].sc, x}) == ers.end()) {
ers.insert({d[i].sc, x});
ers.insert({x, d[i].sc});
ord.pb({d[i].sc, x});
}
}
} make_set(n); reverse(all(ord));
for (auto x : ord) {
ans += union_set(x.ff, x.sc);
} cout << ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sjekira.cpp: In function 'void open_file(std::string)':
sjekira.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen((filename + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sjekira.cpp:28:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | freopen((filename + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |