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

(完)

Code[VS] 1332 题解 【Kosaraju】【Tarjan】的更多相关文章

  1. 强连通分量的模版 Kosaraju+Tarjan+Garbow

    PS:在贴出代码之前,我得说明内容来源——哈尔滨工业大学出版的<图论及应用>.虽然有一些错误的地方,但是不得不说是初学者该用的书. 从效率的角度来说,Kosaraju <Tarjan ...

  2. [题解](tarjan割点/点双)luogu_P3225_矿场搭建

    首先和割点有关,求割点,然后这些割点应该把这个图分成了多个点双,可以考虑点双的缩点,假如缩点做的话我们要分析每个点双的性质和贡献 先拿出一个点双来,如果它没有连接着割点,那么至少要建两个,以防止其中一 ...

  3. 洛谷 P4058 [Code+#1]木材 题解

    P4058 [Code+#1]木材 题目描述 有 \(n\) 棵树,初始时每棵树的高度为 \(H_i\),第 \(i\) 棵树每月都会长高 \(A_i\)​.现在有个木料长度总量为 $ S$ 的订单, ...

  4. 【NOIP2015四校联训Day7】 题 题解(Tarjan缩点+DFS)

    前言:没错,这题的名字就这么直白.我们考试题. ------------------ 你需要完成$n$道题目.有一些题目是相关的,当你做一道题的时候,如果你做过之前对它有帮助的题目,你会更容易地做出它 ...

  5. P3387 【模板】缩点 题解 (Tarjan)

    题目链接 P3387 [模板]缩点 解题思路 这几天搞图论,好有趣hhh,多写几篇博客. 上次学\(Tarjan\)求割点,这次缩点. 思路大概是多一个栈和染色的步骤,每次\(Tarjan\)的时候把 ...

  6. Code[VS] 1230 题解

    1230 元素查找 题目描述 Description 给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过. 输入描述 Input Description 第一行两个整 ...

  7. P3388 【模板】割点(割顶) 题解 (Tarjan)

    题目链接 P3388 [模板]割点(割顶) 解题思路 最近学的东西太杂了,多写点博客免得自己糊里糊涂的过去了. 这个题求割点,感觉这篇文章写得挺好. 割点是啥?如果去掉这个点之后连通图变成多个不连通图 ...

  8. hdu 5286 How far away ? tarjan/lca

    How far away ? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. 2015 NOIP day2 t2 信息传递 tarjan

    信息传递 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.luogu.org/problem/show?pid=2661 Descrip ...

随机推荐

  1. 回溯法解决N皇后问题(以四皇后为例)

    以4皇后为例,其他的N皇后问题以此类推.所谓4皇后问题就是求解如何在4×4的棋盘上无冲突的摆放4个皇后棋子.在国际象棋中,皇后的移动方式为横竖交叉的,因此在任意一个皇后所在位置的水平.竖直.以及45度 ...

  2. Filp Game

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25573   Accepted: 11052 题目链接: ...

  3. fis3-postpackager-loader插件说明

    fis3-postpackager-loader 静态资源前端加载器,用来分析页面中使用的和依赖的资源(js或css), 并将这些资源做一定的优化后插入页面中.如把零散的文件合并. 注意 此插件做前端 ...

  4. Oracle 10g Block Change Tracking特性

    Using Block Change Tracking to Improve Incremental Backup Performance 使用块改变跟踪改善增量备份的性能 The block cha ...

  5. Visual Studio Code 1.0发布:100+语言,300+pull请求,1000+扩展

    在第一个预览版发布一年后,微软发表了Visual Studio Code 1.0. 在//BUILD 2015大会上,微软宣布,他们的一个团队需要几个月来创建Visual Studio Code的第一 ...

  6. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  7. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  8. vmware虚拟机检测

    jpg改rar 

  9. 经典贪心算法uva11729

    uva11729 这个题的题意是 你有n个部下,每个部下需要完成一项任务.第i个部下需要你花Bi分钟交代任务,然后他会立刻独立地.无间断地执行Ji分钟后完成任务. 你需要选择交待任务的顺序,使得所有任 ...

  10. 封装JavaScript的AJAX

    // 创建request对象 function createXMLHttpRequest() { try { return new XMLHttpRequest();//大多数浏览器 } catch ...