Luogu3119 草鉴定-Tarjan+Topsort

Solution

简单的$Tarjan$题。

有大佬现成博客 就不写了 → 传送门

Code

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define rd read()
using namespace std; const int N = 1e5 + ;
const int inf = ~0U >> ; int n, m;
int head[N], tot, Head[N], Tot;
int low[N], dfn[N], cnt;
int st[N], tp, col, c[N], val[N], subsz[N], upsz[N], deg[N], bvis[N];
bool vis[N]; struct edge {
int nxt, fr, to;
}e[N], E[N]; queue<int> q; int read() {
int X = , p = ; char c = getchar();
for (; c > '' || c < ''; c = getchar())
if (c == '-') p = -;
for (; c >= '' && c <= ''; c = getchar())
X = X * + c - '';
return X * p;
} void add(int u, int v) {
e[++tot].to = v;
e[tot].fr = u;
e[tot].nxt = head[u];
head[u] = tot;
} void Add(int u, int v) {
E[++Tot].to = v;
E[Tot].fr = u;
E[Tot].nxt = Head[u];
Head[u] = Tot;
} void cmin(int &A, int B) {
if (A > B) A = B;
} void cmax(int &A, int B) {
if (A < B) A = B;
} void tarjan(int u) {
dfn[u] = low[u] = ++cnt;
vis[u] = ; st[++tp] = u;
for (int i = head[u]; i; i = e[i].nxt) {
int nt = e[i].to;
if (!dfn[nt]) {
tarjan(nt);
cmin(low[u], low[nt]);
}
else if (vis[nt]) cmin(low[u], dfn[nt]);
}
if (low[u] == dfn[u]) {
col++;
for (; tp;) {
int z = st[tp--];
c[z] = col;
vis[z] = ;
val[col]++;
if (z == u) break;
}
}
} void dfs(int u) {
vis[u] = ;
for (int i = Head[u]; i; i = E[i].nxt) {
int nt = E[i].to;
dfs(nt);
}
} int dfs2(int u) {
if (bvis[u] == ) return ;
if (bvis[u] == -) return ;
if (u == c[]) return ;
bool flag = false;
for (int i = Head[u]; i; i = E[i].nxt) {
int nt = E[i].to;
if (dfs2(nt)) {
flag = true;
cmax(upsz[u], upsz[nt] + val[u]);
}
}
bvis[u] = flag ? : -;
return flag;
} void Topsort() {
subsz[c[]] = val[c[]];
for (int i = ; i <= col; ++i)
if (!deg[i]) q.push(i);
for (int u; !q.empty(); ) {
u = q.front(); q.pop();
for (int i = Head[u]; i; i = E[i].nxt) {
int nt = E[i].to;
if (subsz[u] != -inf)
cmax(subsz[nt], subsz[u] + val[nt]);
deg[nt]--;
if (!deg[nt]) q.push(nt);
}
}
} int main()
{
n = rd; m = rd;
for (int i = ; i <= m; ++i) {
int u = rd, v = rd;
add(u, v);
}
for (int i = ; i <= n; ++i)
if (!dfn[i]) tarjan(i);
for (int i = ; i <= m; ++i) {
int u = e[i].fr, v = e[i].to;
u = c[u], v = c[v];
if (u == v) continue;
Add(u, v);
deg[v]++;
}
memset(vis, , sizeof(vis));
for (int i = ; i <= col; ++i) subsz[i] = -inf;
dfs(c[]);
Topsort();
bvis[c[]] = ;
upsz[c[]] = val[c[]];
for (int i = ; i <= col; ++i)
if (!vis[i]) dfs2(i);
int ans = val[c[]];
for (int i = ; i <= m; ++i) {
int u = e[i].fr, v = e[i].to;
u = c[u]; v = c[v];
if (u == v) continue;
if (!vis[v] || !(bvis[u] == )) continue;
cmax(ans, subsz[v] + upsz[u] - val[c[]]);
}
printf("%d\n", ans);
}
上一篇:基于Docker搭建大数据集群(六)Hive搭建


下一篇:基于Docker搭建大数据集群(一)Docker环境部署