codevs 1332 上白泽慧音
来源:互联网 发布:爱知中学滨河 编辑:程序博客网 时间:2024/06/03 16:33
题目描述 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 51 2 11 3 22 4 25 1 23 5 1
样例输出 Sample Output
31 3 5
题目在最后提到了输出最大的绝对联通区域,根据百度的定义呢,这个东西应该被叫做强连通分量吧~~
于是!
这道题就是一道tarjan求强连通分量的裸题
只是最后的时候需要顺序输出比较麻烦,使用一些特殊方法解决一下这题即可通过
#include<iostream>#include<cstdio>#include<queue>#include<stack>#include<cstring># define CY 250using namespace std;const int maxn=233333;struct doubi{ int f,t,d;}edge[maxn];int next[maxn],first[maxn],tot;void build(int f,int t){ edge[++tot].f=f; edge[tot].t=t; next[tot]=first[f]; first[f]=tot;}int pre[124500];int cnt;stack<int> q;int scc_num[maxn];int scc_cnt;int dfs(int u){ int lowu=pre[u]=++cnt; q.push(u); for(int i=first[u];i;i=next[i]){ int v=edge[i].t; if(!pre[v]){ int lowv=dfs(v); lowu=min(lowu,lowv); } else if(!scc_num[v]){ lowu=min(lowu,pre[v]); } } if(pre[u]==lowu){ scc_cnt++; while(CY){ int v=q.top(); q.pop(); scc_num[v]=scc_cnt; if(u==v) break; } } return lowu;}int tong[maxn];int main(){ int n,m; cin>>n>>m; for(int i=1;i<=m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); if(c==1){ build(a,b); } else{ build(a,b); build(b,a); } } for(int i=1;i<=n;i++){ if(!pre[i]){ dfs(i); } } for(int i=1;i<=n;i++){ tong[scc_num[i]]++; } int temp=0,t; for(int i=1;i<=12450;i++){ if(tong[i]>temp){ temp=tong[i]; t=i; } } cout<<temp<<endl; for(int i=1;i<=n;i++){ if(scc_num[i]==t){ cout<<i<<" "; } } puts(""); return 0;}
1 0
- codevs 1332 上白泽慧音
- 【codevs 1332】上白泽慧音
- codevs 1332 上白泽慧音
- CODEVS 1332 上白泽慧音
- Codevs 1332 上白泽慧音
- CODEVS 1332 上白泽慧音
- 【codevs 1332】上白泽慧音
- codevs 1332 上白泽慧音(Tarjan)
- <tarjan算法模板> codevs 1332 上白泽慧音
- Codevs
- codevs 1060
- [CodeVS]3304
- codevs 4248
- Codevs 苹果树
- Codevs 挂缀
- Codevs 加密算法
- CodeVs 1011
- codevs 买票
- 安装了Ubuntu进不了win8.1(引导修复)
- GCD 信号量
- UML之类图
- 利用Data URL(data:image/jpg;base64,)加速网页加载的方法
- 杂论--永远做最重要的事
- codevs 1332 上白泽慧音
- HDU 3364 Lanterns (高斯消元解开关问题)
- 3dsmax 新手学习记录
- 构建有符号加法器和有符号乘法器的经验
- HanSight DataViewer
- http协议之请求
- 十月英语总结
- web缓存、FTP/SMTP
- cf 15a Cottage Village