#include <bits/stdc++.h>
using namespace std;
const int N = 30010;
typedef long long ll;
ll arr[N];
const int coin[] = {1, 5, 10, 25, 50};
int main() {
int x;
arr[0] = 1;
for (auto x : coin) {
for (int i = 0; i <= 30000; ++i) {
arr[i + x] += arr[i];
}
}
while (cin >> x) {
ll ans = arr[x];
if (ans != 1) {
printf("There are %lld ways to produce %d cents change.\n", ans, x);
} else {
printf("There is only 1 way to produce %d cents change.\n", x);
}
}
}
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
int n, m, id;
struct cube {
int x, y, h;
bool operator>(const cube& b) const {
return (x > b.x && y > b.y) || (x > b.y && y > b.x);
}
} c[N];
int dp[N];
int get(int id) {
int& ans = dp[id];
if (ans > 0) return ans;
ans = c[id].h;
for (int i = 1; i <= m; ++i) {
if (c[id] > c[i]) {
ans = max(ans, get(i) + c[id].h);
}
}
return ans;
}
int main() {
int tc = 0;
while (cin >> n && n) {
memset(dp, 0, sizeof(dp));
m = n * 3;
for (int i = 1; i <= m; i += 3) {
int x, y, h;
cin >> x >> y >> h;
c[i] = {x, y, h};
c[i + 1] = {h, x, y};
c[i + 2] = {y, h, x};
}
int ans = 0;
for (int i = 1; i <= m; i++) {
ans = max(get(i), ans);
}
printf("Case %d: maximum height = %d\n", ++tc, ans);
}
}
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
const int N = 100000 + 5;
vector<pi> adj[N];
int s[N], t[N];
int main() {
int n, m, c;
queue<int> q;
cin >> n >> m >> c;
for (int i = 1; i <= n; ++i) cin >> s[i];
for (int i = 1; i <= c; ++i) {
int u, v, w;
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
t[v]++;
}
// 拓扑排序
for (int i = 1; i <= n; ++i)
if (!t[i]) q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto [v, w] : adj[u]) {
s[v] = max(s[v], s[u] + w);
t[v]--;
if (!t[v]) q.push(v);
}
}
for (int i = 1; i <= n; ++i) cout << s[i] << '\n';
}
// 拓扑排序可以排非连通图?!
#include <bits/stdc++.h>
using namespace std;
const int N = 300'000 + 10;
// dp[i][alpha] 表示在字符串前i个字符组成的图中
// alpha这个字符的最大数量
int dp[N][26];
char s[N];
int indeg[N];
vector<int> adj[N];
int m, n;
int main() {
cin >> n >> m >> s + 1;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
indeg[v]++;
adj[u].push_back(v);
}
queue<int> q;
int cnt = 0;
for (int i = 1; i <= n; ++i) {
if (indeg[i] == 0) {
q.push(i);
dp[i][s[i] - 'a'] = 1;
}
}
while (q.size()) {
int u = q.front();
q.pop();
cnt++;
for (auto v : adj[u]) {
for (int j = 0; j < 26; ++j) {
dp[v][j] = max(dp[v][j], dp[u][j] + (s[v] - 'a' == j));
}
indeg[v]--;
if (indeg[v] == 0) q.push(v);
}
}
int ans = -1;
// 因为这道题特殊所以找答案需要找遍dp数组
if (cnt == n) {
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < 26; ++j) {
ans = max(ans, dp[i][j]);
}
}
}
cout << ans << endl;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
const int A = 30;
int n;
vector<int> adj[A];
int indeg[A];
string name[N];
int enc(int a) {
if (a == ' ')
return 0;
else
return a - 'a' + 1;
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> name[i];
name[i] += ' ';
}
for (int i = 0; i < n - 1; ++i) {
int pos = 0;
while (name[i][pos] == name[i + 1][pos]) pos++;
int u = enc(name[i][pos]), v = enc(name[i + 1][pos]);
if (v == 0) { // xy > x
cout << "Impossible\n";
return 0;
}
adj[u].push_back(v);
indeg[v]++;
}
queue<int> q;
int cnt = 0;
for (int i = 0; i <= 26; ++i) {
if (!indeg[i]) q.push(i);
}
string ans;
while (q.size()) {
int u = q.front();
q.pop();
if (u) {
ans += char(u + 'a' - 1);
cnt++;
}
// cout << ans << endl;
for (int v : adj[u]) {
indeg[v]--;
if (!indeg[v]) q.push(v);
}
}
if (cnt == 26)
cout << ans << endl;
else
cout << "Impossible\n";
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10, M = 5e4 + 10;
vector<int> adj[N];
int dfn[N], low[N], deg[N], sz[N];
int scc[N], sccidx; // 节点i所在SCC的编号
bool instack[N];
int n, m;
int dfncnt;
stack<int> s;
void tarjan(int u) {
dfn[u] = low[u] = ++dfncnt;
s.push(u);
instack[u] = true;
for (auto v : adj[u]) {
if (!dfn[v]) { // v未被搜索过
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (instack[v]) // v在栈中
low[u] = min(low[u], dfn[v]);
}
int k = -1; // 不必担心,k在使用前一定是赋值了的
if (low[u] == dfn[u]) {
++sccidx;
do {
k = s.top();
s.pop();
instack[k] = false;
scc[k] = sccidx;
sz[sccidx]++;
} while (u != k);
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
for (int i = 1; i <= n; ++i) {
if (!dfn[i]) tarjan(i);
}
for (int u = 1; u <= n; ++u) {
for (auto v : adj[u]) {
if (scc[u] != scc[v]) {
deg[scc[u]]++; // 遍历点并记录出度
}
}
}
bool id = 0;
for (int i = 1; i <= sccidx; ++i) {
if (!deg[i]) {
if (id) {
// 两次出现出度为0的点,直接输出0
puts("0");
return 0;
}
id = i;
}
}
cout << sz[id] << endl;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
vector<int> adj[N], sccadj[N];
int dfn[N], low[N], deg[N], sz[N], scc[N], memo[N], dfncnt, scccnt, ans;
bool instk[N];
int n, m;
stack<int> s;
void tarjan(int u) {
dfn[u] = low[u] = ++dfncnt;
s.push(u);
instk[u] = true;
for (auto v : adj[u]) {
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (instk[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u]) {
scccnt++;
int k = -1;
do {
k = s.top();
s.pop();
sz[scccnt]++;
instk[k] = false;
scc[k] = scccnt;
} while (k != u);
}
}
int dfs(int u) {
if (memo[u]) return memo[u];
for (const int v : sccadj[u]) memo[u] = max(memo[u], dfs(v));
return memo[u] = memo[u] + sz[u];
}
int main() {
int tc;
cin >> tc;
while (tc--) {
cin >> n >> m;
for (int *arr : {dfn, low, deg, sz, scc, memo}) {
memset(arr, 0, sizeof(dfn));
}
memset(instk, 0, sizeof instk);
while (s.size()) s.pop();
dfncnt = scccnt = ans = 0;
for (auto &v : {adj, sccadj})
for (int i = 1; i <= n; ++i) v[i].clear();
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i);
for (int u = 1; u <= n; ++u) {
for (int v : adj[u]) {
if (scc[u] != scc[v]) {
sccadj[scc[u]].push_back(scc[v]);
}
}
}
for (int i = 1; i <= scccnt; ++i) {
ans = max(ans, dfs(i));
}
cout << ans << endl;
}
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m;
vector<int> adj[N];
vector<int> vec[N];
int indeg[N];
// 考虑前x条信息时的图
void build(int x) {
for (int i = 0; i <= n; ++i) adj[i].clear();
memset(indeg, 0, sizeof indeg);
for (int i = 1; i <= x; ++i) {
for (int j = 0; j < vec[i].size() - 1; ++j) {
int u = vec[i][j], v = vec[i][j + 1];
adj[u].push_back(v);
indeg[v]++;
}
}
}
bool check(int x) {
build(x);
queue<int> q;
int cnt = 0;
for (int i = 1; i <= n; ++i)
if (!indeg[i]) q.push(i);
while (q.size()) {
int u = q.front();
q.pop();
cnt++;
for (int v : adj[u]) {
indeg[v]--;
if (!indeg[v]) q.push(v);
}
}
return cnt == n;
}
void get_ans(int x) {
build(x);
priority_queue<int, vector<int>, greater<int>> q;
for (int i = 1; i <= n; ++i)
if (!indeg[i]) q.push(i);
while (q.size()) {
int u = q.top();
q.pop();
cout << u << ' ';
for (int v : adj[u]) {
indeg[v]--;
if (!indeg[v]) q.push(v);
}
}
cout << endl;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int si;
cin >> si;
for (int j = 1; j <= si; ++j) {
int x;
cin >> x;
vec[i].push_back(x);
}
}
int l = 1, r = m, ans;
while (l <= r) {
int mid = (l + r) / 2;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
get_ans(ans);
}
#include <bits/stdc++.h>
using namespace std;
const int N = 2e4 + 10;
int dfn[N], low[N], deg[N], sz[N], scc[N], outdeg[N], indeg[N], dfncnt, scccnt;
bool instk[N];
vector<int> adj[N];
stack<int> s;
int n, m;
void tarjan(int u) {
dfn[u] = low[u] = ++dfncnt;
s.push(u);
instk[u] = true;
for (auto v : adj[u]) {
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (instk[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u]) {
scccnt++;
int k = -1;
do {
k = s.top();
s.pop();
sz[scccnt]++;
instk[k] = false;
scc[k] = scccnt;
} while (k != u);
}
}
int main() {
int tc;
cin >> tc;
while (tc--) {
cin >> n >> m;
for (int *arr : {dfn, low, deg, sz, scc, outdeg, indeg})
memset(arr, 0, sizeof dfn);
memset(instk, 0, sizeof instk);
while (s.size()) s.pop();
dfncnt = scccnt = 0;
for (int i = 1; i <= n; ++i) adj[i].clear();
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i);
for (int u = 1; u <= n; ++u)
for (int v : adj[u])
if (scc[u] != scc[v]) ++outdeg[scc[u]], ++indeg[scc[v]];
if (scccnt == 1) {
cout << 0 << endl;
} else {
int cntin = 0, cntout = 0;
for (int i = 1; i <= scccnt; ++i) {
if (!indeg[i]) ++cntin;
if (!outdeg[i]) ++cntout;
}
cout << max(cntin, cntout) << endl;
}
}
}
#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int dfn[N], low[N], deg[N], sz[N], scc[N], outdeg[N], indeg[N], dfncnt, scccnt;
bool instk[N];
vector<int> adj[N];
int w[N], a[N], d[N];
int W[N], V[N], dp[N][N];
stack<int> s;
int n, m;
void tarjan(int u) {
dfn[u] = low[u] = ++dfncnt;
s.push(u);
instk[u] = true;
for (auto v : adj[u]) {
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (instk[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u]) {
scccnt++;
int k = -1;
do {
k = s.top();
s.pop();
sz[scccnt]++;
instk[k] = false;
scc[k] = scccnt;
W[scccnt] += w[k];
V[scccnt] += a[k];
} while (k != u);
}
}
void solve(int u) {
for (int i = W[u]; i <= m; ++i) {
dp[u][i] = V[u];
}
for (auto v : adj[u]) {
solve(v);
int k = m - W[u];
for (int i = k; i >= 0; --i) {
for (int j = 0; j <= i; ++j) {
dp[u][i + W[u]] = max(dp[u][i + W[u]], dp[v][j] + dp[u][i + W[u] - j]);
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> w[i];
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) {
cin >> d[i];
if (d[i]) adj[d[i]].push_back(i);
}
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i);
for (int i = 0; i <= n; ++i) adj[i].clear();
for (int i = 1; i <= n; ++i) {
if (scc[d[i]] != scc[i]) {
adj[scc[d[i]]].push_back(scc[i]);
indeg[scc[i]]++;
}
}
for (int i = 1; i <= scccnt; ++i) {
if (!indeg[i]) adj[0].push_back(i);
}
solve(0);
cout << dp[0][m] << endl;
}
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200100, maxm = 400100;
int n, m, head[maxn], len, age[maxn];
bool vis[maxn];
queue<int> q;
struct edge {
int to, next;
} edges[maxm];
void add_edge(int u, int v) {
edges[++len].to = v;
edges[len].next = head[u];
head[u] = len;
}
void add_sub(int u, int a, int v, int b) {
add_edge(u + n * (a ^ 1), v + n * b);
add_edge(v + n * (b ^ 1), u + n * a);
}
bool dfs(int u) {
if (vis[u + n * (u <= n ? 1 : -1)]) {
return 0;
}
if (vis[u]) {
return 1;
}
vis[u] = 1;
q.push(u);
for (int i = head[u]; i; i = edges[i].next) {
int v = edges[i].to;
if (!dfs(v)) {
return 0;
}
}
return 1;
}
bool twoSAT() {
for (int i = 1; i <= n; ++i) {
if (!vis[i] && !vis[i + n]) {
while (q.size()) {
q.pop();
}
if (!dfs(i)) {
while (q.size()) {
vis[q.front()] = 0;
q.pop();
}
if (!dfs(i + n)) {
return 0;
}
}
}
}
return 1;
}
int main() {
while (scanf("%d%d", &n, &m) == 2 && n) {
len = 0;
memset(vis, 0, sizeof(vis));
memset(head, 0, sizeof(head));
int sum = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d", &age[i]);
sum += age[i];
}
while (m--) {
int u, v;
scanf("%d%d", &u, &v);
if (u == v) {
continue;
}
add_sub(u, 0, v, 0);
if ((age[u] * n >= sum) == (age[v] * n >= sum)) {
add_sub(u, 1, v, 1);
}
}
if (!twoSAT()) {
puts("No solution.");
continue;
}
for (int i = 1; i <= n; ++i) {
if (vis[i + n]) {
puts("C");
} else if (age[i] * n >= sum) {
puts("A");
} else {
puts("B");
}
}
}
return 0;
}