質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++11

C++11は2011年に容認されたC++のISO標準です。以前のC++03に代わるもので、中枢の言語の変更・修正、標準ライブラリの拡張・改善を加えたものです。

Q&A

解決済

1回答

459閲覧

union find木の実装

busa145

総合スコア7

C++11

C++11は2011年に容認されたC++のISO標準です。以前のC++03に代わるもので、中枢の言語の変更・修正、標準ライブラリの拡張・改善を加えたものです。

0グッド

0クリップ

投稿2022/09/23 09:22

前提

今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関数の中は無視してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2022/09/23 11:58

> ./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'
guest

回答1

0

ベストアンサー

struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化 for(int i = 0; i < N; i++) par[i] = i; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); //xの根をrx int ry = root(y); //yの根をry if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } };

https://qiita.com/ofutonton/items/c17dfd33fc542c222396

投稿2022/09/23 09:51

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問