求解
代码如下: 问题, vector father=vector(n,0);不报错,而使用vector father(n,0);报错未定义的n?为什么?
class Solution {
private:
int n=200005;
vector father=vector(n,0);
// vector father(n,0);
void init(){
for(int i=0;i father[i]=i;
}
}
int find(int u){
return u==father[u]?u:find(father[u]);
}
bool isSame(int u,int v){
u=find(u);
v=find(v);
return u==v;
}
void join(int u,int v){
u=find(u);
v=find(v);
if(u==v) return;
father[v]=u;
}
public:
bool validPath(int nn, vector>& edges, int source, int destination) {
init();
for(int i=0;i join(edges[i][0],edges[i][1]);
}
return isSame(source,destination);
}
};
class Solution {
private:
int n=200005;
vector
// vector
void init(){
for(int i=0;i
}
}
int find(int u){
return u==father[u]?u:find(father[u]);
}
bool isSame(int u,int v){
u=find(u);
v=find(v);
return u==v;
}
void join(int u,int v){
u=find(u);
v=find(v);
if(u==v) return;
father[v]=u;
}
public:
bool validPath(int nn, vector
init();
for(int i=0;i
}
return isSame(source,destination);
}
};
全部评论
相关推荐