Online Judge

writeup


  • Home

  • UVa

  • Codeforces

115A - Party

| In codeforces |
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, p, groupMax;
vector<int> manager;
vector<int> superior[2001];
void DFS(int m, int group) {
groupMax = max(group, groupMax);
for (int i = 0; i < superior[m].size(); ++i) {
DFS(superior[m][i], group + 1);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> p;
if (p == -1) {
manager.push_back(i);
}
else {
superior[p].push_back(i);
}
}
groupMax = 1;
for (int i = 0; i < manager.size(); ++i) {
DFS(manager[i], 1);
}
cout << groupMax << endl;
return 0;
}

CodeForces 20C - Dijkstra

| In codeforces |
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
#include <climits>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <functional>
using namespace std;
typedef pair<long long, int> Pair;
int n, m, a;
struct to { int b, w; } To;
struct dis {
int parent;
long long path;
} Dis[100001];
vector<to> edge[100001];
inline void Dijkstra() {
Pair p;
priority_queue<Pair, vector<Pair>, greater<Pair>> Queue;
for (int i = 1; i <= n; ++i) {
Dis[i].parent = i;
Dis[i].path = LLONG_MAX;
}
Dis[1].path = 0;
Queue.push(Pair(0, 1));
while (!Queue.empty()) {
p = Queue.top();
Queue.pop();
if (Dis[p.second].path < p.first) {
continue;
}
for (int i = 0; i < edge[p.second].size(); ++i) {
To = edge[p.second][i];
if (Dis[To.b].path > p.first + To.w) {
Dis[To.b].parent = p.second;
Dis[To.b].path = p.first + To.w;
Queue.push(Pair(Dis[To.b].path, To.b));
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
bool isPath = true;
vector<int> path;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> a >> To.b >> To.w;
edge[a].push_back(To);
swap(a, To.b);
edge[a].push_back(To);
}
Dijkstra();
path.push_back(n);
while (n != 1) {
if (Dis[n].parent == n) {
isPath = false;
break;
}
n = Dis[n].parent;
path.push_back(n);
}
if(isPath) {
cout << 1;
for (int i = path.size() - 2; i >= 0; --i) {
cout << ' ' << path[i];
}
cout << '\n';
}
else { cout << "-1\n"; }
return 0;
}

489D - Unbearable Controversy of Being

| In codeforces |
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
#include <iostream>
#include <vector>
using namespace std;
int n, m, a, b, rhombi = 0;
int damm[3001];
bool isVisit[3001];
vector<int> road[3001];
inline void DFS(int i, int step) {
int r;
for (int j = 0; j < road[i].size(); ++j) {
r = road[i][j];
if (!isVisit[r]) {
isVisit[r] = true;
if (step < 2) { DFS(r, step + 1); }
else { ++damm[r]; }
isVisit[r] = false;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> a >> b;
road[a].push_back(b);
}
for (int i = 1; i <= n; ++i) {
isVisit[i] = true;
DFS(i, 1);
for (int i = 1; i <= n; ++i) {
if (damm[i] >= 2) {
rhombi += damm[i] * (damm[i] - 1) / 2;
}
damm[i] = 0;
}
isVisit[i] = false;
}
cout << rhombi << '\n';
return 0;
}

329B - Biridian Forest

| In codeforces |
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int r, c, node = 0, moves, battle = 0;
int dir[4][2] = { { 0, -1 },{ -1, 0 },{ 0, 1 },{ 1, 0 } };
char forest[1000][1000];
struct pos { int x, y; }E, P, p;
struct breeder { int X, step; }b;
vector<struct breeder> B;
inline void BFS(struct pos &E) {
int size, step = 0;
char f;
queue<struct pos> Queue;
Queue.push(E);
while (!Queue.empty()) {
if (node == 0) { break; }
size = Queue.size();
++step;
for (int i = 0; i < size; ++i) {
P = Queue.front();
Queue.pop();
for (int j = 0; j < 4; ++j) {
p.x = P.x + dir[j][0];
p.y = P.y + dir[j][1];
f = forest[p.x][p.y];
if (0 <= p.x && p.x < r &&
0 <= p.y && p.y < c &&
f != 'T') {
if (f == 'S') {
moves = step;
--node;
}
else if (f != '0') {
b.X = f - '0';
b.step = step;
B.push_back(b);
--node;
}
forest[p.x][p.y] = 'T';
Queue.push(p);
}
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> r >> c;
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
cin >> forest[i][j];
switch (forest[i][j]) {
case '0':
case 'T':
continue;
case 'E':
E.x = i;
E.y = j;
forest[i][j] = 'T';
break;
default:
++node;
break;
}
}
}
BFS(E);
for (int i = 0; i < B.size(); ++i) {
if (moves >= B[i].step) {
battle += B[i].X;
}
}
cout << battle << '\n';
return 0;
}

280 - Vertex

| In uva |
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
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int n, i, j, integer, vertex, counter;
bool accessible[101];
vector<int> edge[101];
void DFS(int v) {
int size = edge[v].size(), end;
for (int k = 0; k < size; ++k) {
end = edge[v][k];
if (accessible[end] == false) {
accessible[end] = true;
--counter;
DFS(end);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> n && n != 0) {
for (int k = 1; k <= n; ++k) {
edge[k].clear();
}
while (cin >> i && i != 0) {
while (cin >> j && j != 0) {
edge[i].push_back(j);
}
}
cin >> integer;
for (int k = 0; k < integer; ++k) {
cin >> vertex;
counter = n;
memset(accessible, false, sizeof(bool) * (n + 1));
DFS(vertex);
cout << counter;
for (int l = 1; l <= n; ++l) {
if (accessible[l] == false) {
cout << ' ' << l;
}
}
cout << '\n';
}
}
return 0;
}

572 - Oil Deposits

| In uva |
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
#include <iostream>
using namespace std;
int m, n, deposit;
char grid[100][100];
void DFS(int x, int y) {
int xi, yj;
grid[x][y] = '*';
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
xi = x + i;
yj = y + j;
if (0 <= xi && xi < m &&
0 <= yj && yj < n &&
grid[xi][yj] == '@') {
DFS(xi, yj);
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> m >> n && m != 0) {
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
cin >> grid[i][j];
}
}
deposit = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '@') {
++deposit;
DFS(i, j);
}
}
}
}
cout << deposit << '\n';
return 0;
}

Shoulder Hu

6 posts
2 categories
GitHub E-Mail
© 2017 Shoulder Hu