Zju 1268 Is It A Tree?解题分析









0 0 //is a tree

1 1 0 0 //is not a tree

1 3 2 3 0 0 //is not a tree

122 3 1 3 0 0 //is not a tree


Is It A Tree Is It A Tree


Problem DescriptionrnA tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. rnThere is exactly one node, called the root, to which no directed edges point. rnrnEvery node except the root has exactly one edge pointing to it. rnrnThere is a unique sequence of directed edges from the root to each node. rnrnFor example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.rnrnrnrnrnIn this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not. rnrn rnrnInputrnThe input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero. rn rnrnOutputrnFor each test case display the line ``Case k is a tree." or the line ``Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1). rn rnrnSample Inputrn6 8 5 3 5 2 6 4rn5 6 0 0rn8 1 7 3 6 2 8 9 7 5rn7 4 7 8 7 6 0 0rn3 8 6 8 6 4rn5 3 5 6 5 2 0 0rn-1 -1rn rnrnSample OutputrnCase 1 is a tree.rnCase 2 is a tree.rnCase 3 is not a tree.

ZJU 1157 A Plug for UNIX


A Plug for UNIXrnYou are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of information and ideas on the Internet as cumbersome and bureaucratic as possible. rnrnSince the room was designed to accommodate reporters and journalists from around the world, it is equipped with electrical receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that existed when the room was built. Unfortunately, the room was built many years ago when reporters used very few electric and electronic devices and is equipped with only one receptacle of each type. These days, like everyone else, reporters require many such devices to do their jobs: laptops, cell phones, tape recorders, pagers, coffee pots, microwave ovens, blow dryers, curling irons, tooth brushes, etc. Naturally, many of these devices can operate on batteries, but since the meeting is likely to be long and tedious, you want to be able to plug in as many as you can. rnrnrnBefore the meeting begins, you gather up all the devices that the reporters would like to use, and attempt to set them up. You notice that some of the devices use plugs for which there is no receptacle. You wonder if these devices are from countries that didn't exist when the room was built. For some receptacles, there are several devices that use the corresponding plug. For other receptacles, there are no devices that use the corresponding plug. rnrnrnIn order to try to solve the problem you visit a nearby parts supply store. The store sells adapters that allow one type of plug to be used in a different type of outlet. Moreover, adapters are allowed to be plugged into other adapters. The store does not have adapters for all possible combinations of plugs and receptacles, but there is essentially an unlimited supply of the ones they do have. rnrnrnThis problem contains multiple test cases!rnrnThe first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.rnrnThe output format consists of N output blocks. There is a blank line between output blocks.rnrnrnInputrnrnThe input will consist of one case. The first line contains a single positive integer n (1 <= n <= 100) indicating the number of receptacles in the room. The next n lines list the receptacle types found in the room. Each receptacle type consists of a string of at most 24 alphanumeric characters. The next line contains a single positive integer m (1 <= m <= 100) indicating the number of devices you would like to plug in. Each of the next m lines lists the name of a device followed by the type of plug it uses (which is identical to the type of receptacle it requires). A device name is a string of at most 24 alphanumeric characters. No two devices will have exactly the same name. The plug type is separated from the device name by a space. The next line contains a single positive integer k (1 <= k <= 100) indicating the number of different varieties of adapters that are available. Each of the next k lines describes a variety of adapter, giving the type of receptacle provided by the adapter, followed by a space, followed by the type of plug. rnrnrnOutput rnrnA line containing a single non-negative integer indicating the smallest number of devices that cannot be plugged in. rnrnrnSample Inputrnrn1rnrn4rnArnBrnCrnDrn5rnlaptop Brnphone Crnpager Brnclock Brncomb Xrn3rnB XrnX ArnX DrnrnrnSample Output rnrn1rnrn先用DFS,搜索出插头这件的对应关系,然后建立邻接矩阵,然后就是进行最大匹配了。。。。解题思路比较清楚,只是还是悲剧了。。。。rnrnrn代码:[code=C/C++]rn#include rn#include rn#include rn#define MAX 500rnusing namespace std;rnrnint pnum;//插头数rnint dnum; //设备数rnint anum; //适配器数rnstring plug[MAX]; //存储插头型号rnstring type[MAX]; //设备插头型号rnstring dev[MAX]; //设备名字rnstring inadapt[MAX]; //需要的型号rnstring outadapt[MAX]; //适配的型号rnbool map[MAX][MAX]; //二分矩阵rnbool state[MAX];rnbool flag[MAX]; //匹配插头时的标记rnint res[MAX]; //匹配结果rnvoid DFS(string type, int j, int d)rnrn int i,k;rn string temp;rn for(i = 0; i< anum; i++)rn if(type == outadapt[i] && flag[j] == true)rn rn flag[i] = false;rn DFS(inadapt[i], i,d);rn rn temp= inadapt[j];rn for(k = 0; k< pnum; k++)rn rn if(temp == plug[k])rn map[d][k] = true;rn rnrnrnbool find(int i)rnrn int j;rn for(j = 0; j< dnum; j++)rn if(map[i][j] == true && state[j] == false)rn rn state[j] = true;rn if(res[j] == -1 || find(res[j]) == true)rn rn res[j] = i;rn return true;rn rn rn return false;rnrnrnrnint main()rn rn int n,p1=1;rn cin>>n;rn while(n--)rn rn if(p1)rn p1=0;rn elsern printf("\n");rn int i,j,match;rn string temp;rn memset(map, false, sizeof(map));rn for(i=0 ;i< MAX; i++)rn res[i] = -1;rnrn cin>>pnum;rn for(i=0; i>plug[i]; rn cin>>dnum;rn for(i=0; i>dev[i]>>type[i];rn cin>>anum;rn for(i=0; i>outadapt[i]>>inadapt[i];rnrn///////////////////////////////////////////////////////////////////////////////////////rn for(i = 0; i