#include using namespace std; template class Graph{ public: vector> edges; vector parent; int numberOfVertices; vector> adjacencyMatrix; vector> connected; vector> children; vector verticesValue; vector edgesValue; Graph(vector> e,int N){ edges=e; numberOfVertices=N; } vector> getConnected(){ if(connected.size()>0)return connected; if(edges.size()>0){ connected.resize(numberOfVertices); connected.clear(); for(int i=edges.size()-1;i>=0;i--){ connected[edges[i][0]].push_back(edges[i][1]); connected[edges[i][1]].push_back(edges[i][0]); } return connected; } } vector getParent(int root=0){ if(parent.size()>0)return parent; parent.resize(numberOfVertices); vector isWritten(numberOfVertices); vector> c=getConnected(); queue> q; q.push({-1,root}); while(!q.empty()){ if(isWritten[q.front()[1]]){ goto end; } isWritten[q.front()[1]]=true; parent[q.front()[1]]=q.front()[0]; for(int i:connected[q.front()[1]]){ q.push({q.front()[1],i}); } end:{} q.pop(); } return parent; } vector> getChildren(int root=0){ getParent(root); if(children.size()>0)return children; children.resize(numberOfVertices); children.clear(); for(int i=0;i-1){ children[parent[i]].push_back(i); } } return children; } vector orderFromChildren(){ getParent(); getChildren(); vector ret(0); vector count(numberOfVertices); queue q; for(int i=0;i>N>>S; vector> edges(0,vector(2)); int a,b; for(int i=0;i>a>>b; edges.push_back({a-1,b-1}); } Graph g(edges,N); vector isC(N); for(int i=0;i ord=g.orderFromChildren(); vector c(N),w(N); for(int i:ord){ c[i]+=isC[i]; w[i]+=(1-isC[i]); if(g.parent[i]!=-1){ c[g.parent[i]]+=c[i]; w[g.parent[i]]+=w[i]; } } vector cw(N),ww(N),pcw(N),pww(N); for(int i:ord){ if(!isC[i]){ cw[i]+=c[i]; ww[i]+=w[i]-1; pcw[i]=c[i]; pww[i]=w[i]-1; } if(g.parent[i]!=-1){ cw[g.parent[i]]+=cw[i]; ww[g.parent[i]]+=ww[i]; } } vector cww(N); for(int i:ord){ if(isC[i]){ cww[i]+=ww[i]; cww[i]+=w[i]*cw[i]; for(int j:g.children[i]){ cww[i]-=w[j]*cw[j]; } cww[i]+=(c[i]-1)*ww[i]; for(int j:g.children[i]){ cww[i]-=c[j]*ww[j]; } }else{ cww[i]+=cw[i]-pcw[i]; cww[i]+=c[i]*(w[i]-1); for(int j:g.children[i]){ cww[i]-=c[j]*w[j]; } cww[i]+=(w[i]-1)*(cw[i]-pcw[i]); for(int j:g.children[i]){ cww[i]-=w[j]*cw[j]; } cww[i]+=c[i]*(ww[i]-pww[i]); for(int j:g.children[i]){ cww[i]-=c[j]*ww[j]; } } if(g.parent[i]!=-1){ cww[g.parent[i]]+=cww[i]; } } cout<