-
Notifications
You must be signed in to change notification settings - Fork 11
/
kattis_favourable.cpp
67 lines (59 loc) · 1.86 KB
/
kattis_favourable.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
/**Kattis - favourable
* Another simple dp counting problem. We just use a map because its more convenient, we can use other data
* structures if we felt like it.
*
* Time: O(401), Space: O(401)
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
int num_tc;
int n;
map<int, vector<int>> adjlist;
map<int, int> type; // 0 for internal, 1 for favourable, 2 for unfavourable
ll memo[401];
ll dp(int page){
if (memo[page] != -1) return memo[page];
if (type.find(page) != type.end()){
if (type[page] == 1) return 1;
else return 0;
}
return memo[page] = dp(adjlist[page][0]) + dp(adjlist[page][1]) + dp(adjlist[page][2]);
}
int main() {
fast_cin();
cin >> num_tc;
for (int tc = 0; tc < num_tc; tc++) {
cin >> n;
adjlist.clear();
type.clear();
memset(memo, -1, sizeof memo);
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
for (int i = 0; i < n; i++) {
string s, line; // split line by spaces
getline(cin, line);
vector<string> v;
istringstream ss(line);
while (ss >> s) {
v.emplace_back(s);
}
if (v.size() == 2) {
type[stoi(v[0])] = (v[1] == "favourably") ? 1 : 2;
} else {
adjlist[stoi(v[0])].emplace_back(stoi(v[1]));
adjlist[stoi(v[0])].emplace_back(stoi(v[2]));
adjlist[stoi(v[0])].emplace_back(stoi(v[3]));
}
}
cout << dp(1) << endl;
}
return 0;
}