1 solutions

  • -4
    @ 2025-10-15 14:22:19

    寻宝

    四联通和传送门都看做是边,可以做一个有向图强连通缩点

    在得到的DAG上,用bitset维护连通关系即可

    ps:如果你内存有点紧,分析可以知道强连通缩点后连通块数量上限为nm2\frac{nm}{2}

    实际上数据也完全不满,完全不用担心bitset内存不够

    #include<bits/stdc++.h>
    using namespace std;
    const int NM=5e4+5,MN=2.5e4+5;
    const int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
    bool mp[NM];
    int n,m,k,q;
    int dfn[NM],low[NM],idx=0;
    int scc[NM],scc_cnt;
    int in[NM],sta[NM],top=0;
    bool in_stack[NM];
    vector<int> g[NM],G[NM];
    bitset<MN> can[MN];
    void tarjan(int u)
    {
    	dfn[u]=low[u]=++idx;
    	in_stack[u]=1,sta[++top]=u;
    	for(auto v:g[u])
    	{
    		if(!dfn[v])
    		{
    			tarjan(v);
    			low[u]=min(low[u],low[v]);
    		}else if(in_stack[v]) low[u]=min(low[u],dfn[v]);
    	}
    	if(dfn[u]==low[u])
    	{
    		scc_cnt++;
    		do
    		{
    			scc[sta[top]]=scc_cnt;
    			in_stack[sta[top]]=0;
    		}while(sta[top--]!=u);
    	}
    }
    inline void build()
    {
    	for(int i=1,xx,yy;i<=n;i++)
    		for(int j=1;j<=m;j++)
    		{
    			if(!mp[(i-1)*m+j]) continue;
    			for(int k=0;k<4;k++)
    			{
    				xx=i+dx[k],yy=j+dy[k];
    				if(!xx||!yy||xx>n||yy>m||!mp[(xx-1)*m+yy]) continue;
    				g[(i-1)*m+j].emplace_back((xx-1)*m+yy);
    				g[(xx-1)*m+yy].emplace_back((i-1)*m+j);
    			}
    		}
    }
    int main()
    {
    	freopen("treasure.in","r",stdin);
    	freopen("treasure.out","w",stdout);
    	scanf("%d%d%d%d",&n,&m,&k,&q);
    	char ch;
    	for(int i=1;i<=n;i++)
    	{
    		getchar();
    		for(int j=1;j<=m;j++) scanf("%c",&ch),mp[(i-1)*m+j]=(ch=='.');
    	}
    	build();
    	for(int i=1,x,y,x_,y_;i<=k;i++) scanf("%d%d%d%d",&x,&y,&x_,&y_),g[(x-1)*m+y].emplace_back((x_-1)*m+y_);
    	for(int i=1;i<=n*m;i++) if(!dfn[i]) tarjan(i);
    	for(int i=1;i<=n*m;i++) for(auto j:g[i]) if(scc[i]!=scc[j]) G[scc[j]].emplace_back(scc[i]),in[scc[i]]++;
    	queue<int> que;
    	for(int i=1;i<=scc_cnt;i++)
    	{
    		can[i][i]=1;
    		if(!in[i]) que.push(i);
    	}
    	while(!que.empty())
    	{
    		int u=que.front();que.pop();
    		for(auto v:G[u])
    		{
    			can[v]|=can[u];
    			if(!--in[v]) que.push(v);
    		}
    	}
    	int xst,yst,xed,yed;
    	while(q--)
    	{
    		scanf("%d%d%d%d",&xst,&yst,&xed,&yed);
    		if(can[scc[(xst-1)*m+yst]][scc[(xed-1)*m+yed]]) printf("1\n");
    		else printf("0\n"); 
    	}
    	return 0;
    }
    
    • 1

    Information

    ID
    75
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    9
    Tags
    (None)
    # Submissions
    104
    Accepted
    11
    Uploaded By