bzoj2208:[Jsoi2010]連通數

來源:互聯網
上載者:User

標籤:

http://blog.csdn.net/u013598409/article/details/47037499 裡面似乎有產生資料的。。。//我本來的想法是tarjan縮點之後然後將圖遍曆一遍就可以了,複雜度應該是O(n2)的,為什麼說這樣是n3的啊。。。//=>這種做法是錯的因為有可能會重複計算用一下傳遞閉包就可以瞭然而直接遞推以前寫的dfsqaq
#include<cstdio>#include<cstring>#include<cctype>#include<algorithm>#include<stack>#include<vector>#include<queue>#include<bitset>using namespace std;#define rep(i,s,t) for(int i=s;i<=t;i++)#define dwn(i,s,t) for(int i=s;i>=t;i--)#define clr(x,c) memset(x,c,sizeof(x))#define qwq(x) for(edge *o=head[x];o;o=o->next)const int nmax=2e3+5;const int maxn=nmax*nmax;const int inf=0x7f7f7f7f;struct edge{    int to;edge *next;}; edge es[maxn],*pt=es,*head[nmax];void add(int u,int v){    pt->to=v;pt->next=head[u];head[u]=pt++;}char ch[nmax];int pre[nmax],scc_cnt,dfs_clock,sm[nmax],scc[nmax];stack<int>s;void mins(int &a,int b){    if(a>b) a=b;}int dfs(int x){    int lowu=pre[x]=++dfs_clock;s.push(x);    qwq(x){        if(!pre[o->to]) mins(lowu,dfs(o->to));        if(!scc[o->to]) mins(lowu,pre[o->to]);    }    if(lowu==pre[x]){        scc_cnt++;int o,cnt=0;        while(1){            o=s.top();s.pop();            scc[o]=scc_cnt;++cnt;            if(o==x) break;        }        sm[scc_cnt]=cnt;    }    return lowu;}bitset<nmax>a[nmax];int in[nmax];vector<int>g[nmax];queue<int>q;int main(){    int n,u,v,d;scanf("%d",&n);    rep(i,1,n){        scanf("%s",ch+1);        rep(j,1,n) if(ch[j]==‘1‘) add(i,j);    }    rep(i,1,n) if(!pre[i]) dfs(i);         rep(i,1,n) {        u=scc[i];        qwq(i) if(u!=(v=scc[o->to])) g[u].push_back(v),++in[v];    }    rep(i,1,scc_cnt) a[i][i]=1;    rep(i,1,scc_cnt) if(!in[i]) q.push(i);    while(!q.empty()){        u=q.front();q.pop();        if(!g[u].size()) continue;        rep(i,0,g[u].size()-1){            v=g[u][i];            a[v]|=a[u]; //a儲存的是可以到達該強聯通分量的             if(!--in[v]) q.push(v);        }    }    int ans=0;    rep(i,1,scc_cnt) rep(j,1,scc_cnt) if(a[i][j]) ans+=sm[i]*sm[j];    printf("%d\n",ans);    return 0;}
2208: [Jsoi2010]連通數Time Limit: 20 Sec  Memory Limit: 512 MB
Submit: 2249  Solved: 965
[Submit][Status][Discuss]
Description

Input

輸入資料第一行是圖頂點的數量,一個正整數N。 接下來N行,每行N個字元。第i行第j列的1表示頂點i到j有邊,0則表示無邊。

Output

輸出一行一個整數,表示該圖的連通數。

Sample Input3
010
001
100
Sample Output9HINT

 

對於100%的資料,N不超過2000。

 

Source

第一輪

[Submit][Status][Discuss]

bzoj2208:[Jsoi2010]連通數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.