上白泽慧音

上白泽慧音

题目描述

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

Solution

tarjan求强连通分量最大是多少并输出其中字典序最小的方案。
一开始理解错题意QwQ
环求出来后要sort处理。

#include<bits/stdc++.h>
#pragma GCC optimize(3)
namespace ZDY{
    #define res register
    #define ri res int
    #define ll long long
    #define db double
    #define sht short
    #define il inline
    #define MB template <class T>
    #define Fur(i,x,y) for(ri i=x;i<=y;i++)
    #define fur(i,x,y) for(i=x;i<=y;i++)
    #define Fdr(i,x,y) for(ri i=x;i>=y;i--)
    #define in2(x,y) in(x),in(y)
    #define in3(x,y,z) in2(x,y),in(z)
    #define in4(a,b,c,d) in2(a,b);in2(c,d)
    #define outn(x) out(x),pc('
')
    #define clr(x,y) memset(x,y,sizeof(x))
    #define cpy(x,y) memcpy(x,y,sizeof(x))
    #define fl(i,x) for(ri i=head[x],to;to=e[i].to,i;i=e[i].nxt)
    #define inf 2147483630
    #define fin(s) freopen(s".in","r",stdin)
    #define fout(s) freopen(s".out","w",stdin)
    #define gt io.gc()
    #define l2(n) (log(n)/log(2))
    MB il T ABS(T x){return x>0?x:-x;}
    MB il T MAX(T x,T y){return x>y?x:y;}
    MB il T MIN(T x,T y){return x<y?x:y;}
    MB il T GCD(T x,T y){return y?GCD(y,x%y):x;}
    MB il void SWAP(T x,T y){T t=x;y=t;x=y;}
}using namespace ZDY;using namespace std;

class IO{
   #define fok (ch!=EOF)
   #define sep (ch==' '||ch=='
'||ch=='	')
   #define dsep !isdigit(ch)
   #define neq(a,b) ((a)-(b)>1e-6)
   char rbuf[1<<20],wbuf[1<<20],b,*p1,*p2;
   int rs,ws,S;
   public:
       IO():p1(rbuf),p2(wbuf),S(1000000),rs(1000000),ws(-1),b(1){}
       ~IO(){fwrite(wbuf,1,ws+1,stdout);}
       il char gc(){return rs==S&&(p1=rbuf,rs=-1,(S=fread(rbuf,1,S+1,stdin)-1)==-1)?(b=0,EOF):(++rs,*p1++);}
       il void pc(int x){ws==1000000&&(p2=wbuf,ws=-1,fwrite(wbuf,1,1000001,stdout)),++ws,*p2++=x;}
       il void puts(const char str[]){fwrite(wbuf,1,ws+1,stdout)?(ws=-1):0,fwrite(str,1,strlen(str),stdout);}
       il void gl(string& s){for(res char ch;(ch=gc())!='
'&&fok;)s+=ch;}
       il IO& operator>>(int& x){x=0;res char f=0,ch=gc();while(dsep&&fok)f|=(ch=='-'),ch=gc();while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=gc();return x=f?-x:x,*this;}
       il IO& operator>>(ll& x){x=0;res char f=0,ch=gc();while(dsep&&fok)f|=(ch=='-'),ch=gc();while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=gc();return x=f?-x:x,*this;}
       il IO& operator>>(char& ch){return ch=gc(),*this;}
       il IO& operator>>(string& s){res char ch=gc();while(sep&&fok)ch=gc();while(!sep&&fok)s+=ch,ch=gc();return *this;}
       il IO& operator>>(double& x){x=0;res char f=0,ch=gc();double d=0.1;while(dsep&&fok)f|=(ch=='-'),ch=gc();while(isdigit(ch))x=x*10+(ch^48),ch=gc();if(ch=='.')while(isdigit(ch=gc()))x+=d*(ch^48),d*=0.1;return x=f?-x:x,*this;}
       il IO& operator<<(int x){res char ch[10],i=-1;!x?(pc('0'),0):0,x<0?(pc('-'),x=-x):0;while(x)ch[++i]=x%10+48,x/=10;while(~i)pc(ch[i]),--i;return *this;}
       il IO& operator<<(ll x){res char ch[20],i=-1;!x?(pc('0'),0):0,x<0?(pc('-'),x=-x):0;while(x)ch[++i]=x%10+48,x/=10;while(~i)pc(ch[i]),--i;return *this;}
       il IO& operator<<(char ch){return pc(ch),*this;}
       il IO& operator<<(char str[]){return puts(str),*this;}
       il IO& operator<<(double x){int y=int(x);*this<<y;x-=y;while(neq(x,int(x)))x*=10;x?*this<<'.'<<int(x):0;return *this;}
       il operator bool(){return b;}
}io;

#define N 5011
int n,m,cnt=0,head[N],v[N],sz=0,dfn[N],low[N],z[N],tp=0,a[N],b[N],ans=0;
struct edge{int nxt,to;}e[100010];
il void add(int x,int y){e[++cnt].to=y;e[cnt].nxt=head[x];head[x]=cnt;}
il bool pd(int d){
    Fur(i,1,d)
    if(a[i]<b[i])return 1;
    else if(a[i]>b[i])return 0;
}
il void tarjan(int x){
    dfn[x]=low[x]=++sz;z[++tp]=x;v[x]=1;
    fl(i,x)
    if(!dfn[to])tarjan(to),low[x]=MIN(low[x],low[to]);
    else if(v[to])low[x]=MIN(low[x],dfn[to]);
    if(low[x]==dfn[x]){
        int d=0;
        while(int k=z[tp--]){
            v[a[++d]=k]=0;
            if(k==x){
                sort(a+1,a+d+1);
                if(d>ans||(d==ans&&pd(d))){ans=d;cpy(b,a);}
                break;
            }
        }
    }
}
int main(){
    io>>n>>m;
    int x,y,p;
    Fur(i,1,m){
    	io>>x>>y>>p;
    	add(x,y);if(p>1)add(y,x);
    }
    Fur(i,1,n)if(!dfn[i])tarjan(i);
    io<<ans<<'
';
    Fur(i,1,ans)io<<b[i]<<' ';
}
原文地址:https://www.cnblogs.com/mimiorz/p/9771994.html