上白泽慧音(luogu P1726

                        题目传送门

题目描述

在幻想乡,上白泽慧音是以知识渊博闻名的老师。春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄。因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点。人间之里由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。

输入输出格式

输入格式:

 

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

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

 

输出格式:

 

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

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

 

输入输出样例

输入样例#1: 
5 5
1 2 1
1 3 2
2 4 2
5 1 2
3 5 1
输出样例#1: 
3
1 3 5

说明

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

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

                 

  tarjan模板题。(学习tarjan推荐byvoid的博客:传送门

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<stack>
 7 #include<bitset>
 8 #define LL long long
 9 #define RI register int
10 using namespace std;
11 const int INF = 0x7ffffff ;
12 const int N = 5000 + 10 ;
13 const int M = 50000 + 10 ;
14 
15 inline int read() {
16     int k = 0 , f = 1 ; char c = getchar() ;
17     for( ; !isdigit(c) ; c = getchar())
18       if(c == '-') f = -1 ;
19     for( ; isdigit(c) ; c = getchar())
20       k = k*10 + c-'0' ;
21     return k*f ;
22 }
23 struct Edge {
24     int to, next ;
25 }e[M<<1] ;
26 int n, m, dclock, tot ; int head[N], dfn[N], low[N], num[N], bl[N] ;
27 stack<int>s ;  bitset<N>ins ;
28 inline void add_edge(int x,int y) {
29     static int cnt = 1 ;
30     e[++cnt].to = y, e[cnt].next = head[x], head[x] = cnt ;
31 }
32 
33 void tarjan(int x) {
34     dfn[x] = low[x] = ++dclock ; s.push(x) ; ins[x] = 1 ;
35     for(int i=head[x];i;i=e[i].next) {
36         int y = e[i].to ;
37         if(!dfn[y]) {
38             tarjan(y) ; low[x] = min(low[x],low[y]) ;
39         } else if(ins[y]) low[x] = min(low[x],dfn[y]) ;
40     }
41     if(dfn[x] == low[x]) {
42         tot++ ;
43         while(1) {
44             int xx = s.top() ; s.pop() ; ins[xx] = 0 ; 
45             bl[xx] = tot, num[tot]++ ;
46             if(xx == x) break ;
47         }
48     }
49 }
50 
51 int main() {
52     n = read(), m = read() ;
53     for(int i=1;i<=m;i++) {
54         int x = read(), y = read(), t = read() ;
55         add_edge(x,y) ; if(t == 2) add_edge(y,x) ;
56     }
57     for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i) ;
58     int maxx = 0 ;
59     for(int i=1;i<=tot;i++) maxx = max(maxx,num[i]) ;
60     int k ;
61     for(int i=1;i<=n;i++) if(num[bl[i]] == maxx) { k = bl[i] ; break ; }
62     printf("%d\n",maxx) ;
63     for(int i=1;i<=n;i++) if(bl[i] == k) printf("%d ",i) ;
64     return 0 ;
65 }
66  

 

posted @ 2018-04-14 14:39  zubizakeli  阅读(209)  评论(0编辑  收藏  举报