码迷,mamicode.com
首页 > 其他好文 > 详细

Code[VS] 1332 上白泽慧音题解

时间:2016-09-14 16:24:01      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

Code[VS] 1332 上白泽慧音题解

 
题目描述 Description

在幻想乡,上白泽慧音是以知识渊博闻名的老师。春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄。因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点。人间之里由N个村庄(编号为1..N)和M条道路组成,道路分为两种一种为单向通行的,一种为双向通行的,分别用1和2来标记。如果存在由村庄A到达村庄B的通路,那么我们认为可以从村庄A到达村庄B,记为(A,B)。当(A,B)和(B,A)同时满足时,我们认为A,B是绝对连通的,记为<A,B>。绝对连通区域是指一个村庄的集合,在这个集合中任意两个村庄X,Y都满足<X,Y>。现在你的任务是,找出最大的绝对连通区域,并将这个绝对连通区域的村庄按编号依次输出。若存在两个最大的,输出字典序最小的,比如当存在1,3,4和2,5,6这两个最大连通区域时,输出的是1,3,4。 

输入描述 Input Description

第1行:两个正整数N,M

第2..M+1行:每行三个正整数a,b,t, t = 1表示存在从村庄a到b的单向道路,t = 2表示村庄a,b之间存在双向通行的道路。保证每条道路只出现一次。

输出描述 Output Description

第1行: 1个整数,表示最大的绝对连通区域包含的村庄个数。

第2行:若干个整数,依次输出最大的绝对连通区域所包含的村庄编号。

样例输入 Sample Input

5 5

1 2 1

1 3 2

2 4 2

5 1 2

3 5 1

样例输出 Sample Output

3

1 3 5

数据范围及提示 Data Size & Hint

对于60%的数据:N <= 200且M <= 10,000

对于100%的数据:N <= 5,000且M <= 50,000

 

—————————————————————————分割线—————————————————————————

 分析

这道题是明显的Tarjan Algorithm 裸题,需要对强连通分量进行简单的染色,再统计即可。

 

代码如下:

 1 /*
 2    Tarjan algorithm 
 3    author : SHHHS
 4    2016-09-13 19:01:12 
 5 */
 6 #include "cstdio"
 7 #include "iostream"
 8 
 9 using namespace std ;
10 const int maxN  = 200010 ;
11 const int INF = 2147483647 ;
12 
13 struct Tarjan {int to , next ;}e[ maxN ] ;
14 int head[ maxN ] , color[ maxN ] , stack[ maxN ] ,dfn[ maxN ] , low[ maxN ] , s[ maxN ];
15 bool vis [ maxN ] ;
16 
17 int cnt = 0 , ans = -INF , dfs_num = 0 , col_num = 0 , top  ;
18 
19 int gmin ( int x , int y ) {
20          return x < y  ? x : y ;
21 }
22 
23 void Add_Edge ( int x , int y ) {
24          e[ ++cnt ].to = y ;
25          e[ cnt ].next = head[ x ] ;
26          head[ x ] = cnt ;
27          return ;
28 }
29 
30 void Tarjan ( int x ) {
31          dfn[ x ] = ++dfs_num ;
32          low[ x ] = dfs_num ;
33          vis [ x ] = true ;
34          stack [ ++top ] = x ;
35          for ( int i=head[ x ] ; i!=0 ; i=e[i].next ){
36                   int temp = e[ i ].to ;
37                   if ( !dfn[ temp ] ){
38                            Tarjan ( temp ) ;
39                            low[ x ] = gmin ( low[ x ] , low[ temp ] ) ;
40                  }
41                  else if ( vis[ temp ])low[ x ] = gmin ( low[ x ] , dfn[ temp ] ) ;
42          }
43          if ( dfn[ x ]==low[ x ] ) {
44                   vis[ x ] = false ;
45                   color[ x ] = ++col_num ;
46                   s[ col_num ] ++ ;
47                   while ( stack[ top ] != x ) {
48                            s[ col_num ] ++ ;
49                            color [stack[ top ]] = col_num ;
50                            vis [ stack[ top-- ] ] = false ;
51                  }
52                  top -- ;
53          }
54 }
55 int main ( ) {
56          int N , M , target ;
57          
58          std::ios::sync_with_stdio ( 0 ) ; 
59          
60          cin >> N >> M ;
61          for ( int i=1 ; i<=M ; ++i ) {
62                   int x , y , _ ;
63                   cin >> x >> y >> _ ;
64                   Add_Edge ( x , y ) ;
65                   if ( _-1 ) Add_Edge ( y , x ) ;
66          }
67          for ( int i=1 ; i<=N ; ++i ) {
68                   if ( !dfn[ i ] )  
69                           Tarjan ( i ) ; 
70          }
71          for ( int i=1 ; i<=N ; ++i ) {
72                   if( s[color[ i ]]>ans ) {
73                            ans = s[color[i]] , target = i ;
74                  }
75          }
76          cout << ans << endl ;
77          for ( int i=1 ; i<=N ; ++i ) {
78                   if ( color[ i ]==color[ target ]) {
79                            cout << i <<   ;
80                  }
81          }
82          return 0 ;
83 }

 

2016-09-14

(完)

 

Code[VS] 1332 上白泽慧音题解

标签:

原文地址:http://www.cnblogs.com/shadowland/p/5872417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!