首页 > 代码库 > All You Need is to Wait
All You Need is to Wait
电子科技大学 ACM CDOJ 2 翻译
All You Need is to Wait
Time Limit: 6000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
Submit Status
All the citizens in the magic world cheer for you, the clever programmers! With your help, the war in the magic world finally ends!
所有的人民为你欢呼,是你结束了魔法世界的战争。
Now, our hero zplinti1 is back to work. He works in a computer game company, and his new product is a war-like game, but at the most time the players just sit at their computers and watch how the war going on.
如今我们的ACM英雄zplinti1归来,而他的新工作是战争游戏的程序员,但玩家大部分时间是坐在那里看战争的进行。
The war breaks with the United Kingdom of Red and the United Kingdom of Black. We will call them Red and Black in the following paragraphs. They will keep invading the other’s territory until one wins at last.
战争在红国和黑国之间进行。他们持续的争夺对方的土地直到一方获胜。
The map of the war is a map with n cities, and m roads connecting them, forming an undirected graph. It’s guaranteed the graph is connected.
战争的地图是由n个城市和m条道路连接的无向图,地图是连通的。
At first, some cities are captured by Red, some are captured by Black, and others are unoccupied. Then the cities might get united. That is, if two cities from the same country can be reached to each other without passing any city not captured by their country, they will get united and form a sub-graph. Each maximum sub-graph is called a kingdom.The size of a kingdom is the cities the kingdom contains. The size of a country is also the cities the country contains.
一开始,一些城市由红国或黑国占领,其他城市未被占领,同一个国家的城市之间若直接相连则会联合,并形成一个子图。每个最大子图为一个王国,王国和国家的规模是包含在其中城市的数量。
The game processes in Two steps.
游戏分两步走
For the First Part:
第一部分
In each second, each kingdom begin to invade the unoccupied cities that are directly connected by that kingdom at the same time. For each unoccupied city, if there are more than one kingdoms invading, we judge the battle’s result in this way:
每秒,每个王国同时入侵周围的无主城市,若需要仲裁,则按照如下规则
- Check if all the biggest kingdoms (There might only be one such biggest kingdom!) among those are from the same country. If it is true, the city belongs to that country.
检查这些王国中所有最大的王国是否来自于同一个国家,是则归该国家。 - Otherwise, among all those kingdoms invading the city, we count the total size of the kingdoms from Red and the total size of the kingdoms from Black. If they are different, the city belongs to the bigger country.
否则,在这些入侵王国中,如果红色的城市比黑色的城市数量多,则红国胜利,反之亦然。 - Otherwise, count the total size of Red and total size of Black. If they are different, the bigger one gets the city.
如果最大的王国不是同一边的,且这些王国的红国和黑国的城市数量也一样,则统计地图上红国和黑国城市的数量,大的获胜。 - If it is still impossible to judge the battle’s result, the city will belong to the country Black. (I won’t tell you zplinti1 loves Bearchild very much, and Black andBearchild all begins with letter B!)
若仍无法判断,则黑获胜(因为zplinti1和Bearchild基情满满,而Bearchild和黑色都是B开头)
After eachsecond, the number of the kingdoms and the size of each kingdom might change,since there are more cities getting connected.
每秒钟,王国的数量和其中城市的数量都在改变,城市之间也开始互相连接。
If all thecities are occupied, the Game will get into the second part.
当所有城市都被占领,游戏进入第二部分。
For the second Part:
第二部分
Since there areno unoccupied cities on the map, two countries start to invade each other!
当所有的城市已经被占领,两个国家开始互相入侵。
In each second,every kingdom will start to invade all the kingdoms that is directly connectedto that kingdom, whose size is smaller, and belongs to the other country. Andthe invading will always succeed instantly, and all the cities of that kingdomwill be captured. But all the invading happen at the same time, so whilekingdom B is invading kingdom C, kingdom B’s original cities might be capturedby kingdom A.
每秒,每个王国开始入侵所有直接相连的其他王国,这些王国若满足:城市数量比它小、所属国家不同,则立刻被占领。占领是同时发生的。例如A王国占领B时,A也可以被C占领。
The war willend, when there can be no invading any more. Then you need to output the resultof the war.
理论上不会出现循环占领,也就是说最后战争会结束。结束时,你要输出战争结果。
Input
输入
The first line of input contains anumber T, indicating the number of test cases.(T≤30)(T≤30)
第一行包含数字T,表示用例的个数。
For each case, the first line containstwo integers n and m: the number ofcities and the roads (1≤n≤100,000,1≤m≤200,000)(1≤n≤100,000,1≤m≤200,000).
每个例子中,第一行有两个整数n 和 m,n表示城市的数量,m表示道路的数量
Then m linesfollow, each with two integers u and v, with means there is a undirected road between city u and v (0≤u,v<n)(0≤u,v<n).
接下来会有m行数据,每行有两个整数u和v,表示u到v中间有无方向路。
The following line contains a number P (1≤P≤100). Then P lines follow, each line with two integers cityi and fromi. If fromi equals 0, it means cityi is captured by country Red. If fromi equals 1, it means it iscaptured by country Black (0≤cityi<n,0≤fromi≤1). All the cityi will be different. All the other cities apart fromthose cities are unoccupied at first.
接下来一行包含数字P,接下来有P行,每一行有两个整数cityi 和fromi,分别代表红国和黑国,1表示占领0表示未占领
Output
输出
For each case, output Case #i: first. (i is thenumber of the test case, from 1 to T). If thecountry Red captures all the cities at last, output Red, if the country Black captures all the cities, output Black, if there is a tie, output two numbers, the first is the number of citiesbelonging to Red, the second is the number of the cities belonging to Black.
每一行一个用例结果,以 Case #i:开头,i为1开始的序号。如果红国胜利,输出Red,黑国胜利输出Black,否则输出红国和黑国的城市数量,中间用空格隔开
All You Need is to Wait