classSolution{public:introotCount(vector<vector<int>>&edges,vector<vector<int>>&guesses,intk){intans=0;constintn=edges.size()+1;vector<vector<int>>graph(n);vector<unordered_set<int>>guessGraph(n);vector<int>parent(n);for(constvector<int>&edge:edges){constintu=edge[0];constintv=edge[1];graph[u].push_back(v);graph[v].push_back(u);}for(constvector<int>&guess:guesses){constintu=guess[0];constintv=guess[1];guessGraph[u].insert(v);}// Precalculate `parent`.dfs(graph,0,/*prev=*/-1,parent);// Calculate `correctGuess` for tree rooted at 0.intcorrectGuess=0;for(inti=1;i<n;++i)if(guessGraph[parent[i]].contains(i))++correctGuess;reroot(graph,0,/*prev=*/-1,correctGuess,guessGraph,k,ans);returnans;}private:voiddfs(constvector<vector<int>>&graph,intu,intprev,vector<int>&parent){parent[u]=prev;for(constintv:graph[u])if(v!=prev)dfs(graph,v,u,parent);}voidreroot(constvector<vector<int>>&graph,intu,intprev,intcorrectGuess,constvector<unordered_set<int>>&guessGraph,constint&k,int&ans){if(u!=0){// The tree is rooted at u, so a guess edge (u, prev) will match the new// `parent` relationship.if(guessGraph[u].contains(prev))++correctGuess;// A guess edge (prev, u) matching the old `parent` relationship will no// longer be true.if(guessGraph[prev].contains(u))--correctGuess;}if(correctGuess>=k)++ans;for(constintv:graph[u])if(v!=prev)reroot(graph,v,u,correctGuess,guessGraph,k,ans);}};
classSolution{publicintrootCount(int[][]edges,int[][]guesses,intk){finalintn=edges.length+1;List<Integer>[]graph=newList[n];Set<Integer>[]guessGraph=newSet[n];int[]parent=newint[n];for(inti=0;i<n;++i){graph[i]=newArrayList<>();guessGraph[i]=newHashSet<>();}for(int[]edge:edges){finalintu=edge[0];finalintv=edge[1];graph[u].add(v);graph[v].add(u);}for(int[]guess:guesses){finalintu=guess[0];finalintv=guess[1];guessGraph[u].add(v);}// Precalculate `parent`.dfs(graph,0,/*prev=*/-1,parent);// Calculate `correctGuess` for tree rooted at 0.intcorrectGuess=0;for(inti=1;i<n;++i)if(guessGraph[parent[i]].contains(i))++correctGuess;reroot(graph,0,/*prev=*/-1,correctGuess,guessGraph,k);returnans;}privateintans=0;privatevoiddfs(List<Integer>[]graph,intu,intprev,int[]parent){parent[u]=prev;for(finalintv:graph[u])if(v!=prev)dfs(graph,v,u,parent);}privatevoidreroot(List<Integer>[]graph,intu,intprev,intcorrectGuess,Set<Integer>[]guessGraph,intk){if(u!=0){// The tree is rooted at u, so a guess edge (u, prev) will match the new// `parent` relationship.if(guessGraph[u].contains(prev))++correctGuess;// A guess edge (prev, u) matching the old `parent` relationship will no// longer be true.if(guessGraph[prev].contains(u))--correctGuess;}if(correctGuess>=k)++ans;for(finalintv:graph[u])if(v!=prev)reroot(graph,v,u,correctGuess,guessGraph,k);}}
classSolution:defrootCount(self,edges:list[list[int]],guesses:list[list[int]],k:int,)->int:ans=0n=len(edges)+1graph=[[]for_inrange(n)]guessGraph=[set()for_inrange(n)]parent=[0]*nforu,vinedges:graph[u].append(v)graph[v].append(u)foru,vinguesses:guessGraph[u].add(v)defdfs(u:int,prev:int)->None:parent[u]=prevforvingraph[u]:ifv!=prev:dfs(v,u)# Precalculate `parent`.dfs(0,-1)# Calculate `correctGuess` for tree rooted at 0.correctGuess=sum(iinguessGraph[parent[i]]foriinrange(1,n))defreroot(u:int,prev:int,correctGuess:int)->None:nonlocalansifu!=0:# The tree is rooted at u, so a guess edge (u, prev) will match the new# `parent` relationship.ifprevinguessGraph[u]:correctGuess+=1# A guess edge (prev, u) matching the old `parent` relationship will no# longer be True.ifuinguessGraph[prev]:correctGuess-=1ifcorrectGuess>=k:ans+=1forvingraph[u]:ifv!=prev:reroot(v,u,correctGuess)reroot(0,-1,correctGuess)returnans
Thanks for stopping by! If you find this site helpful, consider buying me some protein powder to keep me fueled! π