-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Indev-0.1 updates: Fixed BIT, Added DSU.
- Loading branch information
oitl-5ab
authored
Sep 13, 2020
1 parent
a5f8425
commit ce8a05f
Showing
5 changed files
with
152 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#ifndef _OITL_ASSETS_DISJOINT_SET_UNION | ||
#define _OITL_ASSETS_DISJOINT_SET_UNION 1 | ||
|
||
#include <vector> | ||
|
||
namespace oitl | ||
{ | ||
class disjoint_set_union | ||
{ | ||
private: | ||
|
||
std::vector<int> __dsu; | ||
|
||
int _Find_root(int __id) | ||
{ | ||
if (__id != __dsu[__id]) | ||
__dsu[__id] = _Find_root(__dsu[__id]); | ||
|
||
return __dsu[__id]; | ||
} | ||
|
||
public: | ||
|
||
disjoint_set_union(int __siz = 0) | ||
{ | ||
__dsu.resize(__siz); | ||
for (register int __i = 0; __i < __siz; ++__i) | ||
__dsu[__i] = __i; | ||
} | ||
|
||
bool empty() const { return __dsu.size() == 0; } | ||
int size() const { return __dsu.size(); } | ||
void reset() { __dsu.clear(); } | ||
|
||
bool same_set(int __ap, int __bp) | ||
{ | ||
return _Find_root(__ap) == _Find_root(__bp); | ||
} | ||
|
||
void unite_sets(int __ap, int __bp) | ||
{ | ||
if (same_set(__ap, __bp)) | ||
return; | ||
|
||
__dsu[_Find_root(__ap)] = _Find_root(__bp); | ||
} | ||
|
||
int add_element() | ||
{ | ||
__dsu.push_back(__dsu.size()); | ||
return __dsu.size() - 1; | ||
} | ||
|
||
void clear() | ||
{ | ||
for (register int __i = 0; __i < __dsu.size(); ++__i) | ||
__dsu[__i] = __i; | ||
} | ||
|
||
void resize(int __siz) | ||
{ | ||
if (__siz > __dsu.size()) | ||
{ | ||
for (register int __i = __dsu.size(); __i < __siz; ++__i) | ||
__dsu.push_back(__i); | ||
} | ||
else | ||
__dsu.resize(__siz); | ||
} | ||
}; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef _OITL_DISJOINT_SET_UNION_LIB | ||
#define _OITL_DISJOINT_SET_UNION_LIB 1 | ||
|
||
#include "./assets/disjoint_set_union.hpp" | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include "../disjoint_set_union" | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
const int max_n = 100000, mod = 998244353; | ||
int ans = 0; | ||
|
||
void addans(bool res) | ||
{ | ||
ans = (2 * ans + res) % mod; | ||
} | ||
|
||
int main() | ||
{ | ||
freopen("1.in", "r", stdin); | ||
|
||
oitl::disjoint_set_union dsu; | ||
|
||
int n, m, opt, u, v; | ||
|
||
scanf("%d%d", &n, &m); | ||
for (int i = 0; i < n; i++) | ||
dsu.add_element(); | ||
|
||
while (m--) | ||
{ | ||
scanf("%d%d%d", &opt, &u, &v); | ||
|
||
if (opt) | ||
addans(dsu.same_set(u, v)); | ||
else | ||
dsu.unite_sets(u, v); | ||
|
||
if (!(m % 100000)) | ||
printf("%d\n", m); | ||
} | ||
|
||
printf("%d\n", ans); | ||
|
||
return 0; | ||
} |