2012年8月9日 星期四

uva 459 - Graph Connectivity


 Graph Connectivity 

Consider a graph G formed from a large number of nodes connected by edges. G is said to be connected if a path can be found in 0 or more steps between any pair of nodes in G. For example, the graph below is not connected because there is no path from A to C.

picture22

This graph contains, however, a number of subgraphs that are connected, one for each of the following sets of nodes: {A}, {B}, {C}, {D}, {E}, {A,B}, {B,D}, {C,E}, {A,B,D}
A connected subgraph is maximal if there are no nodes and edges in the original graph that could be added to the subgraph and still leave it connected. There are two maximal connected subgraphs above, one associated with the nodes {ABD} and the other with the nodes {CE}.
Write a program to determine the number of maximal connected subgraphs of a given graph.

Input and Output

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The first line of each input set contains a single uppercase alphabetic character. This character represents the largest node name in the graph. Each successive line contains a pair of uppercase alphabetic characters denoting an edge in the graph. The sample input section contains a possible input set for the graph pictured above.
Input is terminated by a blank line.
For each test case, the output the number of maximal connected subgraphs. The outputs of two consecutive cases will be separated by a blank line.

Sample Input


1

E
AB
CE
DB
EC

Sample Output


2

union-find 
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<set>
#include<math.h>
#include<algorithm>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define DBG 0

using namespace std;

int N;
int par[30];
set<int>st;
int find(int a){
if(par[a]==a)return a;
int c=a;
while(par[a]!=a)a=par[a];
return par[c]=a;
}
void merge(int to,int from){
int a=find(to),b=find(from);
par[b]=a;
}
int main(){
int a,b,c;
string s;
char nds[10];
bool ll=0;
scanf("%d\n",&a);
rep(i,a){
rep(i,30)par[i]=i;st.clear();
fgets(nds,10,stdin),nds[strlen(nds)-1]='\0',s=nds;
N=s[0]-'A'+1;
if(DBG)printf("N %d\n",N);
while(fgets(nds,10,stdin)){
if(nds[0]=='\n')break;
b=nds[0]-'A',c=nds[1]-'A';
if(find(b)!=find(c)){merge(b,c);}
}
rep(i,N) if(!st.count(find(i)))st.insert(find(i));
if(ll)printf("\n");ll=1;
printf("%d\n",st.size());
}
}

uva 10147 - Highways


Problem E
"Highways"


The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can't reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system.
Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xiyi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus their length is equal to Cartesian distance between towns. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.
The Flatopian government wants to minimize the cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length. Thus, the least expensive highway system will be the one that minimizes the total highways length.

Input

The first line of the input consists of an integer indicating the number of test cases in the input. Then there's a blank line and the datasets separated by a blank line.
Each test case consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built.
The first line of the test case contains a single integer N (1 ≤ N ≤ 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of ithtown (for i from 1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location.
The next line contains a single integer M (0 ≤ M ≤ 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a highway. Each pair of towns is connected by at most one highway.

Output

Write to the output file a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway connects, separated by a space.
If no new highways need to be built (all towns are already connected), then the output file should contain a line with the sentence "No new highways need".
Print a blank line between test cases.

Sample input

1

9
1 5
0 0 
3 2
4 5
5 1
0 4
5 2
1 2
5 3
3
1 3
9 7
1 2

Sample output for the sample input

1 6
3 7
4 9
5 7
8 3

minimum spanning tree - kruskal

uva 10034 - Freckles


Problem A: Freckles

In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through.Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

Sample Input

1

3
1.0 1.0
2.0 2.0
2.0 4.0

Sample Output

3.41

minimum spanning tree: Prim's algorithm

uva 796 - Critical Links



  Critical Links 

In a computer network a link L, which interconnects two servers, is considered critical if there are at least two servers A and B such that all network interconnection paths between A and B pass through L. Removing a critical link generates two disjoint sub-networks such that any two servers of a sub-network are interconnected. For example, the network shown in figure 1 has three critical links that are marked bold: 0 -13 - 4 and 6 - 7.

Figure 1: Critical links
It is known that:
1.
the connection links are bi-directional;
2.
a server is not directly connected to itself;
3.
two servers are interconnected if they are directly connected or if they are interconnected with the same server;
4.
the network can have stand-alone sub-networks.


Write a program that finds all critical links of a given computer network.

Input 

The program reads sets of data from a text file. Each data set specifies the structure of a network and has the format:

$no\_of\_servers$
$server_0\ (no\_of\_direct\_connections)\ connected\_server\ \dots\ connected\_server$
...
$server_{no\_of\_servers}\ (no\_of\_direct\_connections)\ connected\_server\ \dots\ connected\_server$


The first line contains a positive integer $no\_of\_servers$(possibly 0) which is the number of network servers. The next $no\_of\_servers$ lines, one for each server in the network, are randomly ordered and show the way servers are connected. The line corresponding to serverk$0 \le k \le no\_of\_servers - 1$, specifies the number of direct connections of serverk and the servers which are directly connected to serverk. Servers are represented by integers from 0 to $no\_of\_servers - 1$. Input data are correct. The first data set from sample input below corresponds to the network in figure 1, while the second data set specifies an empty network.

Output 

The result of the program is on standard output. For each data set the program prints the number of critical links and the critical links, one link per line, starting from the beginning of the line, as shown in the sample output below. The links are listed in ascending order according to their first element. The output for the data set is followed by an empty line.

Sample Input 

8
0 (1) 1
1 (3) 2 0 3
2 (2) 1 3
3 (3) 1 2 4
4 (1) 3
7 (1) 6
6 (1) 7
5 (0)

0

Sample Output 

3 critical links
0 - 1
3 - 4
6 - 7

0 critical links



Miguel Revilla
2001-01-05

finding bridge
unknown vertex number, use map instead of vector

2012年8月8日 星期三

uva 315 - Network



 Network 

A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect together two places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.

Input

The input file consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at most N lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0.

Output

The output contains for each block except the last in the input file one line containing the number of critical places.

Sample Input


5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
0
0

Sample Output


1
2

articulation point

uva572 - Oil Deposits



  Oil Deposits 

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil.
A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.

Input 

The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise$1 \le m \le 100$ and $1 \le n \le 100$. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.

Output 

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

Sample Input 

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

Sample Output 

0
1
2
2



Miguel A. Revilla
1998-03-10

flood-fill

uva 10080 - Gopher II


Problem A: Gopher II

The gopher family, having averted the canine threat, must face a new predator.
The are n gophers and m gopher holes, each at distinct (x, y) coordinates. A hawk arrives and if a gopher does not reach a hole in s seconds it is vulnerable to being eaten. A hole can save at most one gopher. All the gophers run at the same velocity v. The gopher family needs an escape strategy that minimizes the number of vulnerable gophers.
The input contains several cases. The first line of each case contains four positive integers less than 100: nm,s, and v. The next n lines give the coordinates of the gophers; the following m lines give the coordinates of the gopher holes. All distances are in metres; all times are in seconds; all velocities are in metres per second.
Output consists of a single line for each case, giving the number of vulnerable gophers.

Sample Input

2 2 5 10
1.0 1.0
2.0 2.0
100.0 100.0
20.0 20.0

Output for Sample Input

1

bipartite matching - augmenting path algorithm