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.
代码
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; }
代码
相关文章推荐
- WTO框架下经济结构调整和产业升级
- ant 使用指南
- 【iOS学习笔记 15-11-10】Objective-C中根据函数名调用函数
- 字符串和数组之间的装换
- ant 使用指南
- SQL中Group By, Having, Where, Order by几个语句的执行顺序
- 1 nrf51822简介
- mysql 获取当月de日期信息
- sicily 1375 Balanced lineup
- MyEclipse生成的Hibernate一对一配置问题
- 可拖动的listview
- Linux下nginx安装笔记
- 利用状态机编程思想求解统计英文句子中单词个数问题
- spark视频课程下载链接
- 硬盘的接口种类
- AOP 实现原理
- c++(选择排序)
- c++(单词统计)
- 在Android Eclipse 开发如何 使用 (*.aar)文件
- [LeetCode162]Find Peak Element