Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3690 | Accepted: 2193 |
Description
Bessie, feeling vindictive, decided to sabotage Farmer John's network by cutting power to one of the barns (thereby disrupting all the connections involving that barn). When Bessie does this, it breaks the network into smaller pieces, each of which retains full connectivity within itself. In order to be as disruptive as possible, Bessie wants to make sure that each of these pieces connects together no more than half the barns on FJ.
Please help Bessie determine all of the barns that would be suitable to disconnect.
Input
* Lines 2..N: Each line contains two integers X and Y and represents a connection between barns X and Y.
Output
Sample Input
10 1 2 2 3 3 4 4 5 6 7 7 8 8 9 9 10 3 8
Sample Output
3 8
Hint
The set of connections in the input describes a "tree": it connects all the barns together and contains no cycles.
OUTPUT DETAILS:
If barn 3 or barn 8 is removed, then the remaining network will have one piece consisting of 5 barns and two pieces containing 2 barns. If any other barn is removed then at least one of the remaining pieces has size at least 6 (which is more than half of the original number of barns, 5).
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8892 | Accepted: 3681 |
Description
For example, consider the tree:
Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node 1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees has two nodes, so the balance of node 1 is two.
For each input tree, calculate the node that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number.
Input
Output
Sample Input
1 7 2 6 1 2 1 4 4 5 3 7 3 1
Sample Output
1 2
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 4770 | Accepted: 1632 |
Description
Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders.
Unfortunately, the structure of Chicago mafia is rather complicated. There are n persons known to be related to mafia. The police have traced their activity for some time, and know that some of them are communicating with each other. Based on the data collected, the chief of the police suggests that the mafia hierarchy can be represented as a tree. The head of the mafia, Godfather, is the root of the tree, and if some person is represented by a node in the tree, its direct subordinates are represented by the children of that node. For the purpose of conspiracy the gangsters only communicate with their direct subordinates and their direct master.
Unfortunately, though the police know gangsters’ communications, they do not know who is a master in any pair of communicating persons. Thus they only have an undirected tree of communications, and do not know who Godfather is.
Based on the idea that Godfather wants to have the most possible control over mafia, the chief of the police has made a suggestion that Godfather is such a person that after deleting it from the communications tree the size of the largest remaining connected component is as small as possible. Help the police to find all potential Godfathers and they will arrest them.
Input
The first line of the input file contains n — the number of persons suspected to belong to mafia (2 ≤ n ≤ 50 000). Let them be numbered from 1 to n.
The following n − 1 lines contain two integer numbers each. The pair ai, bi means that the gangster ai has communicated with the gangster bi. It is guaranteed that the gangsters’ communications form a tree.
Output
Print the numbers of all persons that are suspected to be Godfather. The numbers must be printed in the increasing order, separated by spaces.
Sample Input
6 1 2 2 3 2 5 3 4 3 6
Sample Output
2 3
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 10010
using namespace std;
int head[N],etot,tot,n,ans[N];
struct edge{
int v,next;
}g[500010];
void add_edge(int u,int v)
{
g[etot].v=v;//g[etot].v是etot这条边指向的点
g[etot].next=head[u];//g[etot].next是etot这条边的前一条边的标号(即同一个节点出去的其他分支)
head[u]=etot++;//head[u]是u这个点出去的边的标号。
}
int dfs(int u,int fa)
{
int child_max=-1,sum=1;//要加上自己,所以sum初始化为1
for(int i=head[u];i!=-1;i=g[i].next)//i=-1,表示从这点出发的所有分支都搜过了。可以结束循环。
{
int v=g[i].v;
if(v==fa)//v==fa表示是来时的那条分支。不走。
continue;
int value=dfs(v,u);//往下搜。即搜v这个点,v这个点的来时的点是u
if(value>child_max)
child_max=value;//更新
sum+=value;
}
if(n-sum>child_max)
child_max=n-sum;
if(child_max<=n/2)
ans[tot++]=u;
return sum;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
etot=0;
tot=0;
memset(head,-1,sizeof(head));//head初始化为-1 表示前面么有了
for(int i=0;i<n-1;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add_edge(a,b);
add_edge(b,a);//加双向边
}
dfs(1,-1);//从1节点开始,将1节点的前一个节点设为-1
if(tot==0)
printf("NONE\n");
else
{
sort(ans,ans+tot);
for(int i=0;i<tot;i++)
{
printf("%d\n",ans[i]);
}
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 20010
int head[N],tot,etot,n;
struct edge{
int v,next;
}g[500010];
struct minn{
int point,value,time;
}mins;
void add_edge(int u,int v)
{
g[etot].v=v;
g[etot].next=head[u];
head[u]=etot++;
}
int dfs(int u,int fa)
{
int child_max=-1,sum=1;
for(int i=head[u];i!=-1;i=g[i].next)
{
int v=g[i].v;
if(v==fa)
continue;
int value=dfs(v,u);
if(value>child_max)
child_max=value;
sum+=value;
}
if(n-sum>child_max)
child_max=n-sum;
if(child_max<mins.value)
{
mins.value=child_max;
mins.point=u;
mins.time=1;
}
else if(child_max==mins.value)
{
mins.time++;
if(mins.point>u)
mins.point=u;
}
return sum;
}
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
memset(head,-1,sizeof(head));
mins.value=200000;
scanf("%d",&n);
for(int i=0;i<n-1;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add_edge(a,b);
add_edge(b,a);
}
dfs(1,-1);
printf("%d %d\n",mins.point,mins.value);
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
#define N 50010
#include <algorithm>
using namespace std;
int head[N],etot,tot,n,ans[N];
struct edge{
int v,next;
}g[500010];
void add_edge(int u,int v)
{
g[etot].v=v;
g[etot].next=head[u];
head[u]=etot++;
}
struct minn{
int value;
int point[N];
}mins;
int dfs(int u,int fa)
{
int child_max=-1,sum=1;
for(int i=head[u];i!=-1;i=g[i].next)
{
int v=g[i].v;
if(v==fa)
continue;
int value=dfs(v,u);
if(value>child_max)
child_max=value;
sum+=value;
}
if(n-sum>child_max)
child_max=n-sum;
if(child_max<mins.value)
{
mins.value=child_max;
//memset(ans,0,sizeof(ans));
tot=0;
ans[tot++]=u;
}
else if(child_max==mins.value)
{
ans[tot++]=u;
}
return sum;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(head,-1,sizeof(head));
etot=0;
mins.value=500000;
for(int i=0;i<n-1;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add_edge(a,b);
add_edge(b,a);
}
dfs(1,-1);
sort(ans,ans+tot);
//printf("%d\n",tot);
for(int i=0;i<tot;i++)
{
if(i==tot-1)
printf("%d\n",ans[i]);
else
printf("%d ",ans[i]);
}
}
return 0;
}