#include<cstdio> #include<algorithm> #include<iostream> #include<fstream> #include<iomanip> #include<vector> #include<map> #include<set> #include<stack> #define SC(n) scanf("%d", &n) #define SC2(n, m) scanf("%d %d", &n, &m) #define SCC(c) scanf(" %c", &c) #define SCS(b) scanf("%s", b) #define REP(i, n) for(int i = 0; i < (n); ++i) #define FORE(i, a, b) for(int i = (a); i <= (b); ++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define FORIT(i, c, typ) for(typ::iterator i = c.begin(); i != c.end(); ++i) #define PR(n) printf("%d ", (int) (n)) #define PRN(n) printf("%d\n", (int) (n)) #define elif else if #define pb push_back #define mp make_pair #define xx first #define yy second #define all(v) v.begin(), v.end() #define itr iterator #define DBG if(0) printf using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef pair<int, pi> pii; typedef vector<pii> vpii; typedef vector<vpi> vvpi; typedef map<int, vpi> mvpi; const int MMAX = 4000*1000 + 7, NMAX = 2007; int n, k, x, y, w; pii e[MMAX]; int par[NMAX], size[NMAX]; ll ans; inline int getPar(int x) { int y = x; while(x != par[x]) x = par[x]; while(y != x) { int z = par[y]; par[y] = x; y = z; } return x; } inline void connect(int x, int y) { x = getPar(x); y = getPar(y); if(size[x] > size[y]) { par[y] = x; size[x] += size[y]; } else { par[x] = y; size[y] += size[x]; } } int main() { SC(n); k = 0; REP(i, n) FORE(j, i+1, n) { SC(w); e[k++] = mp(w, mp(i,j)); } sort(e, e+k); REP(i, n+1) { par[i] = i; size[i] = 1; } REP(i, k) { x = e[i].yy.xx; y = e[i].yy.yy; if(getPar(x) != getPar(y)) { ans += e[i].xx; connect(x,y); } } printf("%lld", ans); //cout << ans; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #include<cstdio> #include<algorithm> #include<iostream> #include<fstream> #include<iomanip> #include<vector> #include<map> #include<set> #include<stack> #define SC(n) scanf("%d", &n) #define SC2(n, m) scanf("%d %d", &n, &m) #define SCC(c) scanf(" %c", &c) #define SCS(b) scanf("%s", b) #define REP(i, n) for(int i = 0; i < (n); ++i) #define FORE(i, a, b) for(int i = (a); i <= (b); ++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define FORIT(i, c, typ) for(typ::iterator i = c.begin(); i != c.end(); ++i) #define PR(n) printf("%d ", (int) (n)) #define PRN(n) printf("%d\n", (int) (n)) #define elif else if #define pb push_back #define mp make_pair #define xx first #define yy second #define all(v) v.begin(), v.end() #define itr iterator #define DBG if(0) printf using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef pair<int, pi> pii; typedef vector<pii> vpii; typedef vector<vpi> vvpi; typedef map<int, vpi> mvpi; const int MMAX = 4000*1000 + 7, NMAX = 2007; int n, k, x, y, w; pii e[MMAX]; int par[NMAX], size[NMAX]; ll ans; inline int getPar(int x) { int y = x; while(x != par[x]) x = par[x]; while(y != x) { int z = par[y]; par[y] = x; y = z; } return x; } inline void connect(int x, int y) { x = getPar(x); y = getPar(y); if(size[x] > size[y]) { par[y] = x; size[x] += size[y]; } else { par[x] = y; size[y] += size[x]; } } int main() { SC(n); k = 0; REP(i, n) FORE(j, i+1, n) { SC(w); e[k++] = mp(w, mp(i,j)); } sort(e, e+k); REP(i, n+1) { par[i] = i; size[i] = 1; } REP(i, k) { x = e[i].yy.xx; y = e[i].yy.yy; if(getPar(x) != getPar(y)) { ans += e[i].xx; connect(x,y); } } printf("%lld", ans); //cout << ans; return 0; } |