Graph: basic and traversal

来源:互联网 发布:studio软件下载 编辑:程序博客网 时间:2024/06/14 20:48
1000. Forest
Total:135Accepted:41
   
Time Limit: 1sec    Memory Limit:32MB
Description

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.
Input
There’re several test cases. For each case, in the first line there are two integer numbers n and m (1≤n≤100, 0≤m≤100, m≤n*n) indicating the number of nodes and edges respectively , then m lines followed , for each line of these m lines there are two integer numbers a and b (1≤a,b≤n)indicating there’s an edge pointing from a to b. Nodes are represented by numbers between 1 and n .n=0 indicates end of input.
Output

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.

Sample Input
 Copy sample input to clipboard
1 01 11 13 11 32 21 22 10 88
Sample Output
0 1INVALID1 2INVALID
1001. Magic Island
  
Total:261Accepted:75
    
    
Time Limit: 1sec    Memory Limit:32MB
Description
There are N cities and N-1 roads in Magic-Island. You can go from one city to any other. One road only connects two cities. One day, The king of magic-island want to visit the island from the capital. No road is visited twice. Do you know the longest distance the king can go.
Input

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 XYD, 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.

Output
One number per line for each test case, the longest distance the king can go.
Sample Input
 Copy sample input to clipboard
3 11 2 101 3 20
Sample Output
20

Problem Source: ZSUACM Team Member



1002. DAG?
  
Total:192Accepted:66
    
    
Time Limit: 1sec    Memory Limit:256MB
Description

输入一个有向图,判断该图是否是有向无环图(Directed Acyclic Graph)。

Input

输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=100,0<=m<=10000。

接下来的m行,每行是一个数对u v,表示存在有向边(u,v)。顶点编号从1开始。
Output

如果图是DAG,输出1,否则输出0

Sample Input
 Copy sample input to clipboard
3 31 22 33 1
Sample Output
0

Problem Source: 刘晓铭


1003. 有向图边的分类
  
Total:35Accepted:25
    
    
Time Limit: 1sec    Memory Limit:256MB
Description

输入一个有向图,从顶点1开始做dfs对边进行分类。

Input

输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=100,0<=m<=10000。

接下来的m行,每行是一个数对u v,表示存在有向边(u,v)。顶点编号从1开始。

 

接下来的1行,包含一个整数k,表示会查询k条边的类型。

接下来的k行,每行是一个数对u v,表示查询边u v的类型。

Output

对每条查询的边,单独一行输出边的类型,参见输出样例。

Sample Input
 Copy sample input to clipboard
4 61 22 33 11 31 44 241 23 11 34 2
Sample Output
edge (1,2) is Tree Edgeedge (3,1) is Back Edgeedge (1,3) is Down Edgeedge (4,2) is Cross Edge

Problem Source: 刘晓铭



1004. Knight Moves
  
Total:42Accepted:31
    
    
Time Limit: 1sec    Memory Limit:32MB
Description

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.  

Input

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.  

Output

For each test case, print one line saying "To get from xx to yy takes n knight moves.".  

Sample Input
 Copy sample input to clipboard
8e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6  
Sample Output
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.



1005. 无路可逃?
  
Total:133Accepted:44
    
    
Time Limit: 1sec    Memory Limit:256MB
Description

唐僧被妖怪关在迷宫中。孙悟空好不容易找到一张迷宫地图,并通过一个魔法门来到来到迷宫某个位置。假设迷宫是一个n*m的矩阵,它有两种地形,1表示平地,0表示沼泽,孙悟空只能停留在平地上。孙悟空目前的位置在坐标(sx,sy)处,他可以向上下左右四个方向移动。

请你帮助孙悟空计算一下,迷宫中是否存在一条路从他所在位置(sx,sy)到达唐僧所在位置(tx,ty)?
 
Input

输入第一行为一个整数t(0<t<=10),表示测试用例个数。

每个测试样例的第1行是2个正整数n (1≤n≤100),m (1≤n≤100),表示迷宫是n*m的矩阵。接下来的n行输入迷宫,每行有m个数字(0或者1),数字之间用一个空格间隔。
接下来的1行是4个整数sx,sy,tx,ty,1≤sx,tx≤n,1≤sy,ty≤m,表示孙悟空所在位置为第sx行第sy列,唐僧所在位置为第tx行第tx列。迷宫左上角编号是(1,1),右下角编号是(n, m)。
数据保证(sx,sy)格和(tx,ty)必定为平地。
 
Output

每个样例单独输出一行:1表示路径存在,0表示路径不存在。

 
Sample Input
 Copy sample input to clipboard
22 21 00 11 1 2 24 4 1 1 1 01 0 1 11 0 1 11 1 1 01 1 3 4
Sample Output
01

Problem Source: 刘晓铭



1006. shortest path in unweighted graph
  
Total:109Accepted:42
    
    
Time Limit: 1sec    Memory Limit:256MB
Description

输入一个无向图,指定一个顶点s开始bfs遍历,求出s到图中每个点的最短距离。

如果不存在s到t的路径,则记s到t的距离为-1。
 
Input

输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=1000,0<=m<=10000。

以下m行,每行是一个数对v y,表示存在边(v,y)。顶点编号从1开始。 
 
Output

记s=1,在一行中依次输出:顶点1到s的最短距离,顶点2到s的最短距离,...,顶点n到s的最短距离。

每项输出之后加一个空格,包括最后一项。
 
Sample Input
 Copy sample input to clipboard
5 31 21 32 4
Sample Output
0 1 1 2 -1 

Problem Source: 刘晓铭




1007. bicoloring
  
Total:87Accepted:34
    
    
Time Limit: 1sec    Memory Limit:256MB
Description

输入一个简单(无多重边和自环)的连通无向图,判断该图是否能用黑白两种颜色对顶点染色,使得每条边的两个端点为不同颜色。

Input

输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=1000,0<=m<=10000。

以下m行,每行是一个数对u v,表示存在边(u,v)。顶点编号从1开始。
Output

如果能做到双着色,输出"yes",否则输出"no"。

 
Sample Input
 Copy sample input to clipboard
3 31 22 33 1
Sample Output
no

Problem Source: UVa 10004



1008. connect components in undirected graph
  
Total:55Accepted:36
    
    
Time Limit: 1sec    Memory Limit:256MB
Description

输入一个简单无向图,求出图中连通块的数目。

Input

输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=1000,0<=m<=10000。

以下m行,每行是一个数对v y,表示存在边(v,y)。顶点编号从1开始。
Output

单独一行输出连通块的数目。

Sample Input
 Copy sample input to clipboard
5 31 21 32 4
Sample Output
2

Problem Source: 刘晓铭





1009. Can I Post the lette
Total:51Accepted:32
   
Time Limit: 1sec    Memory Limit:32MB
Description

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.

Input

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.

Output
For each test case, if I can post the letter print “I can post the letter” in one line, otherwise print “I can't post the letter”.
Sample Input
 Copy sample input to clipboard
320 11 2310 10
Sample Output
I can post the letterI can't post the letter


0 0
原创粉丝点击