上白澤慧音

來源:互聯網
上載者:User

標籤:href   最小   read   line   輸出   string   stdout   class   ref   

上白澤慧音

題目描述

在幻想鄉,上白澤慧音是以知識淵博聞名的老師。春雪異變導致人間之裡的很多道路都被大雪堵塞,使有的學生不能順利地到達慧音所在的村莊。因此慧音決定換一個能夠聚集最多人數的村莊作為新的教學地點。人間之裡由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('\n')    #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=='\n'||ch=='\t')   #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())!='\n'&&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 5011int 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<<'\n';    Fur(i,1,ans)io<<b[i]<<' ';}

上白澤慧音

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.