BZOJ 2208 JSOI 2010 連通數 Tarjan+bitset

來源:互聯網
上載者:User

標籤:bzoj   jsoi 2010   tarjan   拓撲排序   

題目大意:給出一張有向圖,若一個點能夠到達另一個點,那麼說這兩個點是一對聯通點。問圖中共有多少聯通點。


思路:先進行一次Tarjan,求出所有的scc,對於一個scc中的點,對答案的貢獻就是cnt^2,不同的scc組成了一張可拓撲圖,然後對於每個scc維護一個bitset,來統計他自己和標號比它小的scc中共有多少個不同的點。然後進行dp,其中不停的或就可以了。

第一次使用bitset


CODE:


#include <bitset>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAXP 2010#define MAX 4000010using namespace std; int points;int head[MAXP],total;int next[MAX],aim[MAX]; inline void Add(int x,int y){    next[++total] = head[x];    aim[total] = y;    head[x] = total;} int dfn[MAXP],low[MAXP],_clock;int stack[MAXP],top;bool in_stack[MAXP];int changed[MAXP],scc,num[MAXP];bitset<MAXP> have[MAXP]; void Tarjan(int x){    dfn[x] = low[x] = ++_clock;    stack[++top] = x;    in_stack[x] = true;    for(int i = head[x]; i; i = next[i]) {        if(!dfn[aim[i]])            Tarjan(aim[i]),low[x] = min(low[x],low[aim[i]]);        else if(in_stack[aim[i]])            low[x] = min(low[x],dfn[aim[i]]);    }    if(dfn[x] == low[x]) {        int temp;        ++scc;        do {            temp = stack[top--];            in_stack[temp] = false;            changed[temp] = scc;            ++num[scc];        }while(temp != x);    }} int main(){    cin >> points;    for(int i = 1; i <= points; ++i)        for(int x,j = 1; j <= points; ++j) {            scanf("%1d",&x);            if(x)   Add(i,j);        }    for(int i = 1; i <= points; ++i)        if(!dfn[i]) Tarjan(i);    for(int i = 1; i <= points; ++i)        have[changed[i]][i] = true;    int ans = 0;    for(int i = 1; i <= scc; ++i) {        ans += num[i] * num[i];        bitset<MAXP> temp;        for(int x = 1; x <= points; ++x)            if(changed[x] == i)                for(int j = head[x]; j; j = next[j])                    if(changed[aim[j]] != i)                        temp |= have[changed[aim[j]]];        ans += num[i] * temp.count();        have[i] |= temp;    }    cout << ans << endl;    return 0;}


BZOJ 2208 JSOI 2010 連通數 Tarjan+bitset

聯繫我們

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