Submission #736158

#TimeUsernameProblemLanguageResultExecution timeMemory
736158marvinthangSjekira (COCI20_sjekira)C++17
110 / 110
37 ms4312 KiB
/******************************
*    author : @marvinthang    *
*    date : 27 / 12 / 2021    *
******************************/

#include <bits/stdc++.h>

using namespace std;

#define  superspeed  ios_base::sync_with_stdio(false); cin.tie(nullptr); //cout.tie(nullptr);
#define  file(name)  if (fopen (name".inp", "r") ) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }

template <class U, class V> ostream & operator << (ostream& out, const pair<U, V> &p) { return out << '(' << p.first << ", " << p.second << ')'; }
template <class T> ostream & operator << (ostream &out, const vector<T> &vt) { out << '['; for (size_t i = 0; i + 1 < vt.size(); i++) out << vt[i] << ", "; if (!vt.empty()) out << vt.back(); return out << ']'; }

const 		int MOD = 1e9 + 7;
const     double PI = 3.1415926535897932384626433832795; // acos(-1.0); atan(-1.0);
const      int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
const  long long oo = 1e18;
const       int MAX = 1e5 + 5;

int N, T[MAX];

struct Edge {
	int u, v;

	bool operator < (const Edge &other) const {
		return max(T[u], T[v]) < max(T[other.u], T[other.v]);
	}
} edges[MAX];

struct DisjointSet {
    
    static const int MAXN = 1e5 + 5;

    int par[MAXN] = {}, maxVal[MAXN];

    DisjointSet(int n = 0) {
        for (int i = 1; i <= n; ++i) par[i] = -1, maxVal[i] = T[i];
    }

    int findSet(int u) {
        return par[u] < 0 ? u : par[u] = findSet(par[u]);
    }

    int unionSet(int u, int v) {
        if ((u = findSet(u)) == (v = findSet(v))) return 0;
        if (par[u] > par[v]) swap(u, v);
        par[u] += par[v];
        par[v] = u;
        int res = maxVal[u] + maxVal[v];
        maxVal[u] = max(maxVal[u], maxVal[v]);
        return res;
    }

};

int main(void) {
    superspeed;
    file("chvpt_sjekira");
    cin >> N;
    for (int i = 1; i <= N; ++i) cin >> T[i];
    DisjointSet dsu(N);
    for (int i = 1; i < N; ++i) cin >> edges[i].u >> edges[i].v;
    sort(edges + 1, edges + N);
	long long res = 0;
	for (int i = 1; i < N; ++i) {
		res += dsu.unionSet(edges[i].u, edges[i].v);
	}
	cout << res << '\n';
    return 0;
}

Compilation message (stderr)

sjekira.cpp: In function 'int main()':
sjekira.cpp:11:62: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | #define  file(name)  if (fopen (name".inp", "r") ) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
      |                                                      ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sjekira.cpp:60:5: note: in expansion of macro 'file'
   60 |     file("chvpt_sjekira");
      |     ^~~~
sjekira.cpp:11:96: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | #define  file(name)  if (fopen (name".inp", "r") ) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
      |                                                                                        ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sjekira.cpp:60:5: note: in expansion of macro 'file'
   60 |     file("chvpt_sjekira");
      |     ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...