隨筆 - 147  文章 - 71  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(1)

          隨筆分類(146)

          隨筆檔案(147)

          文章分類(28)

          文章檔案(28)

          喜歡的Blog

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          http://acm.fzu.edu.cn/problem.php?pid=1229
          判斷有N條邊的有向圖的環(huán)的個數(shù)!
          要點:由于每個點只有一個出度,故給每個環(huán)都用特定的數(shù)值進行標記!
          import java.util.*;
          import java.io.*;

          public class ACM_1229{
              
              
          public static void main(String rgs[]) throws Exception
              
          {
                  BufferedReader stdin 
          = new BufferedReader(new InputStreamReader(System.in));
                     String s 
          =null;
                    
          while((s = stdin.readLine())!=null)
                  
          {
                      
          int i,j,k,n=Integer.parseInt(s),count=0
                      
          int[] a=new int[n+1];
                      
          int[] b=new int[n+1];
                      
          int[] flag=new int[n+1];
                      s 
          = stdin.readLine();
                      StringTokenizer st 
          = new StringTokenizer(s);
                      
          for(i=0;i<n;i++){
                          a[i] 
          = Integer.parseInt(st.nextToken());
                          flag[i]
          =0;
                      }

                      
          for(i=0;i<n;i++){
                          
          int p=i+1;
                          
          if(flag[i]==0){
                              j
          =i;
                              k
          =0;
                              
          do{
                                  flag[j]
          =p;
                                  k
          ++;
                                  b[j]
          =k;
                                  j
          =a[j];
                              }
          while(flag[j]==0);
                              
          if(flag[j]==p)
                                  count
          +=k+1-b[j];
                          }

                      }

                      System.out.println(count);
                  }
             
              }

          }
          posted on 2010-09-26 20:58 飛翔天使 閱讀(228) 評論(0)  編輯  收藏 所屬分類: foj

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 龙陵县| 尉犁县| 永吉县| 洞口县| 东方市| 凤台县| 忻城县| 旌德县| 勐海县| 定结县| 长岭县| 新密市| 桐梓县| 台江县| 津南区| 富川| 乐清市| 汤原县| 伊金霍洛旗| 陆良县| 青州市| 彭泽县| 青冈县| 乌兰县| 奉节县| 阿拉善盟| 衡水市| 南岸区| 长治市| 慈利县| 望都县| 博兴县| 临泽县| 永泰县| 富宁县| 宜君县| 兰西县| 合肥市| 亳州市| 全南县| 齐河县|