Graph: basic and traversal
来源:互联网 发布:studio软件下载 编辑:程序博客网 时间:2024/06/14 20:48
In the field of computer science, forest is important and deeply researched , it is a model for many data structures . Now it’s your job here to calculate the depth and width of given forests.
Precisely, a forest here is a directed graph with neither loop nor two edges pointing to the same node. Nodes with no edge pointing to are roots, we define that roots are at level 0 . If there’s an edge points from node A to node B , then node B is called a child of node A , and we define that B is at level (k+1) if and only if A is at level k .
We define the depth of a forest is the maximum level number of all the nodes , the width of a forest is the maximum number of nodes at the same level.For each case output one line of answer , if it’s not a forest , i.e. there’s at least one loop or two edges pointing to the same node, output “INVALID”(without quotation mark), otherwise output the depth and width of the forest, separated by a white space.
1 01 11 13 11 32 21 22 10 88
0 1INVALID1 2INVALID
There are several test cases in the inputA test case starts with two numbers N and K. (1<=N<=10000, 1<=K<=N). The cities is denoted from 1 to N. K is the capital.
The next N-1 lines each contain three numbers X, Y, D, meaning that there is a road between city-X and city-Y and the distance of the road is D. D is a positive integer which is not bigger than 1000.Input will be ended by the end of file.
3 11 2 101 3 20
20
Problem Source: ZSUACM Team Member
输入一个有向图,判断该图是否是有向无环图(Directed Acyclic Graph)。
输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=100,0<=m<=10000。
如果图是DAG,输出1,否则输出0
3 31 22 33 1
0
Problem Source: 刘晓铭
输入一个有向图,从顶点1开始做dfs对边进行分类。
输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=100,0<=m<=10000。
接下来的1行,包含一个整数k,表示会查询k条边的类型。
接下来的k行,每行是一个数对u v,表示查询边u v的类型。
对每条查询的边,单独一行输出边的类型,参见输出样例。
4 61 22 33 11 31 44 241 23 11 34 2
edge (1,2) is Tree Edgeedge (3,1) is Back Edgeedge (1,3) is Down Edgeedge (4,2) is Cross Edge
Problem Source: 刘晓铭
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy. Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part. Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
There are multiple test cases. The first line contains an integer T, indicating the number of test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
8e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6
To get from e2 to e4 takes 2 knight moves. To get from a1 to b2 takes 4 knight moves. To get from b2 to c3 takes 2 knight moves. To get from a1 to h8 takes 6 knight moves. To get from a1 to h7 takes 5 knight moves. To get from h8 to a1 takes 6 knight moves. To get from b1 to c3 takes 1 knight moves. To get from f6 to f6 takes 0 knight moves.
唐僧被妖怪关在迷宫中。孙悟空好不容易找到一张迷宫地图,并通过一个魔法门来到来到迷宫某个位置。假设迷宫是一个n*m的矩阵,它有两种地形,1表示平地,0表示沼泽,孙悟空只能停留在平地上。孙悟空目前的位置在坐标(sx,sy)处,他可以向上下左右四个方向移动。
输入第一行为一个整数t(0<t<=10),表示测试用例个数。
每个样例单独输出一行:1表示路径存在,0表示路径不存在。
22 21 00 11 1 2 24 4 1 1 1 01 0 1 11 0 1 11 1 1 01 1 3 4
01
Problem Source: 刘晓铭
输入一个无向图,指定一个顶点s开始bfs遍历,求出s到图中每个点的最短距离。
输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=1000,0<=m<=10000。
记s=1,在一行中依次输出:顶点1到s的最短距离,顶点2到s的最短距离,...,顶点n到s的最短距离。
5 31 21 32 4
0 1 1 2 -1
Problem Source: 刘晓铭
输入一个简单(无多重边和自环)的连通无向图,判断该图是否能用黑白两种颜色对顶点染色,使得每条边的两个端点为不同颜色。
输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=1000,0<=m<=10000。
如果能做到双着色,输出"yes",否则输出"no"。
3 31 22 33 1
no
Problem Source: UVa 10004
输入一个简单无向图,求出图中连通块的数目。
输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=1000,0<=m<=10000。
单独一行输出连通块的数目。
5 31 21 32 4
2
Problem Source: 刘晓铭
I am a traveler. I want to post a letter to Merlin. But because there are so many roads I can walk through, and maybe I can’t go to Merlin’s house following these roads, I must judge whether I can post the letter to Merlin before starting my travel.
Suppose the cities are numbered from 0 to N-1, I am at city 0, and Merlin is at city N-1. And there are M roads I can walk through, each of which connects two cities. Please note that each road is direct, i.e. a road from A to B does not indicate a road from B to A.
Please help me to find out whether I could go to Merlin’s house or not.
There are multiple input cases. For one case, first are two lines of two integers N and M, (N<=200, M<=N*N/2), that means the number of citys and the number of roads. And Merlin stands at city N-1. After that, there are M lines. Each line contains two integers i and j, what means that there is a road from city i to city j.
The input is terminated by N=0.
320 11 2310 10
I can post the letterI can't post the letter
- Graph: basic and traversal
- 【索引】Implicit Graph Traversal
- 【索引】Implicit Graph Traversal
- Basic Graph Traversals
- 基本图论定义与术语(Basic Definition and Glossary in Graph The)
- labeled graph---some basic knowledge
- Assignment 6: Basic Graph Algorithms
- Covisibility Graph and Essential Graph
- 7.3Basic and visual basic
- Graph and Queries hdu3762
- cf459E Pashmak and Graph
- CF624C - Graph and String
- Monk and Graph Problem
- Depth First Traversal for a graph
- Graph Traversal (BFS vs DFS vs Stack)
- Sicily 1876/1949. Basic Graph Problem
- CC Chapter 4 Tree and Graph --Graph
- llvm:Call Graph And Control Flow Graph
- L1 norm, L2 norm
- 1006. 连通性问题
- Hdu 1063 & Poj 1001 Exponentiation (java 高精度实数 去除首尾0)
- UVa 941: Permutations 排列?其实是阶乘
- Number Sequence
- Graph: basic and traversal
- Ext3.2 布局——table layout
- 逃离迷宫
- bash参考手册之四(shell内建命令)续七
- java操作mysql数据库实例
- Java知识总结-抽象类和接口
- Flying to the Mars
- 【Vim使用】2.ctags与taglist
- 畅通工程