正解TLE+MLE
查看原帖
正解TLE+MLE
482610
Mortidesperatslav楼主2024/3/10 16:38
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define mxn 2100005
using namespace std;
using namespace __gnu_pbds;
int dfn[mxn], low[mxn], col[mxn], vl[mxn], lt[mxn], rt[mxn];
int n, m, r, c, a1, a2, cnt, ign, ccnt;
bool vis[mxn];
gp_hash_table<int, vector<int> > G;
stack<int> s;
queue<int> q;
map<pair<int, int>, int> ing;
struct node{
	int x, y, op;
}p[mxn];
void tarjan(int u){
	dfn[u] = low[u] = ++cnt;
	s.push(u);
	vis[u] = 1;
	for (auto &v : G[u]){
		if (!dfn[v]){
			tarjan(v);
			low[u] = min(low[u], low[v]);
		}else if (vis[v])
			low[u] = min(low[u], dfn[v]);
	}
	if (dfn[u] == low[u]){
	//	ccnt++;
		while (!s.empty()){
			int v = s.top();
			s.pop();
			vis[v] = 0;
			col[v] = u;
			if (v > r + c)
				vl[u]++;
		//	cout << u << " :: " << v << " " << vl[u] << "\n";
			if (u == v)
				break;
		}
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> m >> r >> c;
	n = m + r + c;
	for (int i = 1; i <= m; i++){
		cin >> p[i].x >> p[i].y >> p[i].op;
		ing[make_pair(p[i].x, p[i].y)] = i + r + c;
	}
	for (int i = 1; i <= m; i++){
		if (p[i].op == 1){
			G[p[i].y + r].push_back(i + r + c);
			G[p[i].x].push_back(i + r + c);
			G[i + r + c].push_back(p[i].x);
		}else if (p[i].op == 2){
			G[p[i].y + r].push_back(i + r + c);
			G[p[i].x].push_back(i + r + c);
			G[i + r + c].push_back(p[i].y + r);
		}else{
			G[p[i].y + r].push_back(i + r + c);
			G[p[i].x].push_back(i + r + c);
			for (int j = p[i].x - 1; j <= p[i].x + 1; j++)
				for (int k = p[i].y - 1; k <= p[i].y + 1; k++){
					if(ing[make_pair(j, k)])
						G[i + r + c].push_back(ing[make_pair(j, k)]);
				}
		}
	}
	for (int i = 1; i <= n; i++)
		if (!dfn[i])
			tarjan(i);
	memset(low, 0, sizeof(low));
	memset(dfn, 0, sizeof(dfn));
	int ncnt = 0;
	for (int i = 1; i <= n; i++){
		for (auto v : G[i]){
			if (col[i] == col[v])
				continue;
			lt[++ncnt] = col[i];
			rt[ncnt] = col[v];
		}
		if (col[i] == i)
			G[i].clear();
	}
	for (int i = 1; i <= ncnt; i++){
		G[col[lt[i]]].push_back(col[rt[i]]);
		low[col[rt[i]]]++;
	}
	for (int i = 1; i <= n; i++){
		if (!low[i] && col[i] == i){
			q.push(i);
			dfn[i] = vl[i];
		}
	}
	while (!q.empty()){
		int u = q.front();
		q.pop();
		for (auto v : G[u]){
			dfn[v] = max(dfn[v], dfn[u] + vl[v]);
			low[v]--;
			if (!low[v])
				q.push(v);
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++){
		ans = max(ans, dfn[i]);
	//	cout << i << " " << f[i] << "\n";
	}
	cout << ans;
}

洛谷你就不能放大点时空限制吗。

2024/3/10 16:38
加载中...