上白泽慧音——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 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

taijan处理有错。

#include<iostream>
#include<cstdio >
#include<cstring>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
using namespace std;
#define  M 50001
int 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
",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 50001
int 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;
}
原文地址:https://www.cnblogs.com/CLGYPYJ/p/6530945.html