Luogu 2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

Posted czxingchen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Luogu 2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm相关的知识,希望对你有一定的参考价值。

基环树森林,然而我比较菜,直接tarjan找环。

发现缩点之后变成了DAG,每一个点往下走一定会走到一个环,缩点之后搜一遍看看会走到哪个环以及那个环的编号是多少,答案就是环的$siz$$ + $要走的路程。

比较垃圾的我忘记了判重边WA了好多发……

时间复杂度$O(n)$。

Code:

技术分享图片
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 1e5 + 5;
const int inf = 1 << 30;

int n, to[N], dfsc = 0, dfn[N], low[N], cur[N];
int scc = 0, siz[N], top = 0, sta[N], bel[N], dis[N];
int tot = 0, head[N];
bool vis[N], isCur[N];

struct Edge {
    int to, nxt;
} e[N];

inline void add(int from, int ver) {
    e[++tot].to = ver;
    e[tot].nxt = head[from];
    head[from] = tot;
}

inline void read(int &X) {
    X = 0; char ch = 0; int op = 1;
    for(; ch > 9|| ch < 0; ch = getchar())
        if(ch == -) op = -1;
    for(; ch >= 0 && ch <= 9; ch = getchar())
        X = (X << 3) + (X << 1) + ch - 48;
    X *= op;
}

inline int min(int x, int y) {
    return x > y ? y : x;
}

inline int max(int x, int y) {
    return x > y ? x : y;
}

void tarjan(int x) {
    dfn[x] = low[x] = ++dfsc;
    sta[++top] = x, vis[x] = 1;
    
    int y = to[x];
    if(!dfn[y]) {
        tarjan(y);
        low[x] = min(low[x], low[y]);
    } else if(vis[y]) low[x] = min(low[x], dfn[y]);
    
    if(low[x] == dfn[x]) {
        ++scc;
        for(; sta[top + 1] != x; --top) {
            bel[sta[top]] = scc;
            siz[scc]++;
            vis[sta[top]] = 0;
        }
    }
}

void dfs(int x) {
    vis[x] = 1;
    if(isCur[x]) {
        dis[x] = 0, cur[x] = x;
        return;
    }
    for(int i = head[x]; i; i = e[i].nxt) {
        int y = e[i].to;
        if(!vis[y]) dfs(y);
        dis[x] = dis[y] + 1, cur[x] = cur[y];
    }
}

int main() {
//    freopen("testdata.in", "r", stdin);
//    freopen("mine.out", "w", stdout);
    
    read(n);
    for(int i = 1; i <= n; i++) read(to[i]);
    
    for(int i = 1; i <= n; i++)
        if(!dfn[i]) tarjan(i);
        
/*    for(int i = 1; i <= n; i++)
        printf("%d ", bel[i]);
    printf("
");   */
    for(int i = 1; i <= n; i++)
        if(to[i] == i) isCur[bel[i]] = 1;
    for(int i = 1; i <= scc; i++) 
        if(siz[i] >= 2) isCur[i] = 1;
    
    for(int i = 1; i <= n; i++) {
        if(bel[i] == bel[to[i]]) continue;
        add(bel[i], bel[to[i]]);
    }    
    
    for(int i = 1; i <= scc; i++)
        if(!vis[i]) dfs(i);
    
/*    for(int i = 1; i <= scc; i++)
        printf("%d ", cur[i]);
    printf("
");   */
    
    for(int i = 1; i <= n; i++) {
        if(isCur[bel[i]]) printf("%d
", siz[bel[i]]);
        else printf("%d
", dis[bel[i]] + siz[cur[bel[i]]]);
    }
    
    return 0;
}
View Code

 

以上是关于Luogu 2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm的主要内容,如果未能解决你的问题,请参考以下文章

洛谷 P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

题解[USACO08DEC-Gold] Trick or Treat on the Farm

P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm(Tarjan+记忆化)

P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm - Tarjan+拓扑DP

P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 记忆化搜索dfs

一道特别好的深搜题+bfs