#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); for(int i=connected.size()-1;i>=0;--i){ connected[i]={}; } 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); for(int i=children.size()-1;i>=0;--i){ children[i]={}; } 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; 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 o=g.orderFromChildren(); vector> ans(N,vector(2,0)); vector> c=g.getChildren(); for(int i:o){ a=1; b=0; for(int j:c[i]){ a+=ans[j][1]; b+=max(ans[j][0],ans[j][1]); } ans[i][0]=a; ans[i][1]=b; } cout<