前提
今union find木の勉強をしているのですが、コード自体は蟻本に書いているものをそのままパクってきたのですが、なぜかエラーが出ます。
発生している問題・エラーメッセージ
./Main.cpp: In function ‘void init(int)’: ./Main.cpp:10:12: error: reference to ‘rank’ is ambiguous 10 | rank[i] = 0; | ^~~~ In file included from /usr/include/c++/9/bits/move.h:55, from /usr/include/c++/9/bits/nested_exception.h:40, from /usr/include/c++/9/exception:144, from /usr/include/c++/9/ios:39, from /usr/include/c++/9/ostream:38, from /usr/include/c++/9/iostream:39, from ./Main.cpp:1: /usr/include/c++/9/type_traits:1257:12: note: candidates are: ‘template<class> struct std::rank’ 1257 | struct rank | ^~~~ ./Main.cpp:6:15: note: ‘std::vector<int> rank’ 6 | vector<int>rank(MAX_int); | ^~~~ ./Main.cpp: In function ‘void unite(int, int)’: ./Main.cpp:19:11: error: reference to ‘rank’ is ambiguous 19 | if(rank[x] <rank[y])par[x] == y; | ^~~~ In file included from /usr/include/c++/9/bits/move.h:55, from /usr/include/c++/9/bits/nested_exception.h:40, from /usr/include/c++/9/exception:144, from /usr/include/c++/9/ios:39, from /usr/include/c++/9/ostream:38, from /usr/include/c++/9/iostream:39, from ./Main.cpp:1: /usr/include/c++/9/type_traits:1257:12: note: candidates are: ‘template<class> struct std::rank’ 1257 | struct rank | ^~~~ ./Main.cpp:6:15: note: ‘std::vector<int> rank’ 6 | vector<int>rank(MAX_int); | ^~~~ ./Main.cpp:19:20: error: reference to ‘rank’ is ambiguous 19 | if(rank[x] <rank[y])par[x] == y; | ^~~~ In file included from /usr/include/c++/9/bits/move.h:55, from /usr/include/c++/9/bits/nested_exception.h:40, from /usr/include/c++/9/exception:144, from /usr/include/c++/9/ios:39, f...
該当のソースコード
c++
1#include <iostream> 2#include<vector> 3using namespace std; 4static const int MAX_int = 100000000; 5 vector<int>par(MAX_int); 6 vector<int>rank(MAX_int); 7 void init(int n){ 8 for(int i=0;i<n;i++){ 9 par[i] = i; 10 rank[i] = 0; 11 } 12 } 13 int find(int x){ 14 if(par[x] == x)return x; 15 else return par[x] == find(par[x]); 16 } 17 void unite(int x,int y){ 18 x = find(x);y = find(y);if(y == x)return; 19 if(rank[x] <rank[y])par[x] == y; 20 else { 21 par[y] == x; 22 if(rank[x] == rank[y])rank[x]++; 23 } 24 } 25 bool same(int x,int y){ 26 return find[x] == find[y]; 27 } 28int main(void){ 29 int N,M;cin >> N >> M; 30 vector<pair<int,int>>a(M); 31 for(int i=0;i<M;i++){ 32 cin >> a[i].first >> a[i].second; 33 } 34 map<int,int>b; 35 for(int i=0;i<M;i++){ 36 b[a[i].first-1]++;b[a[i].second-1]++; 37 } 38 bool ans = false; 39 for(int i=0;i<M;i++){ 40 if(same(a[i].first ,a[i].second))ans = true; 41 unite(a[i].first,a[i].second); 42 } 43 for(int i = 0;i<N;i++){ 44 if(b[i] > 2 || ans){ 45 cout << "No" << endl;return 0; 46 } 47 } 48 cout << "Yes" << endl; 49} 50
試したこと
補足情報(FW/ツールのバージョンなど)
int関数の中は無視してください。
> ./Main.cpp:10:12: error: reference to ‘rank’ is ambiguous
'rank' が曖昧。
> /usr/include/c++/9/type_traits:1257:12: note: candidates are: ‘template<class> struct std::rank’
候補は 'template<class> struct std::rank'
> ./Main.cpp:6:15: note: ‘std::vector<int> rank’
そして 'std::vector<int> rank'
回答1件
あなたの回答
tips
プレビュー