-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2421. Number of Good Paths.cpp
117 lines (113 loc) · 3.75 KB
/
2421. Number of Good Paths.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
struct DSU{
vector<int> parent,value,count;
int goodPoint;
DSU(int n, vector<int> _value):parent(n,0),value(_value),count(n,1),goodPoint(0){
iota(parent.begin(),parent.end(),0);
}
int find(int id){
if(parent[id]==id) return id;
return parent[id] = find(parent[id]);
}
void merge(int id1,int id2){
int pid1 = find(id1);
int pid2 = find(id2);
if(pid1==pid2) return;
if(value[pid1] < value[pid2]){
parent[pid1] = pid2;
//++goodPoint;
}
else if(value[pid1] > value[pid2]){
parent[pid2] = pid1;
//++goodPoint;
}
else {
parent[pid1] = pid2;
//goodPoint += count[pid1]*count[pid2];
//count[pid1] += count[pid2];
}
}
};
class Solution {
public:
int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {
int n = vals.size();
vector<vector<int>> adj(n);
for (auto& edge : edges) {
adj[edge[0]].push_back(edge[1]);
adj[edge[1]].push_back(edge[0]);
}
map<int, vector<int>> valuesToNodes;
for (int node = 0; node < n; node++) {
valuesToNodes[vals[node]].push_back(node);
}
DSU dsu(n,vals);
int goodPaths = 0;
for (auto& [value, nodes] : valuesToNodes) {
for (int node : nodes) {
for (int neighbor : adj[node]) {
if (vals[node] >= vals[neighbor]) {
dsu.merge(node, neighbor);
}
}
}
unordered_map<int, int> group;
for (int u : nodes) {
group[dsu.find(u)]++;
}
for (auto& [_, size] : group) {
goodPaths += (size * (size + 1) / 2);
}
}
return goodPaths;
}
// DFS solution 123/134 tle
int numberOfGoodPaths1(vector<int>& vals, vector<vector<int>>& edges) {
int n = vals.size();
vector<vector<int>> adj(n);
for(auto& e:edges){
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(e[0]);
}
unordered_map<int,int> temp;
map<int,int> path; // path with value; we need sorted
for(auto& v:vals)
temp[v]++;
// for(auto& t:temp)
// if(t.second>1)
// path[t.first] = 0;
int result = 0;
vector<bool> visited(n,false);
function<void(int, map<int,int>&)> dfs = [&](int node,map<int,int>& paths){
auto it = paths.lower_bound(vals[node]);
map<int,int> pathcopy(it,paths.end());
//auto pathcopy = paths;
// for(auto& p:pathcopy){
// if(p.first < vals[node])
// p.second = 0;
// else break;
// }
visited[node] = true;
for(auto child:adj[node])
if(!visited[child]){
dfs(child,pathcopy);
auto it = pathcopy.lower_bound(vals[node]);
pathcopy.erase(pathcopy.begin(),it);
// for(auto& p:pathcopy){
// if(p.first < vals[node])
// p.second = 0;
// else break;
// }
}
//cout<<node<<" = "<<pathcopy[vals[node]]<<endl;
result += pathcopy[vals[node]];
for(auto& p:pathcopy){
if(p.second > paths[p.first])
paths[p.first] = p.second;
}
if(temp[vals[node]]>1)
paths[vals[node]]++;
};
dfs(0,path);
return result+n;
}
};