Language: C++

// Graph II - Mini#include<iostream>
#include<algorithm>
#include<vector>
#include<functional>
using namespace std;

#if 0
所用時間: 1h

感想: 思ったより実装すべきコード量が多かった。DisjointSetは今後も使う機会が多そうなので、クラス化してみた。
Pの配列を動的に確保するよう工夫した、良い復習になった。

#endif

int N, buff, M = 0, SUM = 0, A[101][101], cost[5000], dpt[5000], dst[5000];//A[Y][X]
vector<pair<int, int> > edges;

class disjointSet {
private:
    int* P;
    
public:
    disjointSet(){
        P = new int[100000];
    }
    disjointSet(int N){     
        P = new int[N];
    }
    ~disjointSet(){
        delete[] P;
    }

    void init(int N){ //木構造の初期化
        for(int i = 0; i <= N; ++i){
            P[i] = i;
        }
    }

    int root(int a){ //根の参照
        if(P[a] == a) return a;
        return (P[a] = root(P[a]));
    }

    bool is_same_set(int a, int b){ //同じ根か?
        return root(a) == root(b);
    }

    void unite(int a, int b){
        P[root(a)] = root(b); //aのrootをbの子にする
    }
};


void preprocess(){//辺の前処理
  for(int r = 0; r < N; ++r){
    for(int c = r+1; c < N; ++c){
      if (A[r][c] > -1) {
        cost[M] = A[r][c];
        dpt[M] = r;
        dst[M] = c;
        M++;
      }
    }
  }

  //for(int i = 0; i < M; ++i) cout << cost[i] << endl;

  for(int i = 0; i < M; ++i){
    edges.push_back(make_pair(cost[i], i));
  }
  for(int i = 0; i < M; ++i) cout << edges[i].first << " " << edges[i].second << endl;

  sort(edges.begin(), edges.end());

  for(int i = 0; i < M; ++i) cout << edges[i].first << " " << edges[i].second << endl;

}

void solve(){
  disjointSet Union(100000);

  int ans = 0;
  preprocess();

  Union.init(N);

  for(int i = 0; i < M; ++i){
    int index = edges[i].second;
    if(Union.is_same_set(dpt[index], dst[index])) continue;
    else {
      Union.unite(dpt[index], dst[index]);
      cout << "unite " << dpt[index] << " and " << dst[index] << endl;
      ans += cost[index];
    }
  }
  cout << ans << endl;
}

int main(){

cin >> N;

for(int i = 0; i < N; ++i){
  for(int j = 0; j < N; ++j){
    cin >> buff;
    A[i][j] = buff;
    }
  }

  //行列の表示
  // cout << "Matrix:" << endl;
  // for(int row=0; row < N; ++row){
  //   for(int col = 0; col < N; ++col){
  //     cout << A[row][col];
  //   }
  //   cout << endl;
  // }

  solve();
}
mum Spanning Tree
// 問題URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A&lang=jp
//
// 使うライブラリも多く、難しく感じた...
#include <iostream>
#include <algorithm>

using namespace std;

int n, buff, cost[101][101]={}, P[101], M = 0, ans = 0;
pair<int, int> edges[10010], vertices[10010];

void init(int n){
  for(int i = 0; i < n; ++i){
    P[i] = i;
  }
}

int root(int x){
  if(P[x]==x) return x;
  P[x] = root(P[x]);
  return P[x];
}

bool is_same_set(int x, int y){
  return root(x) == root(y);
}

void unite(int x, int y){
  P[root(x)] = root(y);
}

int main(){
  cin >> n;

  for(int row = 0; row < n; ++row){
    for(int col = 0; col < n; ++col){
      cin >> buff;
      cost[row][col] = buff; //隣接行列の読込
    }
  }

  for(int row = 0; row < n; ++row){
    for(int col = 0; col < row; ++col){
      if(cost[row][col]>0){
        edges[M].first = cost[row][col];
        edges[M].second = M;
        // cout << M << ": (" << row << ", " << col << ") = " << edges[M].first << endl;
        vertices[M].first = row, vertices[M].second = col;
        M++; //辺の数を管理、idとして利用
      }
    }
  }

  init(n);

  sort(edges, edges+M); //重みの小さい順に整列

  for(int i = 0; i <= M; ++i){
    int index = edges[i].second; //辺のidを取り出す
    if(!is_same_set(vertices[index].first, vertices[index].second)){ //辺の両端が繋がっていなければ
      unite(vertices[index].first, vertices[index].second); //両端を繋げる
      // cout << "unite " << vertices[index].first << " and " << vertices[index].second << endl;
      ans += edges[i].first; //コストを加える
    }
  }
  cout << ans << endl;
}



Compiler output:
a.cc:10:7: error: extended character 。 is not valid in an identifier
   10 | 感想: 思ったより実装すべきコード量が多かった。DisjointSetは今後も使う機会が多そうなので、クラス化してみた。
      |       ^
a.cc:10:7: error: extended character 、 is not valid in an identifier
a.cc:10:7: error: extended character 。 is not valid in an identifier
a.cc:11:1: error: extended character 、 is not valid in an identifier
   11 | Pの配列を動的に確保するよう工夫した、良い復習になった。
      | ^
a.cc:11:1: error: extended character 。 is not valid in an identifier
a.cc: In function 'void preprocess()':
a.cc:71:30: error: 'cout' was not declared in this scope
   71 |   for(int i = 0; i < M; ++i) cout << edges[i].first << " " << edges[i].second << endl;
      |                              ^~~~
a.cc:5:1: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    4 | #include<functional>
  +++ |+#include <iostream>
    5 | using namespace std;
a.cc:71:82: error: 'endl' was not declared in this scope
   71 |   for(int i = 0; i < M; ++i) cout << edges[i].first << " " << edges[i].second << endl;
      |                                                                                  ^~~~
a.cc:5:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
    4 | #include<functional>
  +++ |+#include <ostream>
    5 | using namespace std;
a.cc:75:30: error: 'cout' was not declared in this scope
   75 |   for(int i = 0; i < M; ++i) cout << edges[i].first << " " << edges[i].second << endl;
      |                              ^~~~
a.cc:75:30: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:75:82: error: 'endl' was not declared in this scope
   75 |   for(int i = 0; i < M; ++i) cout << edges[i].first << " " << edges[i].second << endl;
      |                                                                                  ^~~~
a.cc:75:82: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
a.cc: In function 'void solve()':
a.cc:92:7: error: 'cout' was not declared in this scope
   92 |       cout << "unite " << dpt[index] << " and " << dst[index] << endl;
      |       ^~~~
a.cc:92:7: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:92:66: error: 'endl' was not declared in this scope
   92 |       cout << "unite " << dpt[index] << " and " << dst[index] << endl;
      |                                                                  ^~~~
a.cc:92:66: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
a.cc:96:3: error: 'cout' was not declared in this scope
   96 |   cout << ans << endl;
      |   ^~~~
a.cc:96:3: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:96:18: error: 'endl' was not declared in this scope
   96 |   cout << ans << endl;
      |                  ^~~~
a.cc:96:18: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
a.cc: In function 'int main()':
a.cc:101:1: error: 'cin' was not declared in this scope
  101 | cin >> N;
      | ^~~
a.cc:101:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc: At global scope:
a.cc:121:1: error: 'mum' does not name a type
  121 | mum Spanning Tree
      | ^~~
In file included from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:125:
/usr/include/c++/14/bits/basic_string.h:4064:60: error: wrong number of template arguments (1, should be 3)
 4064 |     operator>>(basic_istream<char>& __is, basic_string<char>& __str);
      |                                                            ^
/usr/include/c++/14/bits/basic_string.h:86:11: note: provided for 'template<class _CharT, class _Traits, class _Alloc> class std::__cxx11::basic_string'
   86 |     class basic_string
      |           ^~~~~~~~~~~~
/usr/include/c++/14/bits/basic_string.h:4064:5: error: template-id 'operator>><>' for 'std::basic_istream<char>& std::operator>>(basic_istream<char>&, int&)' does not match any template declaration
 4064 |     operator>>(basic_istream<char>& __is, basic_string<char>& __str);
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:4059:5: note: candidates are: 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(basic_istream<_CharT, _Traits>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 4059 |     operator>>(basic_istream<_CharT, _Traits>& __is,
      |     ^~~~~~~~
In file included from /usr/include/c++/14/bits/memory_resource.h:38,
                 from /usr/include/c++/14/vector:87,
                 from a.cc:3:
/usr/include/c++/14/cstddef:131:5: note:                 'template<class _IntegerType> constexpr std::__byte_op_t<_IntegerType> std::operator>>(byte, _IntegerType)'
  131 |     operator>>(byte __b, _IntegerType __shift) noexcept
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:4139:57: error: wrong number of template arguments (1, should be 3)
 4139 |     getline(basic_istream<char>& __in, basic_string<char>& __str,
      |                                                         ^
/usr/include/c++/14/bits/basic_string.h:86:11: note: provided for 'template<class _CharT, class _Traits, class _Alloc> class std::__cxx11::basic_string'
   86 |     class basic_string
      |           ^~~~~~~~~~~~
/usr/include/c++/14/bits/basic_string.h:4139:5: error: template-id 'getline<>' for 'std::basic_istream<char>& std::getline(basic_istream<char>&, int&, char)' does not match any template declaration
 4139 |     getline(basic_istream<char>& __in, basic_string<char>& __str,
      |     ^~~~~~~
/usr/include/c++/14/bits/basic_string.h:4132:5: note: candidates are: 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_istream<_CharT, _Traits>&&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 4132 |     getline(basic_istream<_CharT, _Traits>&& __is,
      |     ^~~~~~~
/usr/include/c++/14/bits/basic_string.h:4125:5: note:                 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_istream<_CharT, _Traits>&&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)'
 4125 |     getline(basic_istream<_CharT, _Traits>&& __is,
      |     ^~~~~~~
/usr/include/c++/14/bits/basic_string.h:4117:5: note:                 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_istream<_CharT, _Traits>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 4117 |     getline(basic_istream<_CharT, _Traits>& __is,
      |     ^~~~~~~
/usr/include/c++/14/bits/basic_string.h:4100:5: note:                 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_istream<_CharT, _Traits>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)'
 4100 |     getline(basic_istream<_CharT, _Traits>& __is,
      |     ^~~~~~~
/usr/include/c++/14/bits/basic_string.h:4145:63: error: wrong number of template arguments (1, should be 3)
 4145 |     getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
      |                                                               ^
/usr/include/c++/14/bits/basic_string.h:86:11: note: provided for 'template<class _CharT, class _Traits, class _Alloc> class std::__cxx11::basic_string'
   86 |     class basic_string
      |           ^~~~~~~~~~~~
/usr/include/c++/14/bits/basic_string.h:4145:5: error: template-id 'getline<>' for 'std::basic_istream<wchar_t>& std::getline(basic_istream<wchar_t>&, int&, wchar_t)' does not match any template declaration
 4145 |     getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
      |     ^~~~~~~
/usr/include/c++/14/bits/basic_string.h:4132:5: note: candidates are: 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_istream<_CharT, _Traits>&&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 4132 |     getline(basic_istream<_CharT, _Traits>&& __is,
      |     ^~~~~~~
/usr/include/c++/14/bits/basic_string.h:4125:5: note:                 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_istream<_CharT, _Traits>&&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)'
 4125 |     getline(basic_istream<_CharT, _Traits>&& __is,
      |     ^~~~~~~
/usr/include/c++/14/bits/basic_string.h:4117:5: note:                 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_istream<_CharT, _Traits>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 4117 |     getline(basic_istream<_CharT, _Traits>& __is,
      |     ^~~~~~~
/usr/include/c++/14/bits/basic_string.h:4100:5: note:                 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_istream<_CharT, _Traits>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)'
 4100 |     getline(basic_istream<_CharT, _Traits>& __is,
      |     ^~~~~~~
/usr/include/c++/14/bits/basic_string.h:4164:14: error: 'string' does not name a type; did you mean 'stdin'?
 4164 |   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
      |              ^~~~~~
      |              stdin
/usr/include/c++/14/bits/basic_string.h: In function 'int std::__cxx11::stoi(const int&, std::size_t*, int)':
/usr/include/c++/14/bits/basic_string.h:4165:69: error: request for mem