您的位置:首页 > 其它

Codeforces Round #328 (Div. 2)D. Super M 虚树直径

2015-11-10 15:54 225 查看
[b]D. Super M[/b]

Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ superhero. Byteforces is a country that consists of n cities, connected by n - 1 bidirectional roads. Every road connects exactly two distinct cities, and the whole road system is designed in a way that one is able to go from any city to any other city using only the given roads. There are m cities being attacked by humans. So Ari... we meant Super M have to immediately go to each of the cities being attacked to scare those bad humans. Super M can pass from one city to another only using the given roads. Moreover, passing through one road takes her exactly one kron - the time unit used in Byteforces.

///1085422276

#include<bits/stdc++.h>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';ch=getchar();
}return x*f;
}
//****************************************
const int  N=123456+50;
#define mod 1000000007
#define inf 1000000007

vector<int >G[N*2];
int Mark[N*2],d[N*2],q[N*2];
void dfs(int x,int pre) {
int bo=Mark[x];
for(int i=0;i<G[x].size();i++) {
if(G[x][i]==pre) continue;
d[G[x][i]]=d[x]+1;
dfs(G[x][i],x);
if(q[G[x][i]]) bo=1;
}
q[x]=bo;
}
int main() {
int n,m,v,u,x,mx=0;
n=read(),m=read();
for(int i=1;i<=n-1;i++) {
scanf("%d%d",&u,&v);
G[u].pb(v);G[v].pb(u);
}mem(Mark);
for(int i=1;i<=m;i++) {
scanf("%d",&x);Mark[x]=1;
}
mem(d);mem(q);
dfs(1,-1);mx=0,v=1;
for(int i=1;i<=n;i++) {
if(Mark[i]&&d[i]>mx) {
mx=d[i];v=i;
}
}mem(d);mx=0;
int sum=0;
dfs(v,-1);
for(int i=1;i<=n;i++) {
if(q[i]) {
mx=max(mx,d[i]);sum++;
}
}
for(int i=1;i<=n;i++) {
if(q[i]&&d[i]==mx) {
v=min(v,i);
}
}
printf("%d\n%d\n",v,(sum-1)*2-mx);
return 0;
}


代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: