跳到正文

题解

题解:P2403 [SDOI2010] 所驼门王的宝藏

AstralBrahma约 8 分钟
原文
文章信息与版本

文章信息

收录
2026年3月5日
更新
2026年3月5日
篇幅
约 8 分钟
题目
P2403

版本

  1. v2当前
  2. v1

极为恶心的图论题目。

不难发现不能直接建图,所以说考虑缩点。很显然,处在同一横行的 11 类传送门是可以相互到达的,所以说把它们合并成一个点记为 xix_i。同理,处在同一纵列的 22 类传送门也是可以相互到达的,也可以将它们合并成同一个点,记为 yiy_i。然后每一个 33 类传送门单独处理,新编号记为 ziz_i。

然后对于同一横行的传送门,就是 11 类传送门的新编号 xix_i 连向这一行的其它非 11 类传送门的新编号 yiy_i 或 ziz_i。纵列同理,应为 yiy_i 连向 xix_i 和 yiy_i。

然后考虑 33 类传送门怎么做。很显然,采用 pair 加哈希将每一个坐标存下来,然后对于每一个 33 类传送门,都检测其周围八个点是否存在传送门,如果有,则将 ziz_i 连向 xix_i 或 yiy_i。

这样新建图可以保证其边数是 O(k)\cal O(k) 级别的。

然后在新图上跑 tarjan 加二次缩点加拓扑排序即可,整体的时间复杂度是 O(klog⁡k)\cal O(k \log k) ,空间复杂度是 O(N+E)\cal O(N + E) 的,显然可以通过。

细节有一点多,调试代码也没有删,可以用来调试你的代码。

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int INF=1e6+10;
int dr[8][2]={{0,1},{1,0},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};

int r,c,n,nodek;
int hid[INF],lid[INF],a[INF];
vector<pair<int,int> > h[INF],l[INF],ry;
map<pair<int,int>,int> M;
vector<int> mp[INF];
int ne[INF];
//
//
int dfn[INF],low[INF],tim,w[INF];
int st[INF],top,scc,ne2[INF],in[INF];
ll dis[INF];
bool in_st[INF];
vector<int> SCC[INF],G[INF];
namespace Tar{
	inline void tarjan(int u){
		dfn[u]=low[u]=++tim;
		st[++top]=u,in_st[u]=1;
		for (int v:mp[u]){
			if (!dfn[v]){
				tarjan(v);
				low[u]=min(low[u],low[v]);
			}else if (in_st[v])low[u]=min(low[u],dfn[v]);
		}
		if (low[u]==dfn[u]){
			scc++;
			while (st[top]!=u)SCC[scc].push_back(st[top]),in_st[st[top--]]=0;
			SCC[scc].push_back(st[top]),in_st[st[top--]]=0;
		}
	}
	inline void ccc(){
		for (int i=1;i<=scc;i++)for (int u:SCC[i])ne2[u]=i,w[i]+=a[u];
		for (int u=1;u<=nodek;u++)for (int v:mp[u])if (ne2[u]!=ne2[v])G[ne2[u]].push_back(ne2[v]),in[ne2[v]]++;
	}
	inline void topo(){
		queue<int> q;
		for (int i=1;i<=scc;i++)if (!in[i])dis[i]=w[i],q.push(i);
		while (!q.empty()){
			int u=q.front();q.pop();
			for (int v:G[u]){
				dis[v]=max(dis[v],dis[u]+w[v]);
				in[v]--;
				if (!in[v])q.push(v);
			}
		}
		ll maxn=0;
		for (int i=1;i<=scc;i++)maxn=max(maxn,dis[i]);
		cout<<maxn;
//		cout<<"\nSCC:\n";
//		for (int i=1;i<=scc;i++){
//			cout<<SCC[i].size()<<" ";
//			for (int u:SCC[i])cout<<u<<" ";
//			cout<<"\n";
//		}
	}
}
namespace yixing{
	inline void prepare(){
		for (int i=1;i<=r;i++){
			if (h[i].empty())continue;
			bool flag=0;
			for (pair<int,int> u:h[i]){
				if (u.first==1&&!flag)flag=1,nodek++,hid[i]=nodek;
				if (u.first==1)ne[u.second]=nodek,a[nodek]++;
			}
		}
		for (int i=1;i<=c;i++){
			if (l[i].empty())continue;
			bool flag=0;
			for (pair<int,int> u:l[i]){
				if (u.first==2&&!flag)flag=1,nodek++,lid[i]=nodek;
				if (u.first==2)ne[u.second]=nodek,a[nodek]++;
			}
		}
		for (int i=1;i<=r;i++){
			if (h[i].empty())continue;
			for (pair<int,int> u:h[i]){
				if (u.first==1)continue;
				if (u.first==3&&!ne[u.second])ne[u.second]=++nodek,a[nodek]=1;
				if (hid[i])mp[hid[i]].push_back(ne[u.second]);
			}
		}
		for (int i=1;i<=c;i++){
			if (l[i].empty())continue;
			for (pair<int,int> u:l[i]){
				if (u.first==2)continue;
				if (u.first==3&&!ne[u.second])ne[u.second]=++nodek,a[nodek]=1;
				if (lid[i])mp[lid[i]].push_back(ne[u.second]);
			}
		}
		for (pair<int,int> u:ry){
			int x=M[u];
			for (int i=0;i<8;i++){
				int tx=u.first+dr[i][0],ty=u.second+dr[i][1];
				int y=M[{tx,ty}];
				if (y)mp[ne[x]].push_back(ne[y]);
			}
		}
//		cout<<"ne\n";
//		for (int i=1;i<=n;i++)cout<<ne[i]<<" ";
//		cout<<"\nhid\n";
//		for (int i=1;i<=r;i++)cout<<hid[i]<<" ";
//		cout<<"\nlid\n";
//		for (int i=1;i<=c;i++)cout<<lid[i]<<" ";
//		cout<<"\nedges\n";
//		for (int i=1;i<=nodek;i++){
//			for (int u:mp[i])cout<<u<<" ";
//			cout<<"\n";
//		}
	}
	inline void Main(){
		cin>>n>>r>>c;
		for (int i=1;i<=n;i++){
			int x,y,T;cin>>x>>y>>T;
			M[{x,y}]=i;
			h[x].push_back({T,i});
			l[y].push_back({T,i});
			if (T==3)ry.push_back({x,y});
		}
		prepare();
		for (int i=1;i<=nodek;i++)if (!dfn[i])Tar::tarjan(i);
		Tar::ccc();
		Tar::topo();
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	yixing::Main();
	return 0;
}
原文