-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1337.cpp
More file actions
124 lines (98 loc) · 2.37 KB
/
1337.cpp
File metadata and controls
124 lines (98 loc) · 2.37 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// https://acm.timus.ru/problem.aspx?space=1&num=1337
// time simulation + dependency graph (DAG) + prerequisites + reverse adjacency updates + greedy earliest-available scheduling
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<char> vc;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define rep(i,a,b) for(int i=(a); i<(b); i++)
#define pb push_back
#define fi first
#define se second
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, l;
if(!(cin >> n >> l)) return 0;
vi day_doc(l + 1, 0);
for(int i = 1; i <= n; i++)
{
int a;
cin >> a;
if(1 <= a && a <= l) day_doc[a] = i;
}
vector< bitset<101> > need(n + 1);
vvi rev(n + 1);
vi cnt(n + 1, 0);
for(int i = 1; i <= n; i++)
{
int x;
while(cin >> x)
{
if(x == 0) break;
if(1 <= x && x <= n && !need[i][x])
{
need[i][x] = 1;
cnt[i]++;
rev[x].pb(i);
}
}
}
int k;
cin >> k;
vi have;
while(true)
{
int x;
cin >> x;
if(x == 0) break;
have.pb(x);
}
vc req(n + 1, 0);
int rem = 0;
while(true)
{
int x;
cin >> x;
if(x == 0) break;
if(1 <= x && x <= n && !req[x]) { req[x] = 1; rem++; }
}
vc got(n + 1, 0);
auto obtain = [&](int x)
{
if(x < 1 || x > n) return;
if(got[x]) return;
got[x] = 1;
if(req[x]) { req[x] = 0; rem--; }
for(int y : rev[x]) if(need[y][x]) { need[y][x] = 0; cnt[y]--; }
need[x].reset();
cnt[x] = 0;
};
for(int x : have) obtain(x);
if(rem == 0) { cout << 0 << '\n' << '\n'; return 0; }
vi ans;
int t = 0;
int last = -1;
while(rem > 0)
{
if(t - last > l) { cout << "No Solution"; return 0; }
int weekday = ((k - 1 + t) % l) + 1;
int doc = day_doc[weekday];
if(doc != 0 && !got[doc] && cnt[doc] == 0)
{
obtain(doc);
ans.pb(doc);
last = t;
}
if(rem == 0) break;
t++;
}
cout << t << '\n';
for(int i = 0; i < sz(ans); i++) { if(i) cout << ' '; cout << ans[i]; }
cout << '\n';
return 0;
}