Skip to content

Commit

Permalink
Update 2581.Count-Number-of-Possible-Root-Nodes.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Mar 5, 2023
1 parent 2b01c65 commit 40176bc
Showing 1 changed file with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ class Solution {
unordered_set<int>guess[100005];
int ret = 0;
int k;
int along[100005];
int against[100005];
int count[100005];
public:
int rootCount(vector<vector<int>>& edges, vector<vector<int>>& guesses, int k)
{
Expand All @@ -21,26 +20,22 @@ class Solution {

dfs(0, -1);

dfs2(0, -1, along[0]);
dfs2(0, -1, count[0]);

return ret;
}

void dfs(int cur, int parent)
{
int count = 0;
for (int nxt: next[cur])
{
if (nxt==parent) continue;

if (guess[cur].find(nxt)!=guess[cur].end())
along[cur] += 1;
if (guess[nxt].find(cur)!=guess[nxt].end())
against[cur] += 1;
count[cur] += 1;

dfs(nxt, cur);
along[cur] += along[nxt];
against[cur] += against[nxt];
count[cur] += count[nxt];
}
}

Expand Down

0 comments on commit 40176bc

Please sign in to comment.