40,为什么开三个数组过不去?
查看原帖
40,为什么开三个数组过不去?
812597
kiropo楼主2024/3/15 21:59
//luogu P2024.cpp

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pii pair<ll,ll>


const ll mod = 1000000007;
const int N = 500005;

bool isprime(int n) { if (n == 1)return 0; for (int i = 2; i <= sqrt(n); i++)if (n % i == 0)return 0; return 1; }
ll gcd(ll a, ll b) { ll temp; if (a < b)swap(a, b); while (b != 0) { temp = a % b; a = b; b = temp; }return a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll qpow(ll a, ll b) { ll ans = 1, base = a; while (b) { if (b & 1)ans = (ans * base) % mod; base = (base * base) % mod; b = b >> 1; }return ans; }//快速幂
ll A(ll n, ll m) { ll res = 1; for (ll i = m; i >= 1; i--) { res *= n % mod; n--; }return res; }

int fa[500005], up[500005], dn[500005];

int find(int x) {
	if (fa[x] == x)return fa[x];
	return fa[x] = find(fa[x]);
}

void solve() {
	int n, m; cin >> n >> m;
	for (int i = 1; i <= n; i++)fa[i] = i;
	int ans = 0;
	while (m--) {
		int t, x, y; cin >> t >> x >> y;
		if (x > n || y > n) {
			ans++; continue;
		}
		if (t == 1) {
			if (x == y)continue;
			int ux = up[x], dx = dn[x], uy = up[y], dy = dn[y];

			bool i = ((find(uy) != find(x)) && (find(dy) != find(x)));
			bool j = ((find(ux) != find(y)) && (find(dx) != find(y)));

			if (i && j) {
				fa[find(y)] = find(x);

				if (ux == 0)up[x] = uy;
				else if (uy == 0)up[y] = ux;
				else fa[find(uy)] = find(ux);

				if (dx == 0)dn[x] = dy;
				else if (dy == 0)dn[y] = dx;
				else fa[find(dy)] = find(dx);
			}
			else ans++;
		}
		else {
			if (x == y) {
				ans++; continue;
			}
			int ux = up[x], dx = dn[x], uy = up[y], dy = dn[y];

			bool i = ((find(y) != find(x)) && (find(dy) != find(x)));
			bool j = (ux == 0 || ((find(ux) != find(y)) && find(ux) != find(uy)));
			bool k = (dx == 0 || ((find(dx) != find(dy)) && find(dx) != find(uy)));

			if (i && j && k) {


				if (uy == 0)up[y] = x;
				else fa[find(uy)] = find(x);

				if (dx == 0)dn[x] = y;
				else fa[find(y)] = find(dx);

				if (ux == 0) {
					up[x] = dy;
					if (dy)up[dy] = y, dn[dy] = x;
				}
				else if (dy == 0) {
					dn[y] = ux;
					if (ux)up[ux] = y, dn[ux] = x;
				}
				else fa[find(dy)] = find(ux);
			}
			else ans++;
		}
	}
	cout << ans << endl;
}



int main() {
	std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t = 1;
	//cin >> t;

	while (t--) {
		solve();
	}

	return 0;

}

/*
   *
   *  ┏┓   ┏┓+ +
   * ┏┛┻━━━┛┻┓ + +
   * ┃       ┃
   * ┃   ━   ┃ ++ + + +
   *  ████━████+
   *  ◥██◤ ◥██◤ +
   * ┃   ┻   ┃
   * ┃       ┃ + +
   * ┗━┓   ┏━┛
   *   ┃   ┃ + + + +Code is far away from  
   *   ┃   ┃ + bug with the animal protecting
   *   ┃    ┗━━━┓ 神兽保佑,代码无bug 
   *   ┃        ┣┓
   *    ┃        ┏┛
   *     ┗┓┓┏━┳┓┏┛ + + + +
   *    ┃┫┫ ┃┫┫
   *    ┗┻┛ ┗┻┛+ + + +
   */
2024/3/15 21:59
加载中...