NOIP2015信息传递(拓扑排序 / 并查集)

传送

【题意】求最小环长

【解题】我恨这个题

我是一个没有读题的杀手傻手

 

 

20分代码(单环)

//第二题 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector> 
#include<queue>
#define fr(i,n)  for( int i = 1; i <= n; ++i)

const int N = 2e5 + 9;
int idv[N],x,sum,n,son[N];

std::queue<int>q;
int main(){
    scanf("%d",&n);
    fr(i,n){
        scanf("%d",&x);
        idv[x]++;
        son[i] = x;
    }
    fr(i,n)  if(!idv[i]) { sum++; q.push(i); break;}
    while(!q.empty()){
        int x = q.front();
        q.pop();
        if(!idv[x]){
            //////////////////
              int v = son[x];
              idv[v]--;
              if(idv[v] == 0){
                  sum++;
                  q.push(v);
              }
              else break;
          ///////////////////////    
        }
    }
    printf("%d",n - sum);
    return 0;
}
View Code

 

 

60分代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector> 
#include<queue>
#define min(x,y) ((x)<(y)?(x):(y))
#define fr(i,n)  for(register int i = 1; i <= n; ++i)
const int N = 2e5 + 9;
int idv[N],x,sum,ans = 0x3f3f3f,n,son[N],vis[N],temp;
std::queue<int>q; 
int main(){
    scanf("%d",&n);
    fr(i,n){
        scanf("%d",&x);
        idv[x]++;
        son[i] = x;
    }
    fr(i,n) if(!idv[i]){ idv[son[i]]--;} 
    fr(i,n){ 
        if (!vis[i] && idv[i] != 0){
            vis[i] = 1;
            temp = 1;
            int v = son[i];
            while (!vis[v]){ 
                vis[v] = 1;
                v = son[v];
                temp++;
            }
            ans = min(ans,temp);
        }
    }
    printf("%d",ans);
    return 0;
}
View Code

 

100分

#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <algorithm>
#define min(x,y) ((x)<(y)?(x):(y))
using namespace std;
const int N = 200050;
int to[N],vis[N], idv[N];
int n,ans,temp,j;
queue <int> q;

int main(){
    scanf("%d", &n);
    ans = n;
    for (int i = 1; i <= n; i++){
        scanf("%d", &to[i]);
        ++idv[to[i]];
    }
    for (int i = 1; i <= n; i++)
        if (idv[i] == 0){
            q.push(i);
            vis[i] = 1;
        }
        
    while (!q.empty()){
        int u = q.front();
        q.pop();
        --idv[to[u]];
        if (idv[to[u]] == 0) {
            vis[to[u]] = 1;
            q.push(to[u]);
        }  
    }
    
    for (int i = 1; i <= n; i++){ 
        if (!vis[i] && idv[i] != 0){
            vis[i] = 1;
            temp = 1;
            j = to[i];
            while (!vis[j]){ 
                vis[j] = 1;
                j = to[j];
                temp++;
            }
            ans = min(ans,temp);
        }
    }
    
    printf("%d\n", ans);
    return 0;
}
View Code

 

据说这题用并查集代码还会短,心态不爆炸的时候再来补吧。

posted @ 2019-11-11 14:58  QUEKI嶺冬  阅读(209)  评论(0编辑  收藏  举报
/*! Color themes for Google Code Prettify | MIT License | github.com/jmblog/color-themes-for-google-code-prettify */ .pln{color:#4d4d4c}ol.linenums{margin-top:0;margin-bottom:0;color:#8e908c}li.L0,li.L1,li.L2,li.L3,li.L4,li.L5,li.L6,li.L7,li.L8,li.L9{padding-left:1em;background-color:#fff;list-style-type:decimal!important;}@media screen{.str{color:#718c00}.kwd{color:#8959a8}.com{color:#8e908c}.typ{color:#4271ae}.lit{color:#f5871f}.pun{color:#4d4d4c}.opn{color:#4d4d4c}.clo{color:#4d4d4c}.tag{color:#c82829}.atn{color:#f5871f}.atv{color:#3e999f}.dec{color:#f5871f}.var{color:#c82829}.fun{color:#4271ae}} /*下面是我设置背景色,字体大小和字体*/ .cnblogs-markdown code{ background:#fff!important; } .cnblogs_code,.cnblogs_code span,.cnblogs-markdown .hljs{ font-size:16px!important; } .syntaxhighlighter a, .syntaxhighlighter div, .syntaxhighlighter code, .syntaxhighlighter table, .syntaxhighlighter table td, .syntaxhighlighter table tr, .syntaxhighlighter table tbody, .syntaxhighlighter table thead, .syntaxhighlighter table caption, .syntaxhighlighter textarea { font-size: 16px!important; } .cnblogs_code, .cnblogs_code span, .cnblogs-markdown .hljs{ font-family:consolas, "Source Code Pro", monaco, monospace !important; } //以上是代码高亮 /* 文字特效 */