Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int maxn = 1005;
- int tree[maxn] = {0}, enemy[maxn][maxn] = {0}, n, m, p, a, b, ans = 0;
- int Find(int x) {
- int root = x;
- while (tree[root] >= 0)
- root = tree[root];
- while (root != x && x > 0) {
- int temp = tree[x];
- tree[x] = root;
- x = temp;
- }
- return root;
- }
- int Union(int a, int b) {
- int temp = tree[a] + tree[b];
- if (tree[a] > tree[b]) {
- tree[a] = b;
- tree[b] = temp;
- } else {
- tree[b] = a;
- tree[a] = temp;
- }
- }
- int main() {
- cin >> n >> m;
- for (int i = 0; i < maxn; i++)
- tree[i] = -1;
- for (int i = 0; i < m; i++) {
- cin >> p >> a >> b;
- if (p) {
- enemy[a][b] = 1;
- enemy[b][a] = 1;
- } else {
- if (Find(a) != Find(b))
- Union(a, b);
- }
- }
- for(int i = 1; i <= n; i++) {
- int first = 0;
- for(int l = 1; l <= n; l++){
- if (enemy[i][l] == 1) {
- if (first == 0) {
- first = l;
- continue;
- } else {
- if (Find(first) != Find(l))
- Union(first, l);
- }
- }
- }
- }
- for (int i = 1; i <= n; i++)
- if (tree[i] <= 0)
- ans++;
- cout << ans << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment