Skip to content

Commit efae46c

Browse files
committedJul 19, 2020
Tree Isomorphisms solution correction
1 parent b68d285 commit efae46c

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed
 

‎src/1700 - Tree Isomorphism I.cpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@ Author: Sachin Srivastava (mrsac7)
55
*/
66
#include<bits/stdc++.h>
77
using namespace std;
8-
8+
99
#define int long long
1010
#define endl '\n'
11-
12-
string dfs(int s, int p, vector<int> adj[]) {
13-
vector<string> v;
11+
12+
map<vector<int>,int> mp;
13+
int idx = 0;
14+
int dfs(int s, int p, vector<int> adj[]) {
15+
vector<int> v;
1416
for (auto i: adj[s]) {
1517
if (i != p)
1618
v.push_back(dfs(i, s, adj));
1719
}
1820
sort(v.begin(), v.end());
19-
string ans = "(";
20-
for (auto i: v)
21-
ans += i;
22-
ans += ')';
23-
return ans;
21+
if (!mp.count(v)) mp[v] = idx++;
22+
return mp[v];
2423
}
2524
signed main(){
2625
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
@@ -43,8 +42,8 @@ signed main(){
4342
adj2[x].push_back(y);
4443
adj2[y].push_back(x);
4544
}
46-
string s1 = dfs(1, -1, adj1);
47-
string s2 = dfs(1, -1, adj2);
45+
int s1 = dfs(1, -1, adj1);
46+
int s2 = dfs(1, -1, adj2);
4847
if (s1 == s2)
4948
cout<<"YES";
5049
else cout<<"NO";

‎src/1701 - Tree Isomorphism II.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ Author: Sachin Srivastava (mrsac7)
55
*/
66
#include<bits/stdc++.h>
77
using namespace std;
8-
8+
99
#define int long long
1010
#define endl '\n'
11-
11+
1212
vector<int> center(int n, vector<int> adj[]) {
1313
int deg[n+1] = {0};
1414
vector<int> v;
@@ -32,18 +32,18 @@ vector<int> center(int n, vector<int> adj[]) {
3232
}
3333
return v;
3434
}
35-
string dfs(int s, int p, vector<int> adj[]) {
36-
vector<string> v;
35+
map<vector<int>, int> mp;
36+
int idx = 0;
37+
38+
int dfs(int s, int p, vector<int> adj[]) {
39+
vector<int> v;
3740
for (auto i: adj[s]) {
3841
if (i != p)
3942
v.push_back(dfs(i, s, adj));
4043
}
4144
sort(v.begin(), v.end());
42-
string ans = "(";
43-
for (auto i: v)
44-
ans += i;
45-
ans += ')';
46-
return ans;
45+
if (!mp.count(v)) mp[v] = idx++;
46+
return mp[v];
4747
}
4848
signed main(){
4949
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
@@ -67,10 +67,10 @@ signed main(){
6767
adj2[y].push_back(x);
6868
}
6969
vector<int> v1 = center(n, adj1), v2 = center(n, adj2);
70-
string s1 = dfs(v1[0], -1, adj1);
70+
int s1 = dfs(v1[0], -1, adj1);
7171
int f = 0;
7272
for (auto s: v2) {
73-
string s2 = dfs(s, -1, adj2);
73+
int s2 = dfs(s, -1, adj2);
7474
if (s1 == s2)
7575
f = 1;
7676
}

0 commit comments

Comments
 (0)
Please sign in to comment.