Skip to content

Commit eb0cf10

Browse files
committed
Uploaded new files
1 parent b7b09f4 commit eb0cf10

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed
File renamed without changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//Thursday 13-January-2022 21:16:42
2+
#include <bits/stdc++.h>
3+
//@ReetuRaj77
4+
#define lli long long int
5+
#define ulli unsigned long long int
6+
7+
#define all(x) (x).begin(),(x).end()
8+
#define MAXN 200005
9+
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr)
10+
11+
using namespace std;
12+
13+
int logs[MAXN + 5];
14+
int st[MAXN][30];
15+
16+
int main() {
17+
fastio;
18+
int n, q;
19+
cin >> n >> q;
20+
logs[1] = 0;
21+
for (int i = 2; i <= MAXN ; i++)logs[i] = logs[i / 2] + 1;
22+
vector<int>a(n);
23+
for (int i = 0; i < n; i++)cin >> a[i];
24+
int K = 25;
25+
26+
//sparse table calculation
27+
for (int i = 0; i < n; i++)
28+
st[i][0] = a[i];
29+
for (int j = 1; j <= K; j++) {
30+
for (int i = 0; i + (1 << j) <= n; i++) {
31+
st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
32+
}
33+
}
34+
//using sparse table to calculate the corresponding RMQs
35+
//the idea is to think that we can divide an interval into
36+
//parts of power of two..
37+
38+
for (int i = 0; i < q; i++) {
39+
int L, R;
40+
cin >> L >> R;
41+
int j = logs[R - L + 1]; //to calculate nearest power of two, to start
42+
43+
int min_range = min(st[L - 1][j], st[R - (1 << j)][j]);
44+
cout << min_range << endl;
45+
}
46+
}

TreeAlgorithms/DiameterOfTree.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//Tuesday 11-January-2022 11:49:02
2+
#include <bits/stdc++.h>
3+
//@ReetuRaj77
4+
#define lli long long int
5+
#define ulli unsigned long long int
6+
7+
#define all(x) (x).begin(),(x).end()
8+
9+
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr)
10+
11+
using namespace std;
12+
13+
vector<vector<int>>adj(200005, vector<int>());
14+
int dfs(int ch, int par, lli& res) {
15+
vector<lli>ans;
16+
for (auto x : adj[ch]) {
17+
if (x != par) {
18+
ans.push_back(dfs(x, ch, res));
19+
}
20+
}
21+
if (ans.size() == 0)return 0;
22+
sort(all(ans), greater<lli>());
23+
lli t = ans.front();
24+
if (ans.size() >= 2)
25+
res = max(res, ans[1] + ans[0] + 2);
26+
else
27+
res = max(res, t + 1);
28+
return 1 + t;
29+
30+
}
31+
int main() {
32+
fastio;
33+
int n;
34+
cin >> n;
35+
for (int i = 0; i < n - 1; i++) {
36+
int x, y;
37+
cin >> x >> y;
38+
adj[x].push_back(y);
39+
adj[y].push_back(x);
40+
}
41+
lli res = 0;
42+
int t = dfs(1, 0, res);
43+
cout << res << endl;
44+
}

TreeAlgorithms/Subordinates.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//Saturday 15-January-2022 11:22:42
2+
#include <bits/stdc++.h>
3+
//@ReetuRaj77
4+
#define lli long long int
5+
#define ulli unsigned long long int
6+
7+
#define all(x) (x).begin(),(x).end()
8+
9+
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr)
10+
11+
using namespace std;
12+
13+
vector<vector<int>>adj(200005, vector<int>());
14+
int subor[200005];
15+
int dfs(int src, int parent) {
16+
int cur = 1;
17+
for (auto A : adj[src]) {
18+
if (A != parent) {
19+
cur += dfs(A, src);
20+
}
21+
}
22+
return subor[src] = cur;
23+
}
24+
int main() {
25+
fastio;
26+
int n;
27+
cin >> n;
28+
for (int i = 2; i <= n; i++) {
29+
int x;
30+
cin >> x;
31+
adj[x].push_back(i);
32+
adj[i].push_back(x);
33+
}
34+
subor[1] = dfs(1, 0);
35+
for (int i = 1; i <= n; i++) {
36+
cout << subor[i] - 1 << " ";
37+
}
38+
return 0;
39+
}

TreeAlgorithms/TreeMatching.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//Friday 17-December-2021 23:38:07
2+
#include <bits/stdc++.h>
3+
//@ReetuRaj77
4+
#define lli long long int
5+
#define ulli unsigned long long int
6+
7+
#define all(x) (x).begin(),(x).end()
8+
9+
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr)
10+
11+
using namespace std;
12+
vector<vector<int>>adj(200005, vector<int>());
13+
int ans = 0;
14+
bool marked[200005] = {false};
15+
16+
void parenting_dfs(int source, int parent) {
17+
for (int i = 0; i < adj[source].size(); i++) {
18+
if (adj[source][i] != parent) {
19+
parenting_dfs(adj[source][i], source);
20+
}
21+
}
22+
if (!marked[source] && !marked[parent] && parent != 0) {
23+
ans ++;
24+
marked[source] = marked[parent] = true;
25+
}
26+
}
27+
28+
int main() {
29+
fastio;
30+
int n;
31+
cin >> n;
32+
33+
for (int i = 0; i < n - 1; i++) {
34+
int x, y;
35+
cin >> x >> y;
36+
adj[x].push_back(y);
37+
adj[y].push_back(x);
38+
}
39+
40+
parenting_dfs(1, 0);
41+
cout << ans << endl;
42+
}

0 commit comments

Comments
 (0)