Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit80fd573

Browse files
authored
Merge pull requestneetcode-gh#1103 from julienChemillier/patch-14
Add 684 in c language
2 parents89b07fa +679bc4c commit80fd573

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎c/684-Redundant-Connection.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Return an edge that can be removed so that the resulting graph is a tree of n nodes.
3+
Time: O(n)
4+
Space: O(n)
5+
*/
6+
intmax(inta,intb) {
7+
returna>b?a:b;
8+
}
9+
10+
intfind(int*parent,intk) {
11+
if (parent[k]==k)
12+
returnk;
13+
returnfind(parent,parent[k]);
14+
}
15+
16+
17+
18+
int*findRedundantConnection(int**edges,intedgesSize,int*edgesColSize,int*returnSize){
19+
int*ans=malloc(sizeof(int)*2);
20+
int*parent=malloc(sizeof(int)*(edgesSize+1));
21+
for (inti=0;i<=edgesSize;i++)
22+
parent[i]=i;
23+
for (inti=0;i<edgesSize;i++) {
24+
intf1=find(parent,edges[i][0]);
25+
intf2=find(parent,edges[i][1]);
26+
if (f1==f2) {
27+
ans[0]=edges[i][0];
28+
ans[1]=edges[i][1];
29+
}else {// Union
30+
parent[f1]=f2;
31+
}
32+
}
33+
*returnSize=2;
34+
returnans;
35+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp