糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > 上白泽慧音——tarjian

上白泽慧音——tarjian

时间:2023-07-14 14:17:24

相关推荐

上白泽慧音——tarjian

题目描述

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

输出样例#1:

31 3 5

说明

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

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

taijan处理有错。

#include<iostream>#include<cstdio >#include<cstring>#include<algorithm>#include<math.h>#include<vector>#include<queue>using namespace std;#define M 50001int n,m,zhan[M],v[M],color[M],head[M],nex[M*10],num[M*10],cnt;int r[M],dfn[M],low[M],tot,top,high[M],maxn;vector<int>lie;void tarjan(int x){dfn[x]=++tot;low[x]=tot; color[x]=tot;high[x]=top;if(!v[x])zhan[++top]=x;v[x]=1; for(int i=head[x];i;i=nex[i]){int o=num[i];if(r[o])r[o]--,tarjan(o);low[x]=min(low[x],low[o]);}if(dfn[x]==low[x]) {if((top-high[x])>maxn){maxn=(top-high[x]);lie.resize(0); while(zhan[top]!=x){int t=zhan[top];lie.push_back(t);color[t]=color[x]; v[t]=0; top--;}lie.push_back(x); }v[x]=0;top--;}}int main(){scanf("%d%d",&n,&m);for(int i=1,a,b,t;i<=m;i++){scanf("%d%d%d",&a,&b,&t);if(t==2){num[++cnt]=b;nex[cnt]=head[a];head[a]=cnt;num[++cnt]=a;nex[cnt]=head[a];head[b]=cnt;r[a]++;r[b]++;}else {num[++cnt]=b;nex[cnt]=head[cnt];head[a]=cnt;r[b]++;}}for(int i=1;i<=n;i++)if(!color[i])tarjan(i);printf("%d\n",lie.size());for(int i=lie.size()-1;i>=0;i--)printf("%d ",lie[i]);return 0;}

#include<iostream>#include<cstdio >#include<cstring>#include<algorithm>#include<math.h>#include<vector>#include<queue>using namespace std;#define M 50001int n,m,zhan[M],v[M],color[M],head[M],nex[M*10],num[M*10],cnt;int r[M],dfn[M],low[M],tot,top,high[M],maxn;vector<int>lie;void tarjan(int x){dfn[x]=++tot;low[x]=tot; color[x]=tot;high[x]=top;if(!v[x])zhan[++top]=x;v[x]=1; for(int i=head[x];i;i=nex[i]){int o=num[i];if(!v[o])tarjan(o);low[x]=min(low[x],low[o]);}if(dfn[x]==low[x]){cout<<endl;while(zhan[top]!=x){int t=zhan[top];printf("%d ",t);color[t]=color[x]; v[t]=0; top--;}printf("%d ",x);v[x]=0;top--;}}int main(){scanf("%d%d",&n,&m);for(int i=1,a,b,t;i<=m;i++){scanf("%d%d%d",&a,&b,&t);if(t==2){num[++cnt]=b;nex[cnt]=head[a];head[a]=cnt;//!!num[++cnt]=a;nex[cnt]=head[b];head[b]=cnt;r[a]++;r[b]++;}else {num[++cnt]=b;nex[cnt]=head[cnt];head[a]=cnt;r[b]++;}} for(int i=1;i<=n;i++)if(!color[i])tarjan(i); return 0;}

如果觉得《上白泽慧音——tarjian》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。