forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigicomp2.cpp
89 lines (76 loc) · 1.75 KB
/
digicomp2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
queue<ll> q;
vector<bool> isleft;
vector<ll> l;
vector<ll> r;
vector<ll> deg;
vector<ll> balls;
void toposort(bool skiproot) {
while(!q.empty()) {
ll curr = q.front();
q.pop();
ll addfirst = (balls[curr]+1)/2;
ll addsecond = (balls[curr])/2;
if(isleft[curr]) {
balls[l[curr]] += addfirst;
balls[r[curr]] += addsecond;
}
else {
balls[r[curr]] += addfirst;
balls[l[curr]] += addsecond;
}
if(!(skiproot && l[curr] == 0)) {
deg[l[curr]]--;
if(deg[l[curr]] == 0) {
q.push(l[curr]);
}
}
if(!(skiproot && r[curr] == 0)) {
deg[r[curr]]--;
if(deg[r[curr]] == 0) {
q.push(r[curr]);
}
}
}
}
int main() {
ll m, switches;
cin >> m >> switches;
isleft.resize(switches+1);
l.resize(switches+1);
r.resize(switches+1);
deg.resize(switches+1,0);
balls.resize(switches+1,0);
for(ll i = 1; i <= switches; i++) {
char c; cin >> c;
if(c == 'L') isleft[i] = true;
else isleft[i] = false;
cin >> l[i] >> r[i];
deg[l[i]]++;
deg[r[i]]++;
}
deg[0] = (ll)1 << 60;
for(int i = 2; i <= switches; i++) {
if(deg[i] == 0) {
q.push(i);
}
}
toposort(true);
q.push(1);
balls[1] = m;
toposort(false);
for(ll i = 1; i <= switches; i++) {
if(balls[i]%2 == 1) {
isleft[i] = !isleft[i];
}
if(isleft[i]) {
cout << "L";
}
else {
cout << "R";
}
}
cout << endl;
}