結果
問題 |
No.1451 集団登校
|
ユーザー |
|
提出日時 | 2021-03-31 15:19:21 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,795 bytes |
コンパイル時間 | 913 ms |
コンパイル使用メモリ | 77,256 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-12-14 23:36:22 |
合計ジャッジ時間 | 2,171 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | WA * 3 |
other | WA * 27 |
コンパイルメッセージ
main.cpp: In function 'int main(int, const char**)': main.cpp:48:25: warning: 'n' is used uninitialized [-Wuninitialized] 48 | std::vector<int> x(n,1); | ^ main.cpp:47:7: note: 'n' was declared here 47 | int n,m; | ^ main.cpp:51:26: warning: 'm' may be used uninitialized [-Wmaybe-uninitialized] 51 | for (size_t i = 0; i < m; i++) { | ^ main.cpp:47:9: note: 'm' was declared here 47 | int n,m; | ^
ソースコード
#include <iostream> #include <vector> using namespace std; // Union-Find struct UnionFind { vector<int> par, siz; // 初期化 UnionFind(int n) : par(n, -1) , siz(n, 1) { } // 根を求める int root(int x) { if (par[x] == -1) return x; // x が根の場合は x を返す else return par[x] = root(par[x]); } // x と y が同じグループに属するかどうか (根が一致するかどうか) bool issame(int x, int y) { return root(x) == root(y); } // x を含むグループと y を含むグループとを併合する bool unite(int x, int y) { // x, y をそれぞれ根まで移動する x = root(x); y = root(y); // すでに同じグループのときは何もしない if (x == y) return false; // union by size (y 側のサイズが小さくなるようにする) if (siz[x] < siz[y]) swap(x, y); // y を x の子とする par[y] = x; siz[x] += siz[y]; return true; } // x を含むグループのサイズ int size(int x) { return siz[root(x)]; } }; int main(int argc, char const *argv[]) { int n,m; std::vector<int> x(n,1); std::vector<bool> y(n,true); UnionFind uf(n); for (size_t i = 0; i < m; i++) { int a,b; std::cin >> a>>b; if (uf.size(a)>uf.size(b)) { y.at(uf.root(b))=false; uf.unite(a,b); } else if (uf.size(a)<uf.size(b)) { y.at(uf.root(a))=false; uf.unite(a,b); } else { x.at(uf.root(a))*=2; x.at(uf.root(b))*=2; uf.unite(a,b); } } for (size_t i = 0; i < n; i++) { if (y.at(i)) { std::cout << (1e9+8)/x.at(i) << '\n'; } else{ std::cout << 0 << '\n'; } } return 0; }