Submission #00199
ソースコード
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 | #include <bits/stdc++.h> #define fin << "\n" #define lol long long int #define nintendo switch #define usort(a) sort(a.begin() , a.end()) #define dsort(a) sort(a.rbegin() , a.rend()) #define space << " " << #define push_pair(a , b) push_back(make_pair(a , b)) #define check cout << 1 fin; #define fixout(a) cout << fixed << setprecision(a) #define bs(a , b) binary_search(a.begin() , a.end() , b) #define ub(a , b) upper_bound(a.begin() , a.end() , b) #define lb(a , b) lower_bound(a.begin() , a.end() , b) #define Iter(type) type::iterator using namespace std; struct UnionFind { vector< int > par; UnionFind() { } UnionFind( int n) : par(n, -1) { } void init( int n) { par.assign(n, -1); } int root( int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool same( int x, int y) { return root(x) == root(y); } bool merge( int x, int y) { x = root(x), y = root(y); if (x == y) return false ; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true ; } int size( int x) { return -par[root(x)]; } friend ostream& operator << (ostream &s, UnionFind uf) { map< int , vector< int >> groups; for ( int i = 0; i < uf.par.size(); ++i) { int r = uf.root(i); groups[r].push_back(i); } for ( const auto &it : groups) { s << "group: " ; for (auto v : it.second) s << v << " " ; s << endl; } return s; } }; int main( void ){ ios::sync_with_stdio( false ); cin.tie(0); int n; cin >> n; UnionFind uni(n); for ( int i = 0;i < n;i ++){ int c; cin >> c; if (c) uni.merge(i , (i + 1) % n); } set< int > ans; for ( int i = 0;i < n;i ++){ ans.insert(uni.root(i)); } cout << ans.size() fin; return (0); } |
ステータス
項目 | データ |
---|---|
問題 | 0004 - グループ |
ユーザー名 | Koba_gochiusa |
投稿日時 | 2023-08-28 11:33:45 |
言語 | C++17 |
状態 | Accepted |
得点 | 20 |
ソースコード長 | 1983 Byte |
最大実行時間 | 24 ms |
最大メモリ使用量 | 700 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 20 / 20 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in1 | AC | 20 ms | 600 KB |
1
|
in2 | AC | 18 ms | 456 KB |
1
|
in3 | AC | 19 ms | 400 KB |
1
|
in4 | AC | 17 ms | 488 KB |
1
|
in5 | AC | 19 ms | 700 KB |
1
|
in6 | AC | 21 ms | 656 KB |
1
|
in7 | AC | 19 ms | 608 KB |
1
|
in8 | AC | 17 ms | 564 KB |
1
|
in9 | AC | 21 ms | 640 KB |
1
|
in10 | AC | 19 ms | 560 KB |
1
|
in11 | AC | 24 ms | 648 KB |
1
|
in12 | AC | 18 ms | 604 KB |
1
|
in13 | AC | 17 ms | 560 KB |
1
|
in14 | AC | 17 ms | 636 KB |
1
|
in15 | AC | 19 ms | 580 KB |
1
|
in16 | AC | 24 ms | 528 KB |
1
|
in17 | AC | 21 ms | 608 KB |
1
|
in18 | AC | 22 ms | 560 KB |
1
|
in19 | AC | 22 ms | 508 KB |
1
|
in20 | AC | 23 ms | 544 KB |
1
|
in21 | AC | 16 ms | 628 KB |
1
|
in22 | AC | 22 ms | 576 KB |
1
|