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或Kosaraju算法

代码如下:

 /*
Tarjan algorithm
author : SHHHS
2016-09-13 19:01:12
*/
#include "cstdio"
#include "iostream" using namespace std ;
const int maxN = ;
const int INF = ; struct Tarjan {int to , next ;}e[ maxN ] ;
int head[ maxN ] , color[ maxN ] , stack[ maxN ] ,dfn[ maxN ] , low[ maxN ] , s[ maxN ];
bool vis [ maxN ] ; int cnt = , ans = -INF , dfs_num = , col_num = , top ; int gmin ( int x , int y ) {
return x < y ? x : y ;
} void Add_Edge ( int x , int y ) {
e[ ++cnt ].to = y ;
e[ cnt ].next = head[ x ] ;
head[ x ] = cnt ;
return ;
} void Tarjan ( int x ) {
dfn[ x ] = ++dfs_num ;
low[ x ] = dfs_num ;
vis [ x ] = true ;
stack [ ++top ] = x ;
for ( int i=head[ x ] ; i!= ; i=e[i].next ){
int temp = e[ i ].to ;
if ( !dfn[ temp ] ){
Tarjan ( temp ) ;
low[ x ] = gmin ( low[ x ] , low[ temp ] ) ;
}
else if ( vis[ temp ])low[ x ] = gmin ( low[ x ] , dfn[ temp ] ) ;
}
if ( dfn[ x ]==low[ x ] ) {
vis[ x ] = false ;
color[ x ] = ++col_num ;
s[ col_num ] ++ ;
while ( stack[ top ] != x ) {
s[ col_num ] ++ ;
color [stack[ top ]] = col_num ;
vis [ stack[ top-- ] ] = false ;
}
top -- ;
}
}
int main ( ) {
int N , M , target ; std::ios::sync_with_stdio ( ) ; cin >> N >> M ;
for ( int i= ; i<=M ; ++i ) {
int x , y , _ ;
cin >> x >> y >> _ ;
Add_Edge ( x , y ) ;
if ( _- ) Add_Edge ( y , x ) ;
}
for ( int i= ; i<=N ; ++i ) {
if ( !dfn[ i ] )
Tarjan ( i ) ;
}
for ( int i= ; i<=N ; ++i ) {
if( s[color[ i ]]>ans ) {
ans = s[color[i]] , target = i ;
}
}
cout << ans << endl ;
for ( int i= ; i<=N ; ++i ) {
if ( color[ i ]==color[ target ]) {
cout << i << ' ' ;
}
}
return ;
}
/*
Kosaraju algorithm
author : SHHHS
2016-09-18 00:32:28
*/ #include "cstdio"
#include "iostream"
#include "algorithm" using namespace std ; const int maxN = , maxM = ; struct Kosaraju { int to , next ; } ; Kosaraju E[ ][ maxM ] ;
bool vis[ maxN ];
int head[ ][ maxN ] , cnt[ ] , ord[maxN] , size[maxN] ,color[ maxN ]; int tot , dfs_num , col_num , N , M ; void Add_Edge( int x , int y , int _ ){//建图
E[ _ ][ ++cnt[ _ ] ].to = y ;
E[ _ ][ cnt[ _ ] ].next = head[ _ ][ x ] ;
head[ _ ][ x ] = cnt[ _ ] ;
} void DFS_1 ( int x , int _ ){
dfs_num ++ ;//发现时间
vis[ x ] = true ;
for ( int i = head[_][x] ; i ; i = E[_][i].next ) {
int temp = E[_][i].to;
if(vis[temp] == false) DFS_1 ( temp , _) ;
}
ord[(N<<) + - (++dfs_num) ] = x ;//完成时间加入栈
} void DFS_2 ( int x , int _ ){
// 强连通分量的大小
vis[ x ] = false ;
color[ x ] = col_num ;//染色
size[ color[ x ] ]++ ;
for ( int i=head[ _ ][ x ] ; i ; i = E[ _ ][ i ].next ) {
int temp = E[_][i].to;
if(vis[temp] == true) DFS_2(temp , _);
}
} int main ( ){
scanf("%d %d" , &N , &M );
for ( int i= ; i<=M ; ++i ){
int _x , _y , _ ;
scanf("%d %d %d" , &_x , &_y , &_ ) ;
Add_Edge( _x , _y , ) ;//原图的邻接表
Add_Edge( _y , _x , ) ;//逆图的邻接表
if ( _- ){
Add_Edge( _y , _x , ) ;
Add_Edge( _x , _y , ) ;
}
}
for ( int i= ; i<=N ; ++i )
if ( vis[ i ]==false )
DFS_1 ( i , ) ;//原图的DFS for ( int i = ; i<=(N<<) ; ++i ) {
if( ord[ i ]!= && vis[ ord[ i ] ] ){
tot ++ ; //强连通分量的个数
col_num ++ ;//染色的颜色
DFS_2 ( ord[ i ] , ) ;
}
}
int ans = - , target ;
for ( int i= ; i<=N ; ++i ) {
if( size[color[ i ]]>ans ) {
ans = size[color[i]] , target = i ;
}
}
printf ( "%d\n" , ans ) ;
for ( int i= ; i<=N ; ++i ) {
if ( color[ i ]==color[ target ]) {
printf ( "%d " , i ) ;
}
}
return ;
}

2016-09-14

(完)

最新文章

  1. Redis学习和环境搭建
  2. 【Python】使用Supervisor来管理Python的进程
  3. web项目总结——通过jsp+servlet实现对oracle的增删改查功能
  4. 在WPF中获取DataGridTemplateColumn模板定义的内容控件
  5. GJM :FPSCalc-简单FPS观测类 [转载]
  6. UVA-12436 Rip Van Winkle&#39;s Code (线段树区间更新)
  7. 【Irrlicht鬼火引擎】 认识鬼火引擎
  8. 追MM与Java的23种设计模式
  9. 判断是否为ie(包含ie11)
  10. C# System.Guid.NewGuid() 【转】
  11. [原创]浅谈NT下Ring3无驱进入Ring0的方法
  12. 常用的JQuery数字类型验证正则表达式
  13. jsp Ajax请求(返回json数据类型)
  14. Eclipse 中svn 分支,主干 合并与同步:
  15. ccf-炉石传说-201609-3
  16. [leetcode]Weekly Contest 68 (767. Reorganize String&amp;&amp;769. Max Chunks To Make Sorted&amp;&amp;768. Max Chunks To Make Sorted II)
  17. C++ Primer 笔记——关联容器
  18. day_6.8 py 网络编程
  19. Java向服务端转身 系统平台所对应的机器语言 虚拟CPU的机器语言字节码 bytecode
  20. testin 测试用例管理平台

热门文章

  1. C#接扣和抽象类
  2. 七牛:关于图片 EXIF 信息中旋转参数 Orientation 的理解
  3. Linux Shell 高级编程技巧2----shell工具
  4. Oracle 创建/删除 表空间、用户、授权
  5. 11g SQL Monitor
  6. map[C++]
  7. android AsyncTask介绍(转)
  8. MarkupExtension
  9. Java学习笔记(五)&mdash;&mdash;数组
  10. AJAX案例二:简单表单验证