P9641 [SNCPC2019] Grid with Arrows题解

看到这道题就想到了不久之前学的 $Tarjan$,但题解中好像并没有这种解法,所以我打算写一篇题解记录一下。

前置芝士

Tarjan缩点

链式前向星

 在此之前建议大家先去练习一下P2002消息扩散

 题意概括

给定一张有向图,问是否存在一个点能够一次性到达剩下的所有的点。

 题目分析

1. 首先,我们要建立一张有向图,将每个坐标为 $(x,y)$ 的点转换成第 $(i-1)* m+j$ 个点,这样我们就得到了 $n * m$ 个点,以下用 $cnt$ 表示。然后连接每条边,但要注意边界问题。

switch(s[i][j-1]){
    case 'u':{
        if(i-p>=1) add((i-1)*m+j,(i-p-1)*m+j);//往上走,判断横坐标是否小于1
        break;
        }
    case 'd':{
        if(i+p<=n) add((i-1)*m+j,(i+p-1)*m+j);//往下走,判断横坐标是否大于n
        break;
    }
    case 'l':{
        if(j-p>=1) add((i-1)*m+j,(i-1)*m+j-p);//往左走,判断纵坐标是否小于1
        break;
    }
    case 'r':{
        if(j+p<=m) add((i-1)*m+j,(i-1)*m+j+p);//往右走,判断纵坐标是否大于m
        break;
    }
}

2. 现在,我们的有向图就建好了。接下来就是缩点模板。

void tarjan(int u){
    dfn[u]=low[u]=++timestamp;
    stk[++top]=u,in_stk[u]=1;
    for(int i=h[u];i!=-1;i=ne[i]){
        int j=e[i];
        if(!dfn[j]){
            tarjan(j);
            low[u]=min(low[u],low[j]);
        } 
        else if(in_stk[j]) low[u]=min(low[u],dfn[j]);
    }
    if(dfn[u]==low[u]){
        int y;
        scc_cnt++;
        do{
            y=stk[top--];
            id[y]=scc_cnt;
            in_stk[y]=0;
        } while(y!=u);
    }
}

3. 然后我们考虑这样一个问题:在缩完点后,什么样的点能够满足题目中的条件呢?我们发现:入度为 $0$ 的点就可以!但不要忘了:这样的点只能有一个,再多就不连通了。

for(int i=1;i<=cnt;i++){
    for(int j=h[i];j!=-1;j=ne[j]){
        if(id[i]!=id[e[j]]){
            din[id[e[j]]]++;
        }
    }
}
int ans=0;
for(int i=1;i<=scc_cnt;i++){
    if(!din[i]) ans++;
}
if(ans>1) cout<<"No\n";
else cout<<"Yes\n";

到这里,这道题就解决了。

$Code$
 

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
int T;
int din[N];
int h[N],e[N],ne[N],idx;
string s[N];
int dfn[N],low[N],timestamp;
int stk[N],top;
bool in_stk[N];
int id[N],scc_cnt;

void add(int a,int b){
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;//链式前向星 
}

void tarjan(int u){
    dfn[u]=low[u]=++timestamp;
    stk[++top]=u,in_stk[u]=1;
    for(int i=h[u];i!=-1;i=ne[i]){
        int j=e[i];
        if(!dfn[j]){
            tarjan(j);
            low[u]=min(low[u],low[j]);
        } 
        else if(in_stk[j]) low[u]=min(low[u],dfn[j]);
    }
    if(dfn[u]==low[u]){
        int y;
        scc_cnt++;
        do{
            y=stk[top--];
            id[y]=scc_cnt;
            in_stk[y]=0;
        } while(y!=u);
    }
}//Tarjan模板 

signed main(){
    cin>>T;
    while(T--){
        int n,m,p,ans=0;
        cin>>n>>m;
        int cnt=n*m;
        top=0,idx=0,scc_cnt=0,timestamp=0;//多测要清空 
        memset(h,-1,sizeof h);
        memset(dfn,0,sizeof dfn);
        memset(low,0,sizeof low);
        memset(stk,0,sizeof stk);
        memset(id,0,sizeof id);
        memset(din,0,sizeof din);
        memset(in_stk,false,sizeof in_stk);
        for(int i=1;i<=n;i++) cin>>s[i];
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++){
                cin>>p;
                switch(s[i][j-1]){//因为字符串是从0开始读入的,所以要看s[i][j-1] 
                    case 'u':{
                        if(i-p>=1) add((i-1)*m+j,(i-p-1)*m+j);;//往上走,判断横坐标是否小于1
                        break;
                    }
                    case 'd':{
                        if(i+p<=n) add((i-1)*m+j,(i+p-1)*m+j);//往下走,判断横坐标是否大于n
                        break;
                    }
                    case 'l':{
                        if(j-p>=1) add((i-1)*m+j,(i-1)*m+j-p);//往左走,判断纵坐标是否小于1
                        break;
                    }
                    case 'r':{
                        if(j+p<=m) add((i-1)*m+j,(i-1)*m+j+p);//往右走,判断纵坐标是否大于m
                        break;
                    }
                }
            }
        for(int i=1;i<=cnt;i++){
            if(!dfn[i]) tarjan(i);//缩点 
        }
        for(int i=1;i<=cnt;i++){
            for(int j=h[i];j!=-1;j=ne[j]){
                if(id[i]!=id[e[j]]){
                    din[id[e[j]]]++;//计算入度 
                }
            }
        }
        for(int i=1;i<=scc_cnt;i++){
            if(!din[i]) ans++;//计算入度为0的点 
        }
        if(ans>1) cout<<"No\n";//记得换行
        else cout<<"Yes\n";
    } 
    return 0;
}

Sure, here is an example code that imports the adjacency matrix of a digraph, outputs the graph with JavaFX, and draws the arrows of the digraph based on the line object: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.stage.Stage; public class DigraphGUI extends Application { // Define the adjacency matrix of the digraph int[][] adjMatrix = { {0, 1, 0, 0}, {1, 0, 1, 1}, {0, 0, 0, 1}, {0, 0, 1, 0} }; // Define the positions of the nodes int[] nodeX = {50, 150, 250, 350}; int[] nodeY = {50, 150, 150, 250}; @Override public void start(Stage primaryStage) { // Create a canvas to draw the graph on Canvas canvas = new Canvas(400, 300); GraphicsContext gc = canvas.getGraphicsContext2D(); // Draw the nodes for (int i = 0; i < adjMatrix.length; i++) { gc.setFill(Color.BLUEVIOLET); gc.fillOval(nodeX[i] - 10, nodeY[i] - 10, 20, 20); gc.setFill(Color.WHITE); gc.fillText(Integer.toString(i), nodeX[i] - 3, nodeY[i] + 3); } // Draw the edges and arrows for (int i = 0; i < adjMatrix.length; i++) { for (int j = 0; j < adjMatrix.length; j++) { if (adjMatrix[i][j] == 1) { gc.setStroke(Color.BLACK); gc.setLineWidth(1.5); gc.strokeLine(nodeX[i], nodeY[i], nodeX[j], nodeY[j]); double angle = Math.atan2(nodeY[j] - nodeY[i], nodeX[j] - nodeX[i]); double arrowX = Math.cos(angle - Math.toRadians(20)) * 10 + nodeX[j]; double arrowY = Math.sin(angle - Math.toRadians(20)) * 10 + nodeY[j]; gc.strokeLine(nodeX[j], nodeY[j], arrowX, arrowY); arrowX = Math.cos(angle + Math.toRadians(20)) * 10 + nodeX[j]; arrowY = Math.sin(angle + Math.toRadians(20)) * 10 + nodeY[j]; gc.strokeLine(nodeX[j], nodeY[j], arrowX, arrowY); } } } // Create a scene and add the canvas to it StackPane root = new StackPane(); root.getChildren().add(canvas); Scene scene = new Scene(root, 400, 300); // Set the stage and show the scene primaryStage.setTitle("Digraph GUI"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` In this code, we first define the adjacency matrix of the digraph and the positions of the nodes. Then, we create a canvas and draw the nodes on it. Next, we iterate through the adjacency matrix and draw the edges and arrows based on the line object of JavaFX. Finally, we create a scene and add the canvas to it, then show the scene on the stage.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值