//Michal Wos MIM UW #include <climits> #include <cstdlib> #include <iostream> #include <stdio.h> #include <vector> #include <algorithm> #include <set> #include <map> #include <cmath> #include <string> #include <string.h> #define pb push_back #define pii pair<int,int> #define vi vector<int> #define f first #define s second #define x first #define y second #define Size(x) ((int)(x).size()) #define FOR(z,b,e) for(__typeof(b) z = b; z<=e; z++ ) #define debon 1 #define deb(burak) if(debon) {cout<<"DEB-> "<<#burak<<": "<<burak<<endl;} #define debv(burak) if(debon) {cout<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<burak.size();zyx++) cout<<burak[zyx]<<" "; cout<<endl;} #define debt(burak,SIzE) if(debon) {cout<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<SIzE;zyx++) cout<<burak[zyx]<<" "; cout<<endl;} #define debend if(debon) {cout<<"_____________________"<<endl;} #define memcheck if(debon) {FILE *fp = fopen("/proc/self/status","r");while( !feof(fp) ) putchar(fgetc(fp));} using namespace std; typedef unsigned long long ULL; typedef long long LL; void readLL(LL *n) {register char c=0; while (c < 33) c=getc_unlocked(stdin); (*n)=0; while (c>32) {(*n)=(*n)*10LL + (c-'0'); c=getc_unlocked(stdin);}} const int inf=1073741824,mod=1e9+7; const int s=2010; int tab[s], ile[s]; int Find(int a) { if (tab[a] == a) { return a; } int fa = Find(tab[a]); tab[a] = fa; return fa; } bool Union(int a, int b) { int fa = Find(a); int fb = Find(b); if (fa == fb) { return false; } if (ile[fa] <= ile[fb]) { ile[fb] += ile[fa]; tab[fa] = fb; } else { ile[fa] += ile[fb]; tab[fb] = fa; } return true; } int main() { #define edge pair<ULL, pii> #define waga first #define va second.first #define vb second.second vector <edge> E; int n, c; ULL res = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { scanf("%d", &c); E.pb(edge(c,pii(i, j+1))); } } sort(E.begin(),E.end()); //init F&U for (int i=0; i<=n; i++) { tab[i] = i; ile[i] = 1; } //MST int kr = 0; for (int i = 0; i < Size(E); i++) { if ( Find(E[i].va) != Find(E[i].vb) ) { Union(E[i].va, E[i].vb); res += E[i].waga; kr++; if ( kr == n ) { printf("%llu\n", res); return 0; } } } return 1; }
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 106 107 108 109 | //Michal Wos MIM UW #include <climits> #include <cstdlib> #include <iostream> #include <stdio.h> #include <vector> #include <algorithm> #include <set> #include <map> #include <cmath> #include <string> #include <string.h> #define pb push_back #define pii pair<int,int> #define vi vector<int> #define f first #define s second #define x first #define y second #define Size(x) ((int)(x).size()) #define FOR(z,b,e) for(__typeof(b) z = b; z<=e; z++ ) #define debon 1 #define deb(burak) if(debon) {cout<<"DEB-> "<<#burak<<": "<<burak<<endl;} #define debv(burak) if(debon) {cout<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<burak.size();zyx++) cout<<burak[zyx]<<" "; cout<<endl;} #define debt(burak,SIzE) if(debon) {cout<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<SIzE;zyx++) cout<<burak[zyx]<<" "; cout<<endl;} #define debend if(debon) {cout<<"_____________________"<<endl;} #define memcheck if(debon) {FILE *fp = fopen("/proc/self/status","r");while( !feof(fp) ) putchar(fgetc(fp));} using namespace std; typedef unsigned long long ULL; typedef long long LL; void readLL(LL *n) {register char c=0; while (c < 33) c=getc_unlocked(stdin); (*n)=0; while (c>32) {(*n)=(*n)*10LL + (c-'0'); c=getc_unlocked(stdin);}} const int inf=1073741824,mod=1e9+7; const int s=2010; int tab[s], ile[s]; int Find(int a) { if (tab[a] == a) { return a; } int fa = Find(tab[a]); tab[a] = fa; return fa; } bool Union(int a, int b) { int fa = Find(a); int fb = Find(b); if (fa == fb) { return false; } if (ile[fa] <= ile[fb]) { ile[fb] += ile[fa]; tab[fa] = fb; } else { ile[fa] += ile[fb]; tab[fb] = fa; } return true; } int main() { #define edge pair<ULL, pii> #define waga first #define va second.first #define vb second.second vector <edge> E; int n, c; ULL res = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { scanf("%d", &c); E.pb(edge(c,pii(i, j+1))); } } sort(E.begin(),E.end()); //init F&U for (int i=0; i<=n; i++) { tab[i] = i; ile[i] = 1; } //MST int kr = 0; for (int i = 0; i < Size(E); i++) { if ( Find(E[i].va) != Find(E[i].vb) ) { Union(E[i].va, E[i].vb); res += E[i].waga; kr++; if ( kr == n ) { printf("%llu\n", res); return 0; } } } return 1; } |