ZOJ 3795 Grouping 強聯通縮點+拓撲序+偏序集的最大鏈的大小

來源:互聯網
上載者:User

標籤:

題意:有n個人,m個關係,關係是這兩個人前一個人可以跟後一個比較。那麼問你我最少分多少組可以使這個組裡的人都不可以比較。
只會強聯通縮點,真特麼不知道怎麼做,想了一個小時,網上一看,還要會偏序集的東西,有一個叫Dilworth定理的東西。定理1 令(X,≤)是一個有限偏序集,並令r是其最大鏈的大小。則X可以被劃分成r個但不能再少的反鏈。 
其對偶定理稱為Dilworth定理:
定理2 令(X,≤)是一個有限偏序集,並令m是反鏈的最大的大小。則X可以被劃分成m個但不能再少的鏈。
然後我們用到的是定理1.
也就是說我們直接求可以比較的最大鏈的大小。先強聯通縮點,然後用拓撲序bfs一邊,找到最長的鏈,就ok了。o(︶︿︶)o 唉,感覺就是在考這個定理。

#include<iostream>#include<string.h>#include<stdlib.h>#include<stdio.h>#include<queue>#include<algorithm>using namespace std;const int MAXN = 100010; //點數const int MAXM = 300010; //邊數struct Edge{    int to, next;} edge[MAXM];int head[MAXN],tot;int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN]; //Belong數組的值是1~sccint Index,top;int scc; //強連通分量的個數bool Instack[MAXN];int num[MAXN]; //各個強連通分量包含點的個數,數組編號1~scc//num數組不一定需要,結合實際情況int dis[MAXN];int in[MAXN];vector<int>G[MAXN];void addedge(int u, int v){    edge[tot]. to = v;    edge[tot]. next = head[u];    head[u] = tot++;}void Tarjan( int u){    int v;    Low[u] = DFN[u] = ++Index;    Stack[top++] = u;    Instack[u] = true;    for(int i = head[u]; i != - 1; i = edge[i]. next)    {        v = edge[i]. to;        if( !DFN[v] )        {            Tarjan(v);            if( Low[u] > Low[v] )Low[u] = Low[v];        }        else if(Instack[v] && Low[u] > DFN[v])            Low[u] = DFN[v];    }    if(Low[u] == DFN[u])    {        scc++;        do        {            v = Stack[-- top];            Instack[v] = false;            Belong[v] = scc;            num[scc]++;        }        while( v != u);    }}void solve(int N){    memset(DFN,0, sizeof(DFN));    memset(Instack, false, sizeof(Instack));    memset(num,0,sizeof(num));    Index = scc = top = 0;    for(int i = 1; i <= N; i++)        if(!DFN[i])            Tarjan(i);}void work(){    memset(dis,0,sizeof(dis));    queue<int>q;    for(int i=1;i<=scc;i++){        if(in[i]==0) {q.push(i);dis[i]=num[i];}    }    while(!q.empty()){        int u=q.front();        q.pop();        int v;        for(int i=0;i<G[u].size();i++){            v=G[u][i];            dis[v]=max(dis[u]+num[v],dis[v]);            in[v]--;            if(in[v]==0) q.push(v);        }    }    int ans=0;    for(int i=1;i<=scc;i++){        ans=max(ans,dis[i]);    }    printf("%d\n",ans);}void init(){    tot = 0;    memset(head, -1, sizeof(head));    memset(in,0,sizeof(in));}int main(){    int n,m,a,b;    while(~scanf("%d%d",&n,&m)){        init();        for(int i=1;i<=m;i++){            scanf("%d%d",&a,&b);            addedge(a,b);        }        solve(n);        for(int i=1;i<=scc;i++){            G[i].clear();        }        for(int i=1;i<=n;i++){            for(int j=head[i];j!=-1;j=edge[j].next)            {                int v=edge[j].to;                if(Belong[i]!=Belong[v]){                        in[Belong[v]]++;                        G[Belong[i]].push_back(Belong[v]);                }            }        }        work();    }return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

ZOJ 3795 Grouping 強聯通縮點+拓撲序+偏序集的最大鏈的大小

聯繫我們

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