Description
![](https://i-blog.csdnimg.cn/blog_migrate/811b9bb62e81a1644fabf26a0ab8f164.png)
Input
![](https://i-blog.csdnimg.cn/blog_migrate/3633c73e6c14ef4df3bfad8f1042b5b0.png)
Output
![](https://i-blog.csdnimg.cn/blog_migrate/c05eed01fab5f59c35d396ef83283af1.png)
Sample Input
5 7 2 2 3 1 2 1 3 1 4 2 4 3 4 3 5 4 5
Sample Output
1 2 2 1 2
Data Constraint
![](https://i-blog.csdnimg.cn/blog_migrate/69f132c56aa74e3345f39bd3aa102214.png)
Hint
![](https://i-blog.csdnimg.cn/blog_migrate/edab5ef3ceec0c28648132fea626a030.png)
分析
显然我们对于每个绿洲求spfa即可
(为什么我dij堆优化炸了????)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream> #include <cstdio> #include <deque> #include <memory.h> using namespace std; const int N=1e5+10; struct Edge { int v,nx; }g[2*N]; int cnt,list[N],d[N],ans[N]; bool b[N],gr[N]; int n,m,k; void Add(int u,int v) { g[++cnt].v=v;g[cnt].nx=list[u];list[u]=cnt; } void Spfa(int v0) { int cnt=1,sum=0; deque<int> q; while (!q.empty()) q.pop_front(); memset(d,0x3f,sizeof d);memset(b,0,sizeof b); q.push_back(v0); d[v0]=0;b[v0]=1; while (!q.empty()) { int u=q.front();q.pop_front(); if (d[u]*cnt>sum) { q.push_back(u); continue; } cnt--;sum-=d[u]; for (int i=list[u];i;i=g[i].nx) if (d[g[i].v]>d[u]+1) { d[g[i].v]=d[u]+1; if (!b[g[i].v]) { if (!q.empty()&&d[g[i].v]<d[q.front()]) q.push_front(g[i].v); else q.push_back(g[i].v); cnt++;sum+=d[g[i].v]; } b[g[i].v]=1; } b[u]=1; } } int main() { freopen("oasis.in","r",stdin); freopen("oasis.out","w",stdout); scanf("%d%d%d",&n,&m,&k); for (int i=1;i<=k;i++) { int id; scanf("%d",&id); gr[id]=1; } for (int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); Add(u,v);Add(v,u); } for (int i=1;i<=n;i++) if (gr[i]) { Spfa(i); for (int i=1;i<=n;i++) ans[i]=max(ans[i],d[i]); } for (int i=1;i<=n;i++) printf("%d ",ans[i]); fclose(stdin);fclose(stdout); }