LeetCode 690. 员工的重要性
原题链接
简单
作者:
尤雨溪
,
2021-05-01 15:17:59
,
所有人可见
,
阅读 464
并查集
/*
// Definition for Employee.
class Employee {
public:
int id;
int importance;
vector<int> subordinates;
};
*/
class Solution {
public:
int p[2010];
int d[2010];
int find(int x)
{
return p[x] == x?p[x]:p[x] = find(p[x]);
}
void merge(int x,int y)
{
x=find(x);
y=find(y);
p[y]=x;
}
int getImportance(vector<Employee*> employees, int id) {
for(int i=1;i<=2000;i++) p[i]=i;
for(int i=0;i<employees.size();i++)
{
Employee index=*employees[i];
d[index.id]=index.importance;
for(int j=0;j<index.subordinates.size();j++)
{
int t=index.subordinates[j];
if(t!=id)
merge(index.id,t);
}
}
int ans=0;
for(int i=1;i<=2000;i++)if(find(i)==id)ans+=d[i];
return ans;
}
};
大佬
这个怎么解释啊?