12178. damage bullpen

John realized that Bessie build a network cost him a huge amount of funds, put her fired. Bessie very angry, intends to severely revenge. She intends to destroy the newly built John's network. John is a tree network is connected with N (1≤N≤10000) a cowshed, she plans to cut off the power of one of the bullpen, and the bullpen all cables connected to all the disruption. After, there will be the presence of several sub-networks. In order to ensure the destruction is large enough, the bullpen each subnet number shall not exceed half of the total number of the bullpen, which was destroyed bullpen worth it?

Input formats:

Line 1: N. integer 
of 2 to N + 1 rows: two integers per line, a cable showing two endpoints.

Output formats:

In ascending order, all output is worth the destruction of the bullpen. Without a worthy destroyed, output NONE

Example 1:

Input: 
10 . 1 2 2. 3 . 3. 4 . 4. 5 . 6. 7 . 7. 8 . 8. 9 . 9 10 . 3. 8
Output: 
3 8
Note: 
If the bullpen bullpen 8 3 or destroyed, the remaining three sub-nodes will be 5,2,2, does not exceed 5.



#include <cstdio>
#include <vector>

the using namespace STD @;
// graph theory
// meaning of problems, given an unrooted trees, seeking to meet the requirements of cut points
@ requirements: the maximum Unicom remove the cut point components generated no more than n / 2 nodes
const int N = 100002;

int n, num [N], pre [N], root, age = 0;

std::vector<int> g[N];

int dfs(int cur,int father)
{
pre[cur]=father;
if(cur!=root && g[cur].size()==1)
{
return num[cur]=1;
}
int sum=0,v;
for(int i=0;i<g[cur].size();++i)
{
v=g[cur][i];
if(v!=father)
{
sum+=dfs(v,cur);
}
}
return num[cur]=sum+1;
}

int main ()
{
Scanf ( "% D", & n-);
int A, B;
for (int I =. 1; I <n-; ++ I)
{
Scanf ( "% D% D", & A, & B);
G [a] .push_back (B);
G [B] .push_back (a);
}
// selected from a first node as a root
the root =. 1;
// deep search again, in order to identify dfs, all subtrees the number of nodes,
DFS (1,0);
// traverse all the nodes, to find the cut point satisfies the requirements
for (int I =. 1; I <= n-; I ++)
{
// if the removal of the subtree i, the number of remaining nodes not meet the requirements, then skip this subtree, continue to search for the next subtree
IF (NUM [. 1] -num [I]> n-/ 2)
{
continue;
}
// traversing the subtree all subtrees
BOOL P = to true;
for (int J = 0; J <G [I] .size (); J ++)
{
int V G = [I] [J];
// skip parent node
if ( pre == V [I])
{
Continue;
}
IF (NUM [V]>n/2)
{
p=false;
break;
}
}
if(p==true)
{
++ans;
printf("%d\n",i);
}
}
if(ans==0)
{
printf("NONE");
}

return 0;
}

Guess you like

Origin www.cnblogs.com/jishuren/p/12233328.html