zoukankan      html  css  js  c++  java
  • bzoj 3037 贪心

    我们可以贪心的分析,每个点的入度如果是0,那么这个点不可能

    被用来更新答案,那么我们每次找入度为0的点,将他去掉,如果他连的

    点没有被更新过答案,那么更新答案,去掉该点,环的时候最后处理就行了

    /**************************************************************
        Problem: 3037
        User: BLADEVIL
        Language: Pascal
        Result: Accepted
        Time:1672 ms
        Memory:12920 kb
    ****************************************************************/
     
    //By BLADEVIL
    var
        n, ans, cnt                 :longint;
        other, sum                  :array[0..1000020] of longint;
        flag                        :array[0..1000020] of boolean;
        que                         :array[0..1000020] of longint;
         
    procedure init;
    var
        i                           :longint;
    begin
        readln(n);
        for i:=1 to n do
        begin
            read(other[i]);
            inc(sum[other[i]]);
        end;
    end;
     
    procedure main;
    var
        h, t, cur                   :longint;
        i, j                        :longint;
         
    begin
        t:=0;
        for i:=1 to n do
        if sum[i]=0 then
        begin
            inc(t);
            que[t]:=i;
        end;
        h:=1;
        while h<=t do
        begin
            cur:=que[h];
            inc(h);
            if (not flag[cur]) and (not flag[other[cur]]) then
            begin
                inc(ans);
                flag[other[cur]]:=true;
                dec(sum[other[other[cur]]]);
                if sum[other[other[cur]]]=0 then
                begin
                    inc(t);
                    que[t]:=other[other[cur]];
                end;
            end;
            flag[cur]:=true;
        end;
         
        for i:=1 to n do
            if  not flag[i] then
            begin
                cnt:=1;
                flag[i]:=true;
                j:=i;
                while other[j]<>i do
                begin
                    flag[other[j]]:=true;
                    inc(cnt);
                    j:=other[j];
                end; 
                inc(ans,cnt div 2);
            end;
        writeln(ans);
    end;
     
    begin
        init;
        main;
    end.
  • 相关阅读:
    [CLR via C#]5.2 引用类型和值类型
    Yii easyWechat 开发的时候报错:cURL error 60: SSL certificate problem: unable to get local issuer certificat
    前端浏览器自动刷新神器:Browsersync
    phpStudy 切换版本后没有权限的问题
    最全的CSS浏览器兼容问题
    (转)详解JS位置、宽高属性之一:offset系列
    js 跨域问题
    移动端IOS 固定下方的输入框,点击输入框位置会变的修复
    ie8网页时调用特定的css文件
    Bootstrap如何禁止响应式布局 不适配
  • 原文地址:https://www.cnblogs.com/BLADEVIL/p/3496041.html
Copyright © 2011-2022 走看看