zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    从缓冲上看阻塞与非阻塞socket在发送接收上的区别
    关于TCP封包、粘包、半包
    CURL 和LIBCURL C++代码 上传本地文件,好不容易碰到了这种折腾我几天的代码
    Spring boot 搭配 JPA 生成表注释 和 字段注释
    Spring Data JPA 中常用注解
    SpringBoot Data JPA 关联表查询的方法
    Spring boot data JPA数据库映射关系 : @OneToOne,@OneToMany,@ManyToMany
    Spring Boot Jpa 表名小写转大写
    SpringBoot入门系列~Spring-Data-JPA自动建表
    使用Spring-Session共享使用Session
  • 原文地址:https://www.cnblogs.com/CzxingcHen/p/9595345.html
Copyright © 2011-2022 走看看