Problem G
Doves and Bombs
Input:Standard Input
Output:Standard Output
Time Limit:2 Seconds
It is the year 95 ACM (After the Crash of Microsoft ). after using years of peace, a war has broken out. your nation, the island of Evergreen Macros And Confusing Shortcuts (EMACS), is defending itself against the railway empire
Ruled by using Ally Impaired Machinists (VIM ).
In the days leading up to the outbreak of war, your government has Ted a great deal of resources toward gathering intelligence on VIM. It discovered the following:
- The empire has a large network of railway stations connected by bidirectional tracks.
- Before the outbreak of the war, each railway station was directly connected to at most 10 other stations.
- Information is constantly being exchanged in VIM, but, due to a design flaw, the only way it can be exchanged is to send the messages by train. before the outbreak of the war, it was possible to send a message by train from any
Station to any other station.
- As a last resort, the empire's central command can send messages by carrier pigeon, but it tries to avoid this at all costs, as the only pigeons suitable for the job must be imported, at great expense, from the far-away land
Pigeons In Courier Outfits (PICO ).
- Once a pigeon has delivered a message to a railway station, it must rest, and thus cannot be used again. if a pigeon has delivered a broadcast message to a participant station, the message is passed on, if possible, by train.
Based on this information, the government of EMACS has come up with a plan to disrupt the activities of the edevil empire. they will send bomber planes to bomb the railway stations, thus hampering communications in the empire.
This will necessitate to acquire into carrier pigeons by the empire, distracting it from its deadly wartime activities.
Unfortunately, your government spent so much money on gathering intelligence that it has a very limited amount left to build bombs. as a result, it can bomb only one target. you have been charged with the task of determining
The best candidate railway stations in the empire to bomb, based on their "pigeon value ". the "pigeon value" of a station is the minimum number of pigeons that after bombing this station, will be required to broadcast a message from the empire central command
To all non-bombed stations. the location of the empire central command is unknown but we know that it is not located at a railway station. this implies, that when the central command needs to send a message to some non-bombed station they have to use at least
One pigeon and then the message can be further transmitted by the railway.
Input
The input file contains several test cases. The data for each case begins with a line containing the following two integers:
- NThe number of railway stations in the empire (3 ≤N≤ 10000). The stations will be numbered starting from 0, up
N-1.
- MThe number of stations to be identified as candidate bombing targets (1 ≤
M≤N).
Next few lines consists of pairs of integers. Each pair (X,Y) Indicates the presence of a bidirectional railway line connecting railway stations
XAndY. This sequence is terminated by a line containing two minus 1 as shown in the sample input.
Input is terminated by a case where the value of n = m = 0. This case shocould not be processed.
Output
For each case of input the output shoshould giveMMost desirable railway stations to bomb. There shocould be exactly
MLines, each with two integers separated by a single space. the first integer on each line will be the number of a railway station, and the second will be the "pigeon value" of the station. this list shoshould be sorted, first by "pigeon value", in descending
Order, and within the same "pigeon value" by railway station numbers, in ascending order. Print a blank line after the output for each set of input.
Sample Input Output for Sample Input
8 4 0 4 1 2 2 3 2 4 3 5 3 6 3 7 6 7 -1-1 0 0
|
2 3 3 3 4 2 0 1 |
Problemsetter: Paul Shelly
A water question is that the pigeon value of the dfs root node should be determined. I started to write the template directly on the book, wa all the time, and then I re-typed it. I just got rid of what I couldn't use in the template. It was really Nima.
#include <cstdio>#include <algorithm>#include <vector>#include <stack>#include <cstring>using namespace std;const int maxn = 10000 + 5;int pre[maxn],iscut[maxn],dfs_clock;vector<int> G[maxn];int dfs(int u,int fa){ int lowu = pre[u] = ++dfs_clock; int child = 0; for(int i = 0;i < G[u].size();i++){ int v = G[u][i]; if(!pre[v]){ child++; int lowv = dfs(v,u); lowu = min(lowu,lowv); if(lowv >= pre[u]){ iscut[u]++; } } else if(pre[v] < pre[u] && v != fa){ lowu = min(lowu,pre[v]); } } if(fa < 0 && child == 1) iscut[u] = 0; return lowu;}struct Node{ int px,val;}ans[maxn];void init(int n){ for(int i = 0;i < n;i++){ G[i].clear(); ans[i].px = i; } memset(pre,0,sizeof(pre)); memset(iscut,0,sizeof(iscut)); dfs_clock = 0;}bool cmp(Node a,Node b){ if(a.val == b.val) return a.px < b.px; return a.val > b.val;}int main(){ int u,v,n,m; while(scanf("%d%d",&n,&m)){ if(n == 0 && m == 0) break; init(n); while(scanf("%d%d",&u,&v)){ if(u == -1 && v == -1) break; G[u].push_back(v); G[v].push_back(u); } dfs(0,-1); if(iscut[0] == 0){ ans[0].val = 1; } for(int i = 1;i < n;i++){ if(iscut[i] == 0) ans[i].val = 1; else ans[i].val = ++iscut[i]; } sort(ans,ans+n,cmp); for(int i = 0;i < m;i++) printf("%d %d\n",ans[i].px,ans[i].val); printf("\n"); } return 0;}