Skip to content

Commit b052bbb

Browse files
committed
Solve alot of problems :3
1 parent ec3317e commit b052bbb

13 files changed

+545
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1e5 + 1;
6+
int t, n, m, id, dfs, idx[N], low[N], tmp;
7+
bool in[N];
8+
vector<int> st;
9+
vector<vector<int> > g;
10+
11+
void DFS(int v) {
12+
idx[v] = low[v] = dfs++;
13+
in[v] = true;
14+
st.push_back(v);
15+
16+
for(int i = 0, u; i < g[v].size(); ++i) {
17+
u = g[v][i];
18+
if(idx[u] == -1) {
19+
DFS(u);
20+
low[v] = min(low[v], low[u]);
21+
} else if(in[u])
22+
low[v] = min(low[v], low[u]);
23+
}
24+
25+
if(low[v] == idx[v]) {
26+
int node;
27+
do {
28+
node = st.back();
29+
in[node] = false;
30+
st.pop_back();
31+
} while(node != v);
32+
++id;
33+
}
34+
}
35+
36+
int main() {
37+
scanf("%d", &t);
38+
tmp = 0;
39+
while(t-- != 0) {
40+
if(tmp == -1)
41+
break;
42+
43+
scanf("%d %d", &n, &m);
44+
g.clear();
45+
g.resize(n);
46+
for(int i = 0, a, b; i < m; ++i) {
47+
scanf("%d %d", &a, &b);
48+
--a, --b;
49+
g[a].push_back(b);
50+
}
51+
scanf("%d", &tmp);
52+
53+
memset(idx, -1, sizeof idx);
54+
memset(in, false, sizeof in);
55+
dfs = id = 0;
56+
for(int i = 0; i < n; ++i)
57+
if(idx[i] == -1)
58+
DFS(i);
59+
60+
printf("%d\n", id);
61+
}
62+
63+
return 0;
64+
}

ACM ICPC Live Archive/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- [2247 - Prime Digital Roots](https://icpcarchive.ecs.baylor.edu/index.php?option=onlinejudge&page=show_problem&problem=248)
44
- [3883 - Prime Gap](https://icpcarchive.ecs.baylor.edu/index.php?option=onlinejudge&page=show_problem&problem=1884)
5+
- [4262 - Road Networks](https://icpcarchive.ecs.baylor.edu/index.php?option=onlinejudge&page=show_problem&problem=2263)
56
- [4736 - Probability One](https://icpcarchive.ecs.baylor.edu/index.php?option=onlinejudge&page=show_problem&problem=2737)
67
- [4919 - Judges' Time Calculation](https://icpcarchive.ecs.baylor.edu/index.php?option=onlinejudge&page=show_problem&problem=2920)
78
- [4920 - Mad Scientist](https://icpcarchive.ecs.baylor.edu/index.php?option=onlinejudge&page=show_problem&problem=2921)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1e5 + 10;
6+
int n, a[N], sol[N];
7+
8+
int main() {
9+
cin >> n;
10+
for(int i = 0; i < n - 1; ++i)
11+
cin >> a[i];
12+
13+
sol[n - 1] = 9;
14+
for(int i = n - 2; i >= 0; --i) {
15+
if(a[i] == -1) {
16+
sol[i] = sol[i + 1] - 1;
17+
if(sol[i] < 0) {
18+
puts("-1");
19+
return 0;
20+
}
21+
} else if(a[i] == 0) {
22+
sol[i] = sol[i + 1];
23+
} else {
24+
sol[i] = sol[i + 1] + 1;
25+
if(sol[i] == 10) {
26+
for(int j = i; j < n; ++j) {
27+
--sol[j];
28+
if(sol[j] < ) {
29+
puts("-1");
30+
return 0;
31+
}
32+
33+
if(a[j] == -1) {
34+
if(j + 1 < n && sol[j] < sol[j + 1])
35+
break;
36+
} else if(a[j] == 0) {
37+
if(j + 1 < n && sol[j] == sol[j + 1])
38+
break;
39+
} else if(j + 1 < n) {
40+
if(sol[j] > sol[j + 1])
41+
break;
42+
}
43+
}
44+
} else
45+
sol[i] = 9;
46+
}
47+
}
48+
49+
if(sol[0] == 0) {
50+
puts("-1");
51+
return 0;
52+
}
53+
54+
for(int i = 0; i < n; ++i)
55+
cout << sol[i];
56+
puts("");
57+
58+
return 0;
59+
}

CS Academy/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Problems links
22

3+
- [18. Consecutive Digits Signs](https://csacademy.com/contest/round-18/task/consecutive-digit-signs/)
34
- [24. Vector Size](https://csacademy.com/contest/round-24/#task/vector-size)
45
- [24. Kth Special Number](https://csacademy.com/contest/round-24/#task/kth-special-number)
56
- [26. Limited Vocabulary](https://csacademy.com/contest/round-26/#task/limited-vocabulary)

CodeForces/118E. Bertown roads.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1e5 + 1;
6+
int n, m, idx[N], low[N], dfs, bridges;
7+
bool vis[N];
8+
set<pair<int, int> > st;
9+
vector<vector<int> > g;
10+
11+
void DFS(int v, int p) {
12+
idx[v] = low[v] = dfs++;
13+
14+
for(int i = 0, u; i < g[v].size(); ++i) {
15+
u = g[v][i];
16+
if(idx[u] == -1) {
17+
DFS(u, v);
18+
low[v] = min(low[v], low[u]);
19+
20+
if(low[u] > idx[v])
21+
++bridges;
22+
} else if(u != p)
23+
low[v] = min(low[v], idx[u]);
24+
}
25+
}
26+
27+
void print(int v, int p) {
28+
vis[v] = true;
29+
30+
for(int i = 0, u; i < g[v].size(); ++i) {
31+
u = g[v][i];
32+
33+
if(!vis[u]) {
34+
printf("%d %d\n", v + 1, u + 1);
35+
st.insert({v, u});
36+
st.insert({u, v});
37+
print(u, v);
38+
} else if(u != p && st.count({v, u}) == 0) {
39+
st.insert({v, u});
40+
st.insert({u, v});
41+
printf("%d %d\n", v + 1, u + 1);
42+
}
43+
}
44+
}
45+
46+
int main() {
47+
scanf("%d %d", &n, &m);
48+
g.resize(n);
49+
for(int i = 0, a, b; i < m; ++i) {
50+
scanf("%d %d", &a, &b);
51+
--a, --b;
52+
g[a].push_back(b);
53+
swap(a, b);
54+
g[a].push_back(b);
55+
}
56+
57+
memset(idx, -1, sizeof idx);
58+
DFS(0, -1);
59+
60+
if(bridges > 0) {
61+
puts("0");
62+
return 0;
63+
}
64+
65+
print(0, -1);
66+
67+
return 0;
68+
}

CodeForces/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- [116A. Tram](http://codeforces.com/problemset/problem/116/A)
4949
- [118A. String Task](http://codeforces.com/problemset/problem/118/A)
5050
- [118D. Caesar's Legions](http://codeforces.com/contest/118/problem/D)
51+
- [118E. Bertown roads](http://codeforces.com/problemset/problem/118/E)
5152
- [122A. Lucky Division](http://codeforces.com/problemset/problem/122/A)
5253
- [122C. Lucky Sum](http://codeforces.com/contest/122/problem/C)
5354
- [124B. Permutations](http://codeforces.com/problemset/problem/124/B)

SPOJ/EN - Entrapment.cpp

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 1e5 + 1;
6+
int t, n, m, idx[N], low[N], dfs, cost[2][N];
7+
bool vis[N];
8+
vector<vector<int> > g;
9+
vector<pair<int, int> > edges;
10+
set<int> ap;
11+
queue<int> q;
12+
13+
void DFS(int v, int par) {
14+
idx[v] = low[v] = dfs++;
15+
16+
int ch = 0;
17+
for(int i = 0, u; i < g[v].size(); ++i) {
18+
u = g[v][i];
19+
if(idx[u] == -1) {
20+
++ch;
21+
22+
DFS(u, v);
23+
low[v] = min(low[v], low[u]);
24+
25+
if(par == -1 && ch > 1)
26+
ap.insert(v);
27+
else if(par != -1 && low[u] >= idx[v])
28+
ap.insert(v);
29+
} else if(par != u)
30+
low[v] = min(low[v], idx[u]);
31+
}
32+
}
33+
34+
int BFS(int src, int idx) {
35+
memset(cost[idx], 0, sizeof cost[idx]);
36+
memset(vis, false, sizeof vis);
37+
while(!q.empty()) q.pop();
38+
39+
q.push(src);
40+
41+
while(!q.empty()) {
42+
int fr = q.front();
43+
q.pop();
44+
45+
if(vis[fr])
46+
continue;
47+
vis[fr] = true;
48+
49+
for(int i = 0; i < g[fr].size(); ++i) {
50+
cost[idx][g[fr][i]] = cost[idx][fr] + 1;
51+
q.push(g[fr][i]);
52+
}
53+
}
54+
55+
return 0;
56+
}
57+
58+
int main() {
59+
scanf("%d", &t);
60+
while(t-- != 0) {
61+
scanf("%d %d", &n, &m);
62+
63+
g.clear();
64+
g.resize(n);
65+
edges.clear();
66+
edges.resize(m);
67+
for(int i = 0, a, b; i < m; ++i) {
68+
scanf("%d %d", &a, &b);
69+
--a, --b;
70+
g[a].push_back(b);
71+
g[b].push_back(a);
72+
edges[i] = {a, b};
73+
}
74+
75+
memset(idx, -1, sizeof idx);
76+
ap.clear();
77+
dfs = 0;
78+
DFS(0, -1);
79+
80+
if(ap.empty()) {
81+
printf("%d\n", n);
82+
} else {
83+
BFS(0, 0);
84+
BFS(n - 1, 1);
85+
86+
int v1 = 1e9, v2 = -1e9, i, v;
87+
for(set<int>::iterator it = ap.begin(); it != ap.end(); ++it) {
88+
v = *it;
89+
90+
if(cost[0][v] < v1 && cost[1][v] > v2) {
91+
i = v;
92+
v1 = cost[0][v];
93+
v2 = cost[1][v];
94+
}
95+
}
96+
printf("%d\n", i + 1);
97+
}
98+
}
99+
100+
return 0;
101+
}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int t, n;
6+
vector<int> all;
7+
int const MAX = 20000001;
8+
bitset<MAX> prime;
9+
10+
void sieve_of_eratosthenes() {
11+
prime.set();
12+
13+
prime[0] = prime[1] = 0;
14+
for(int i = 4; i < MAX; i += 2)
15+
prime[i] = 0;
16+
17+
for(int i = 3; i * i < MAX; i += 2)
18+
if(prime[i])
19+
for(int j = i * i; j < MAX; j += (i << 1))
20+
prime[j] = 0;
21+
}
22+
23+
inline int toInt() {
24+
if(all[0] == 0)
25+
return 1;
26+
int ret = 0;
27+
for(int i = 0; i < all.size(); ++i) {
28+
ret *= 10;
29+
ret += all[i];
30+
}
31+
return ret;
32+
}
33+
34+
int main() {
35+
sieve_of_eratosthenes();
36+
37+
scanf("%d", &t);
38+
while(t-- != 0) {
39+
scanf("%d", &n);
40+
41+
all.clear();
42+
while(n != 0) {
43+
all.push_back(n % 10);
44+
n /= 10;
45+
}
46+
sort(all.begin(), all.end());
47+
48+
int res = 0;
49+
do {
50+
res += prime[toInt()];
51+
} while(next_permutation(all.begin(), all.end()));
52+
printf("%d\n", res);
53+
}
54+
55+
return 0;
56+
}

SPOJ/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [EDIST - Edit distance](http://www.spoj.com/problems/EDIST/)
2525
- [EDIT - Edit Distance Again](http://www.spoj.com/problems/EDIT/)
2626
- [ELIS - Easy Longest Increasing Subsequence](http://www.spoj.com/problems/ELIS/)
27+
- [EN - Entrapment](http://www.spoj.com/problems/EN/)
2728
- [ENIGMATH - PLAY WITH MATH](http://www.spoj.com/problems/ENIGMATH/)
2829
- [EPALIN - Extend to Palindrome](http://www.spoj.com/problems/EPALIN/)
2930
- [ETF - Euler Totient Function](http://www.spoj.com/problems/ETF/)
@@ -40,6 +41,7 @@
4041
- [NDIV - n-divisors](http://www.spoj.com/problems/NDIV/)
4142
- [NSTEPS - Number Steps](http://www.spoj.com/problems/NSTEPS/)
4243
- [OPCPRIME - Prime Factorization](http://www.spoj.com/problems/OPCPRIME/)
44+
- [PRIMPERM - Prime Permutations](http://www.spoj.com/problems/PRIMPERM/)
4345
- [PT07Y - Is it a tree](http://www.spoj.com/problems/PT07Y/)
4446
- [RMQSQ - Range Minimum Query](http://www.spoj.com/problems/RMQSQ/)
4547
- [ROADS - Roads](http://www.spoj.com/problems/ROADS/)

0 commit comments

Comments
 (0)