Section 1.1
parent
b7111be060
commit
403cee6e88
@ -0,0 +1,53 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
inline int min(int a, int b) { return a < b ? a : b; }
|
||||||
|
const int maxn = 110, maxm = 10100;
|
||||||
|
int head[maxn], next[maxm], to[maxm], ecnt, f[maxn], g[maxn];
|
||||||
|
inline void addEdge(int f, int t)
|
||||||
|
{
|
||||||
|
ecnt++;
|
||||||
|
next[ecnt] = head[f];
|
||||||
|
head[f] = ecnt;
|
||||||
|
to[ecnt] = t;
|
||||||
|
}
|
||||||
|
int dfn[maxn], low[maxn], stk[maxn], scc[maxn], scccnt, top, idx;
|
||||||
|
void tarjan(int x)
|
||||||
|
{
|
||||||
|
dfn[x] = low[x] = ++idx;
|
||||||
|
stk[top++] = x;
|
||||||
|
for (int i = head[x]; i; i = next[i])
|
||||||
|
if (!dfn[to[i]])
|
||||||
|
tarjan(to[i]), low[x] = min(low[x], low[to[i]]);
|
||||||
|
else if (!scc[to[i]])
|
||||||
|
low[x] = min(low[x], dfn[to[i]]);
|
||||||
|
if (dfn[x] == low[x])
|
||||||
|
{
|
||||||
|
scccnt++;
|
||||||
|
do
|
||||||
|
scc[stk[--top]] = scccnt;
|
||||||
|
while (stk[top] != x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
scanf("%d", &n);
|
||||||
|
for (int i = 1, x; i <= n; i++)
|
||||||
|
for (scanf("%d", &x); x; scanf("%d", &x))
|
||||||
|
addEdge(i, x);
|
||||||
|
for (int i = 1; i <= n; i++)
|
||||||
|
if (!dfn[i]) tarjan(i);
|
||||||
|
for (int i = 1; i <= n; i++)
|
||||||
|
for (int j = head[i]; j; j = next[j])
|
||||||
|
if (scc[i] != scc[to[j]])
|
||||||
|
f[scc[i]]++, g[scc[to[j]]]++;
|
||||||
|
int ans1 = 0, ans2 = 0;
|
||||||
|
if (scccnt == 1)
|
||||||
|
printf("1\n0");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int i = 1; i <= scccnt; i++)
|
||||||
|
ans1 += f[i] == 0, ans2 += g[i] == 0;
|
||||||
|
printf("%d\n%d", ans2, ans1 > ans2 ? ans1 : ans2);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@ -0,0 +1,65 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
inline int min(int a, int b) { return a < b ? a : b; }
|
||||||
|
int head[10010], next[50010], to[50010], ecnt;
|
||||||
|
int dfn[10010], low[10010], stk[10010], scc[10010], top, idx, scccnt;
|
||||||
|
bool instk[10010];
|
||||||
|
int deg[10010];
|
||||||
|
inline void addEdge(int f, int t)
|
||||||
|
{
|
||||||
|
ecnt++;
|
||||||
|
next[ecnt] = head[f];
|
||||||
|
head[f] = ecnt;
|
||||||
|
to[ecnt] = t;
|
||||||
|
}
|
||||||
|
void tarjan(int x)
|
||||||
|
{
|
||||||
|
dfn[x] = low[x] = ++idx;
|
||||||
|
instk[stk[top++] = x] = true;
|
||||||
|
for (int cur = head[x]; cur; cur = next[cur])
|
||||||
|
if (!dfn[to[cur]])
|
||||||
|
tarjan(to[cur]), low[x] = min(low[x], low[to[cur]]);
|
||||||
|
else if (instk[to[cur]])
|
||||||
|
low[x] = min(low[x], dfn[to[cur]]);
|
||||||
|
if (dfn[x] == low[x])
|
||||||
|
{
|
||||||
|
scccnt++;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
top--;
|
||||||
|
scc[stk[top]] = scccnt;
|
||||||
|
instk[stk[top]] = false;
|
||||||
|
} while (stk[top] != x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int n, m;
|
||||||
|
scanf("%d%d", &n, &m);
|
||||||
|
for (int i = 0, x, y; i < m; i++)
|
||||||
|
{
|
||||||
|
scanf("%d%d", &x, &y);
|
||||||
|
addEdge(x, y);
|
||||||
|
}
|
||||||
|
for (int i = 1; i <= n; i++)
|
||||||
|
if (!dfn[i])
|
||||||
|
tarjan(i);
|
||||||
|
for (int i = 1; i <= n; i++)
|
||||||
|
for (int cur = head[i]; cur; cur = next[cur])
|
||||||
|
if (scc[i] != scc[to[cur]])
|
||||||
|
deg[scc[i]]++;
|
||||||
|
int zcnt = 0, id = 0;
|
||||||
|
for (int i = 1; i <= scccnt; i++)
|
||||||
|
if (deg[i] == 0)
|
||||||
|
zcnt++, id = i;
|
||||||
|
if (zcnt != 1)
|
||||||
|
putchar('0');
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int ans = 0;
|
||||||
|
for (int i = 1; i <= n; i++)
|
||||||
|
if (scc[i] == id)
|
||||||
|
ans++;
|
||||||
|
printf("%d", ans);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@ -0,0 +1,46 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
inline int min(int a, int b) { return a < b ? a : b; }
|
||||||
|
const int maxn = 100010;
|
||||||
|
int head[maxn], next[maxn << 1], to[maxn << 1], ecnt, n, m;
|
||||||
|
int dfn[maxn], scc[maxn], cnt[maxn], scccnt, stk[maxn], low[maxn], idx, top;
|
||||||
|
inline void addEdge(int f, int t)
|
||||||
|
{
|
||||||
|
ecnt++;
|
||||||
|
next[ecnt] = head[f];
|
||||||
|
head[f] = ecnt;
|
||||||
|
to[ecnt] = t;
|
||||||
|
}
|
||||||
|
void tarjan(int x)
|
||||||
|
{
|
||||||
|
dfn[x] = low[x] = ++idx;
|
||||||
|
stk[top++] = x;
|
||||||
|
for (int i = head[x]; i; i = next[i])
|
||||||
|
if (!dfn[to[i]])
|
||||||
|
tarjan(to[i]), low[x] = min(low[x], low[to[i]]);
|
||||||
|
else if (!scc[to[i]])
|
||||||
|
low[x] = min(low[x], dfn[to[i]]);
|
||||||
|
if (dfn[x] == low[x])
|
||||||
|
{
|
||||||
|
scccnt++;
|
||||||
|
do
|
||||||
|
scc[stk[--top]] = scccnt;
|
||||||
|
while (stk[top] != x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
scanf("%d%d", &n, &m);
|
||||||
|
for (int i = 0, x, y; i < m; i++)
|
||||||
|
{
|
||||||
|
scanf("%d%d", &x, &y);
|
||||||
|
addEdge(x, y);
|
||||||
|
}
|
||||||
|
for (int i = 1; i <= n; i++)
|
||||||
|
if (!dfn[i]) tarjan(i);
|
||||||
|
int ans = 0;
|
||||||
|
for (int i = 1; i <= n; i++) cnt[scc[i]]++;
|
||||||
|
for (int i = 1; i <= scccnt; i++)
|
||||||
|
if (cnt[i] > 1) ans++;
|
||||||
|
printf("%d", ans);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
void tarjan(int u)
|
||||||
|
{
|
||||||
|
dfn[u] = low[u] = ++idx;
|
||||||
|
st[top++] = u;
|
||||||
|
for (Edge cur : G[u])
|
||||||
|
if (!dfn[cur.to])
|
||||||
|
tarjan(cur.to),
|
||||||
|
low[u] = min(low[u], low[cur.to]);
|
||||||
|
else if (!scc[cur.to])
|
||||||
|
low[u] = min(low[u], dfn[cur.to]);
|
||||||
|
if (dfn[u] == low[u] && ++cnt)
|
||||||
|
do scc[st[--top]] = cnt;
|
||||||
|
while (st[top] != u);
|
||||||
|
}
|
||||||
Binary file not shown.
Loading…
Reference in new issue