Skip to content

Commit 09ec46b

Browse files
committed
[kattis] Add C++ solution for Reactivity Series.
1 parent 776ee23 commit 09ec46b

File tree

6 files changed

+50
-0
lines changed

6 files changed

+50
-0
lines changed

kattis/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ There are solutions for the following
229229
1. [Racing Around the Alphabet](racingalphabet.cc) ([problem site](https://open.kattis.com/problems/racingalphabet))
230230
1. [Ragged Right](raggedright.cc) ([problem site](https://open.kattis.com/problems/raggedright))
231231
1. [Railroad](railroad2.cc) ([problem site](https://open.kattis.com/problems/railroad2))
232+
1. [Reactivity Series](reactivity.cc) ([problem site](https://open.kattis.com/problems/reactivity))
232233
1. [Working at the Restaurant](restaurant.cc) ([problem site](https://open.kattis.com/problems/restaurant))
233234
1. [Reversed Binary Numbers](reversebinary.py) ([problem site](https://open.kattis.com/problems/reversebinary))
234235
1. [Reverse Rot](reverserot.cc) ([problem site](https://open.kattis.com/problems/reverserot))

kattis/reactivity-1.ans

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0 1

kattis/reactivity-1.in

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2 1
2+
0 1

kattis/reactivity-2.ans

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
back to the lab

kattis/reactivity-2.in

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
4 4
2+
0 1
3+
0 2
4+
2 3
5+
1 3

kattis/reactivity.cc

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// https://open.kattis.com/problems/reactivity
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
using vi = vector<int>;
6+
using vvi = vector<vi>;
7+
using qi = queue<int>;
8+
9+
int main() {
10+
int n, m, u, v;
11+
cin >> n >> m;
12+
vvi g(n);
13+
vi a(n), b;
14+
for (int i = 0; i < m; i++) {
15+
cin >> u >> v;
16+
g[u].push_back(v);
17+
a[v]++;
18+
}
19+
qi q;
20+
for (int i = 0; i < n; i++)
21+
if (!a[i]) q.push(i);
22+
bool ok = 1;
23+
while (!q.empty()) {
24+
if (q.size() > 1) {
25+
ok = 0;
26+
break;
27+
}
28+
u = q.front();
29+
q.pop();
30+
b.push_back(u);
31+
for (int v : g[u]) {
32+
a[v]--;
33+
if (!a[v]) q.push(v);
34+
}
35+
}
36+
if (ok)
37+
for (int i = 0; i < n; i++) cout << b[i] << " \n"[i == n - 1];
38+
else
39+
cout << "back to the lab\n";
40+
}

0 commit comments

Comments
 (0)