1 Star 1 Fork 0

laodasbch/Leetcode-Complete-Guide

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
1579.txt 2.76 KB
一键复制 编辑 原始数据 按行查看 历史
JunB(66哥) 提交于 5年前 . Update 1579.txt
思路:
1.add edge instead remove
2. union find
3. 理由:想象minimum spanning tree
代码:
class Solution {
boolean visit[];
List<Integer>adjecent[];
public int maxNumEdgesToRemove(int n, int[][] edges) {
int A[]=new int[n];
int B[]=new int[n];
for(int i=0;i<n;i++){
A[i]=i;
B[i]=i;
}
int res=0;
for(int pair[]:edges){
if(pair[0]!=3)continue;
int v1=pair[1]-1;
int v2=pair[2]-1;
int root1=find(A,v1);
int root2=find(A,v2);
int root3=find(B,v1);
int root4=find(B,v2);
if(root1==root2&&root3==root4){
res++;
}else{
A[root1]=root2;
B[root1]=root2;
}
}
for(int pair[]:edges){
if(pair[0]==3)continue;
int v1=pair[1]-1;
int v2=pair[2]-1;
if(pair[0]==1){
int root1=find(A,v1);
int root2=find(A,v2);
if(root1==root2)res++;
else{
A[root1]=root2;
}
}else{
int root3=find(B,v1);
int root4=find(B,v2);
if(root3==root4)res++;
else{
B[root3]=root4;
}
}
}
visit=new boolean[n];
adjecent=new ArrayList[n];
for(int i=0;i<n;i++)adjecent[i]=new ArrayList<>();
for(int pair[]:edges){
if(pair[0]==3||pair[0]==1)
{
int v1=pair[1]-1;
int v2=pair[2]-1;
adjecent[v1].add(v2);
adjecent[v2].add(v1);
}
}
dfs(0);
for(boolean b:visit){
if(!b)return -1;
}
visit=new boolean[n];
adjecent=new ArrayList[n];
for(int i=0;i<n;i++)adjecent[i]=new ArrayList<>();
for(int pair[]:edges){
if(pair[0]==3||pair[0]==2)
{
int v1=pair[1]-1;
int v2=pair[2]-1;
adjecent[v1].add(v2);
adjecent[v2].add(v1);
}
}
dfs(0);
for(boolean b:visit){
if(!b)return -1;
}
return res;
}
public void dfs(int root){
visit[root]=true;
List<Integer>childs=adjecent[root];
for(int c:childs){
if(visit[c])continue;
dfs(c);
}
}
public int find(int nums[],int x){//union find => find method
if(nums[x]==x)return x;
int root=find(nums,nums[x]);
nums[x]=root;
return root;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/laodasbch/Leetcode-Complete-Guide.git
git@gitee.com:laodasbch/Leetcode-Complete-Guide.git
laodasbch
Leetcode-Complete-Guide
Leetcode-Complete-Guide
master

搜索帮助