include iostream include vector include algorithm include cstdio inclu

  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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
using namespace std;
long long inf = 1000000000;
struct node {
int to;
long long cap;
long long fl;
int inv;
node(int T = 0, int C = 0, int F = 0, int I = 0): to(T), cap(C), fl(F), inv(I){};
};
struct id {
string name, spec;
int rang;
id(string N = "", string S = "", int R = -1): name(N), spec(S), rang(R){};
};
vector<vector<node> > g;
vector<int> d;
vector<int> ptr;
bool bfs(int sz, int s, int t) {
queue<int> q;
d.assign(sz, -1);
d[s] = 0;
q.push(s);
while (!q.empty() && d[t] == -1) {
int v = q.front();
q.pop();
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i].to;
int cur_fl = g[v][i].fl;
int cur_cap = g[v][i].cap;
if (d[to] == -1 && cur_fl < cur_cap) {
q.push(to);
d[to] = d[v] + 1;
}
}
}
return (d[t] != -1);
}
long long dfs(int v, int t, long long flow) {
if (!flow)
return 0;
if (v == t)
return flow;
for (; ptr[v] < g[v].size(); ++ptr[v]) {
node cur = g[v][ptr[v]];
int to = cur.to;
int inv = cur.inv;
if (d[to] != d[v] + 1)
continue;
if (cur.cap - cur.fl <= 0)
continue;
long long pushed = dfs(to, t, min(flow, cur.cap - cur.fl));
if (pushed) {
g[v][ptr[v]].fl += pushed;
g[to][inv].fl -= pushed;
return pushed;
}
}
return 0;
}
long long dinic(int s, int t) {
long long flow = 0;
int n = g.size();
while (true){
if (!bfs(n, s, t)) break;
ptr.assign(n, 0);
while (int pushed = dfs(0, t, inf)){
flow += pushed;
}
}
return flow;
}
int main() {
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
//inf *= inf;
int N, r;
string nm, str;
long long answer = 0;
vector<id> fp, sp;
fp.reserve(N);
sp.reserve(N);
cin >> N;
for (int i = 0; i < N; i++) {
cin >> nm >> str >> r;
if (r % 4 < 2) {
fp.push_back(id(nm, str, r));
}
else {
sp.push_back(id(nm, str, r));
}
}
int n = fp.size(), m = sp.size();
int s = 0;
int t = m + n + 1;
g.resize(n + m + 2);
node cur, inv_cur;
for (int i = 1; i <= n; i++) {
cur = node(i, 1, 0, g[i].size());
g[s].push_back(cur);
inv_cur = node(s, 0, 0, g[s].size() - 1);
g[i].push_back(inv_cur);
}
for (int i = n + 1; i <= n + m; i++) {
cur = node(t, 1, 0, g[t].size());
g[i].push_back(cur);
inv_cur = node(i, 0, 0, g[i].size() - 1);
g[t].push_back(inv_cur);
}
for (int i = 1; i <= n; i++){
for (int j = n + 1; j <= n + m; j++) {
int ind1 = i - 1;
int ind2 = j - n - 1;
bool ch1 = (fp[ind1].spec != sp[ind2].spec) && (abs(fp[ind1].rang - sp[ind2].rang) == 2);
bool ch2 = (fp[ind1].spec == sp[ind2].spec) && (fp[ind1].spec == "anything") &&
(abs(fp[ind1].rang - sp[ind2].rang) == 2);
if (ch1 || ch2) {
cout << "j = " << j << endl;
cur = node(j, 1, 0, g[j].size());
g[i].push_back(cur);
inv_cur = node(i, 0, 0, g[i].size() - 1);
g[j].push_back(inv_cur);
}
}
}
cout << fp.size() << " " << sp.size() << endl;
cout << "lol" << endl;
answer += dinic(s, t);
cout << answer << endl;
vector<pair<id, id> > ans;
for (int i = 1; i <= n; i++) {
int ind1 = i - 1;
for (int j = 0; j < g[i].size(); j++) {
if (!g[i][j].fl)
continue;
int to = g[i][j].to;
cout << "to = " << to << endl;
int ind2 = to - n - 1;
cout << ind1 << " " << ind2 << endl;
ans.push_back(make_pair(fp[ind1], sp[ind2]));
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
// if (ans[i].first.spec == "testdata")
// swap(ans[i].first, ans[i].second);
//else if (ans[i].second.spec == "statements")
// swap(ans[i].first, ans[i].second);
cout << ans[i].first.name << " " << ans[i].second.name << endl;
}
return 0;
}