不知道是否可行, AC 1, 3, 9
查看原帖
不知道是否可行, AC 1, 3, 9
549027
__JiCanDuck__楼主2023/9/25 17:31

就是只记录父亲和吃什么,可行与否?

#include <iostream>
#define anss ans++; continue

using namespace std;

const int kMaxN = 5e4 + 5;

int n, m, ans, fa[kMaxN], eat[kMaxN], pr[kMaxN];

int findRt(int x) {
  return fa[x] == x ? x : findRt(fa[x]);
}

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int op, x, y; m; m--) {
    cin >> op >> x >> y;
    if (max(x, y) > n || x == y && op == 2) {
      ans++;
      continue;
    }
    x = findRt(x), y = findRt(y);
    if (op == 1) {
      if (eat[x] == y || eat[y] == x) {
        anss;
      }
      fa[x] = y;
      if (eat[x]) eat[y] = eat[x];
    } else {
      if (eat[y] == x || x == y) {
        anss;
      }
      eat[x] = y;
    }
  }
  cout << ans;
  return 0;
}
2023/9/25 17:31
加载中...