#include <cassert>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <vector>
const int maxn = 10010;
using std::vector;
vector <int> g[maxn];
int n1, n2, m, test = 0;
int c[maxn], r[maxn], l1[maxn], l2[maxn], ok[maxn], step = 100;
int q[maxn], qs, qz;
bool dfs( int u )
{
c[u] = step;
for (int i = 0; i < (int)g[u].size(); i++)
{
int to = g[u][i];
if (l2[to] != l1[u] + 1)
continue;
if (r[to] == -1 || (c[r[to]] != step && dfs(r[to]) && l1[r[to]] == l1[u] + 2))
{
r[to] = u;
return true;
}
}
return false;
}
bool dfs2( int u )
{
c[u] = step;
for (int i = 0; i < (int)g[u].size(); i++)
{
int to = g[u][i];
if (r[to] != -1 && c[r[to]] != step)
dfs2(r[to]);
}
return false;
}
void bfs()
{
qs = qz = 0;
memset(c, 0, sizeof(c[0]) * n1);
memset(l1, -1, sizeof(l1[0]) * n1);
memset(l2, -1, sizeof(l2[0]) * n2);
for (int i = 0; i < n1; i++)
if (!ok[i])
q[qs + qz++] = i, l1[i] = 1, c[i] = 1;
while (qz > 0)
{
int u = q[qs++]; qz--;
for (int i = 0; i < (int)g[u].size(); i++)
if (l2[g[u][i]] == -1)
{
l2[g[u][i]] = l1[u] + 1;
if (r[g[u][i]] != -1 && c[r[g[u][i]]] == 0)
{
c[r[g[u][i]]] = 1;
l1[r[g[u][i]]] = l1[u] + 2;
q[qs + qz++] = r[g[u][i]];
}
}
}
}
int main()
{
assert(freopen("sociology.in", "r", stdin));
assert(freopen("sociology.out", "w", stdout));
memset(c, 0, sizeof(c));
while (scanf("%d%d", &n2, &n1) == 2)
{
++test;
assert(scanf("%d", &m) == 1);
for (int i = 0; i < m; i++)
{
int a, b;
assert(scanf("%d%d", &a, &b) == 2);
g[b - 1].push_back(a - 1);
}
memset(r, -1, sizeof(r[0]) * n2);
memset(ok, 0, sizeof(ok[0]) * n1);
bool good = true, ans = true;
while (good)
{
good = false;
bfs();
// memset(c, 0, sizeof(c[0]) * n1);
step++;
for (int i = 0; i < n1; i++)
if (l1[i] == 1 && c[i] != step && dfs(i))
ok[i] = true, good = true;
}
for (int i = 0; i < n1; i++)
if (!ok[i])
{
ans = false;
step++;
dfs2(i);
break;
}
if (ans)
printf("Case #%d: There is no excessive subset of managers.\n", test);
else
{
vector <int> res;
for (int i = 0; i < n1; i++)
if (c[i] == step)
res.push_back(i + 1);
if (res.size() == 1)
printf("Case #%d: Manager %d forms an excessive subset.\n", test, res[0]);
else
{
printf("Case #%d: Managers ", test);
for (int i = 0; i < (int)res.size(); i++)
{
if (i == (int)res.size() - 1)
printf(" and ");
else if (i > 0)
printf(", ");
printf("%d", res[i]);
}
printf(" form an excessive subset.\n");
}
}
for (int i = 0; i < n1; i++)
g[i].clear();
}
fprintf(stderr, "Total time: %.2f\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
I2luY2x1ZGUgPGNhc3NlcnQ+CiNpbmNsdWRlIDxjdGltZT4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDx2ZWN0b3I+Cgpjb25zdCBpbnQgbWF4biA9IDEwMDEwOwoKdXNpbmcgc3RkOjp2ZWN0b3I7Cgp2ZWN0b3IgPGludD4gZ1ttYXhuXTsKaW50IG4xLCBuMiwgbSwgdGVzdCA9IDA7CmludCBjW21heG5dLCByW21heG5dLCBsMVttYXhuXSwgbDJbbWF4bl0sIG9rW21heG5dLCBzdGVwID0gMTAwOwppbnQgcVttYXhuXSwgcXMsIHF6OwoKYm9vbCBkZnMoIGludCB1ICkKewogIGNbdV0gPSBzdGVwOwogIGZvciAoaW50IGkgPSAwOyBpIDwgKGludClnW3VdLnNpemUoKTsgaSsrKQogIHsKICAgIGludCB0byA9IGdbdV1baV07CiAgICBpZiAobDJbdG9dICE9IGwxW3VdICsgMSkKICAgICAgY29udGludWU7CiAgICBpZiAoclt0b10gPT0gLTEgfHwgKGNbclt0b11dICE9IHN0ZXAgJiYgZGZzKHJbdG9dKSAmJiBsMVtyW3RvXV0gPT0gbDFbdV0gKyAyKSkKICAgIHsKICAgICAgclt0b10gPSB1OwogICAgICByZXR1cm4gdHJ1ZTsKICAgIH0KICB9CiAgcmV0dXJuIGZhbHNlOwp9Cgpib29sIGRmczIoIGludCB1ICkKewogIGNbdV0gPSBzdGVwOwogIGZvciAoaW50IGkgPSAwOyBpIDwgKGludClnW3VdLnNpemUoKTsgaSsrKQogIHsKICAgIGludCB0byA9IGdbdV1baV07CiAgICBpZiAoclt0b10gIT0gLTEgJiYgY1tyW3RvXV0gIT0gc3RlcCkKICAgICAgZGZzMihyW3RvXSk7CiAgfQogIHJldHVybiBmYWxzZTsKfQoKdm9pZCBiZnMoKQp7CiAgcXMgPSBxeiA9IDA7CiAgbWVtc2V0KGMsIDAsIHNpemVvZihjWzBdKSAqIG4xKTsKICBtZW1zZXQobDEsIC0xLCBzaXplb2YobDFbMF0pICogbjEpOwogIG1lbXNldChsMiwgLTEsIHNpemVvZihsMlswXSkgKiBuMik7CiAgZm9yIChpbnQgaSA9IDA7IGkgPCBuMTsgaSsrKQogICAgaWYgKCFva1tpXSkKICAgICAgcVtxcyArIHF6KytdID0gaSwgbDFbaV0gPSAxLCBjW2ldID0gMTsKICB3aGlsZSAocXogPiAwKQogIHsKICAgIGludCB1ID0gcVtxcysrXTsgcXotLTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgKGludClnW3VdLnNpemUoKTsgaSsrKQogICAgICBpZiAobDJbZ1t1XVtpXV0gPT0gLTEpCiAgICAgIHsKICAgICAgICBsMltnW3VdW2ldXSA9IGwxW3VdICsgMTsKICAgICAgICBpZiAocltnW3VdW2ldXSAhPSAtMSAmJiBjW3JbZ1t1XVtpXV1dID09IDApCiAgICAgICAgewogICAgICAgICAgY1tyW2dbdV1baV1dXSA9IDE7CiAgICAgICAgICBsMVtyW2dbdV1baV1dXSA9IGwxW3VdICsgMjsKICAgICAgICAgIHFbcXMgKyBxeisrXSA9IHJbZ1t1XVtpXV07CiAgICAgICAgfQog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