题目原文:

In graph theory, a node X dominates a node Y if every path from the predefined start node to Y must go through X. If Y is not reachable from the start node then node Y does not have any dominator. By definition, every node reachable from the start node dominates itself. In this problem, you will be given a directed graph and you have to find the dominators of everynode where the 0-th node is the start node. Asanexample,for the graph shown right,3 dominates 4 since all the paths from 0 to 4 must pass through 3. 1 doesn’t dominate 3 since there is a path 0-2-3 that doesn’t include 1.

Input

The first line of input will contain T (≤ 100) denoting the number of cases. Each case starts with an integer N (0 < N < 100) that represents the number of nodes in the graph. The next N lines contain N integers each. If the j-th (0 based) integer of i-th (0 based) line is ‘1’, it means that there is an edge from node i to node j and similarly a ‘0’ means there is no edge.

Output

For each case, output the case number first. Then output 2N +1 lines that summarizes the dominator relationship between every pair of nodes. If node A dominates node B, output ‘Y’ in cell (A,B), otherwise output ‘N’. Cell (A,B) means cell at A-th row and B-th column. Surround the output with ‘|’, ‘+’ and ‘-’ to make it more legible. Look at the samples for exact format.

Sample Input

2
5
0 1 1 0 0
0 0 0 1 0
0 0 0 1 0
0 0 0 0 1
0 0 0 0 0
1
1

Sample Output

题意

对0到n-1个点,求出每个点的支配点,即从0开始必须经过 i 点才能到达的点,输出所有的点的情况。特殊情况:0也是0的支配点之一。

貌似可以用割点的方法求出来,但这是一道简单题,应该尽量让代码简洁一点。

思路

采用深度优先,对每个点,先将该点暂时标记,使得深搜进入不了该点,而后对0点进行深搜,记录到达的点的情况。

而后,从1开始到n-1,将每个点的搜索结果与0点的搜索结果一一异或,便可得到最终结果。

原理就是,去除某点后的遍历情况如果有和0点一样的,说明不通过该点也能到达那种情况,存在其他路径。只有不一样的情况才算是独有路径。

代码

#include <cstdio>
#include <cstring>
int g[110][110];
int visit[110][110];
int book[110];
int n;
void dfs(int x){book[x] = 1;for(int i = 0; i < n; i++){if(book[i] == 0 && g[x][i] == 1)dfs(i);}
}
void prow(){putchar('+');for(int i = 0; i < n * 2 - 1; i++)putchar('-');puts("+");
}
int main(){// freopen("i.txt", "r", stdin);// freopen("o.txt", "w", stdout);int t, c = 1; scanf("%d", &t);while(t--){scanf("%d", &n);for(int i = 0; i < n; i++)for(int j = 0; j < n; j++)scanf("%d", &g[i][j]);for(int i = 0; i < n; i++){memset(book, 0, sizeof(book));book[i] = -1;//暂时去除该点dfs(0);book[i] = 0;for(int j = 0; j < n; j++)visit[i][j] = book[j];}visit[0][0] = 1;//记得将特殊情况补上for(int i = 1; i < n; i++)for(int j = 0; j < n; j++)visit[i][j] ^= visit[0][j];//异或printf("Case %d:\n", c++);for(int i = 0; i < n; i++){prow();putchar('|');for(int j = 0; j < n; j++)printf("%c|", visit[i][j] == 1 ? 'Y' : 'N');puts("");}prow();}
}

ACM Plan UVa - 11902 Dominator(图的遍历,深度优先)相关推荐

  1. 图的遍历——深度优先搜索+广度优先搜索

    一:图的遍历--深度优先搜索 在本文其他内容中只是大体概括了主要的图论内容,更加详细的代码实现及算法分析在此给出. 深度优先搜索(DFS)类似树的先序遍历. 假设初始状态是图中所有顶点未曾被访问,则深 ...

  2. 图--广度优先遍历/深度优先遍历(c语言实现)

    //不能通过编译,没有引入队列头文件 1 #include<stdlib.h> 2 #define MAX_VERTEX_NUM; 3 typedef int infoType; 4 ty ...

  3. 图的遍历——深度优先遍历与广度优先遍历

    目录 何谓遍历? 图的遍历特点 图的遍历方式 深度优先搜索 过程分析 案例分析: 算法的代码实现 测试案例: 测试结果如下: 遍历非连通图 算法复杂度分析 额外补充 广度优先搜索 过程分析 辅助队列 ...

  4. ACM Plan UVa - 168 Theseus and the Minotaur(图的遍历,深度优先)

    题目: 原文太长,简单说一下.如标题所述,是希腊神话的迷宫故事,给出一张图,以及T和M两个人物的位置.因为M怕光,所以T拿着蜡烛追逐M,T每走过K个节点,就会在第K个节点上点上一支蜡烛.M在逃跑时,会 ...

  5. 图 之遍历----深度优先遍历0.o

    何为深度优先遍历0.o呢?DFS是图论中的经典算法.其利用深度优先搜索算法可以产生目标图的相应拓扑排序表,利用拓扑排序表可以方便的解决很多相关的图论问题,如最大路径问题等等. 基本思想:(一条路走到底 ...

  6. 7.3图的遍历(深度优先)-理论

    深度优先遍历(DepthFirstSearch)也称为深度优先搜索,简称DFS. 他是怎么遍历的呢? 下面先看这个图,我们规定为右手定则,也就是向右走 . 如下图: 规定右手原则:在没有碰到重复顶点的 ...

  7. 图的遍历——深度优先搜索和广度(宽度)优先搜索(含例题)

    专栏导读及目录https://blog.csdn.net/createprogram/article/details/86741044 深度优先搜索 DFS基本思想 基本步骤: 1.从图中某个顶点v0 ...

  8. 数据结构 笔记:图的遍历(BFS)

    时间复杂度的对比分析   MatrixGraph ListGraph addVertex - O(n) removeVertex - O(n^2) getVertex O(1) O(n) setVer ...

  9. 算法:C++实现图的遍历

    ​​​​​​ 目录 图的遍历 深度优先搜索法 广度优先搜索法 代码及注释部分 图的遍历,属于数据结构中的内容.指的是从图中的任一顶点出发,对图中的所有顶点访问一次且只访问一次.图的遍历操作和树的遍历操 ...

  10. 图的遍历 广度优先遍历(爱思创)

    前言: 这篇文章还是是为了帮助一些 像我这样的菜鸟 找到简单的题解 问题描述: 读入一个用邻接矩阵存储的无向连通图,输出它的广度(宽度)优先遍历序列. 输入格式 第一行一个正整数 n(2≤n≤100) ...

最新文章

  1. Quick-cocos2d-x3.3 Study (一) --------- 创建一个UI标签
  2. windows sftp工具_将SSH服务器映射成Windows网络驱动器
  3. uni-app小程序v-show内容始终不显示
  4. 各大媒体优劣对比_信息流投放广告丨各大平台的信息流都有什么特点与弊端
  5. 令人难以置信的竞争性编程-您无需学习
  6. Android 系统(196)---Android 属性动画
  7. Java微信公众号开发梳理
  8. hibernate连接MySQL数据库
  9. 模板设计模式_设计模式实用又简单的模板模式
  10. 如何查看Ubuntu的内核是AMD、ARM、x86、x86_64
  11. 分享一些视频加密播放技术业内知识
  12. 阿里巴巴交易平台技术揭秘
  13. php mql web开发,自己动手开发多线程异步 MQL5 WebRequest
  14. MAC安装中文输入法Rime
  15. iPhone如何恢复出厂设置?苹果手机恢复出厂设置教程【3种方法】
  16. ubuntu 串口调试助手
  17. [KALI系列第四章]进行ARP断网攻击,包含安装方法
  18. sql loader导出数据和导入数据(sqlldr)
  19. CCNA专业英文词汇全集
  20. NGINX工作原理解析

热门文章

  1. 初学C语言——三位数倒序
  2. java求导数_java实现队列链表,求一元多项式的导数
  3. jQuery手机版日历插件带农历
  4. PRCS-1011 PRCS-1014
  5. 28. 移动端touch触摸事件
  6. [云原生专题-23]:K8S - Kubernetes(K8S)整体概述与组件架构通俗讲解
  7. 基于单片机的超市储物柜设计_基于单片机的超市储物柜控制系统设计.docx
  8. 阿里云因发现Log4j2 核弹级漏洞,未及时上报,被工信部处罚。。
  9. 使用 HTML5 控制摄像头摄像和拍照
  10. turtle库画超立方体 难度1