Portal
Topic: Directionless graph with n points, n paths (n-1<m<=2*m), n points with some candy, some monsters, and point 1 with candy. When you start at 1:00 and ask for the expectation of finding the biggest candy, you are smart and know the map. When you first meet a monster, you have a chance to escape that point.
Idea: First, dfs records all the points connected with a point, and then traverses the monster points connected with a point connected block to update the maximum value.
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #include <algorithm> #include <math.h> #include <map> #include <iterator> #include <vector> #include <set> #include <bitset> #include <stack> #define ull unsigned long long #define Mod(x) (x+mod)%mod #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define rep(i,a,b) for(int i=a;i<=b;i++) #define mems(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; const ll mod=1e9+7;; const int N=1e5+5; const double pi=acos(-1); const int inf=0x3f3f3f3f; const int M=50000+5; int num; int vi[N],vis[N],fi[N]; int ans[N],tot; vector<int>ve[N]; void dfs(int u,int fa) { vis[u]=1; num++; for(int i=0;i<ve[u].size();i++) { int v=ve[u][i]; if(v==fa) continue; if(vi[v]) { ans[tot++]=v; continue; } if(!vis[v]) { dfs(v,u); } } } void dfs1(int u,int fa) { if(vis[u]||vi[u]) return; fi[u]=fa; num++; for(int i=0;i<ve[u].size();i++) { int v=ve[u][i]; if(fi[v]==fa) continue; dfs1(v,fa); } } void init(int n) { for(int i=0;i<=n;i++) { ve[i].clear(); vi[i]=0; vis[i]=0; fi[i]=0; } num=0; tot=0; } int main() { int t; scanf("%d",&t); while(t--) { int n,m,k; scanf("%d%d%d",&n,&m,&k); init(n); for(int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); ve[a].push_back(b); ve[b].push_back(a); } for(int i=1;i<=k;i++) { int a; scanf("%d",&a); vi[a]=1; } dfs(1,-1); int ljt=num; double f=0.0; int v=0; for(int i=0;i<tot;i++) { num=0; for(int j=0;j<ve[ans[i]].size();j++) { dfs1(ve[ans[i]][j],++v); // printf("%d %d**\n",ve[ans[i]][j],num); } double ff=(double)num/ve[ans[i]].size(); // printf("%d %d %d %.6f*\n",ans[i],num,ve[ans[i]].size(),ff); if(ff>f) f=ff; } printf("%.6f\n",ljt+f); } }