bzoj 1098 [POI2007] 辦公樓 biu

來源:互聯網
上載者:User

標籤:png   操作   pre   nbsp   namespace   互連   temp   時間   printf   

# 解題思路

畫畫圖可以發現,只要是兩個點之間沒有相互連邊,那麼就必須將這兩個人安排到同一個辦公樓內,:

那,我們可以建立補圖,就是先建一張完全圖,然後把題目中給出的邊都刪掉,這就是一張補圖,顯然補圖中相互連邊的點就放在同一棟辦公樓內。

我們可以用並查集來完成,但是資料範圍顯然不允許用這樣的方法,建圖的複雜度是 $N^2$ 的。所以考慮另一種方法:

將原圖建立好,在原圖中,從一個點開始,把這個點所能夠直接到達的點標記出來,這些點是不可以放在一起的。然後將這些點刪除。

之後對每一個點都進行這樣的操作,那麼之後要刪除的點都要滿足既沒有被刪除也沒有被標記。這樣做下來的複雜度還是 $N^2$ 的。再來想想如何最佳化,我們如果在刪點的時候,不去枚舉那些已經被刪除的點。那所有的刪點的操作總時間複雜度是 $M$ 的,因為每個邊都要只遍曆一次。如何最佳化?鏈表啊。。。

 

# 附上代碼
#include <algorithm>#include <iostream>#include <cstring>#include <cstdio>#include <queue>using namespace std;template <typename T> inline void read(T &x) {    x = 0; T f = 1; char c = getchar();    while (c < ‘0‘ || c > ‘9‘) {if(c == ‘-‘) f = -1; c = getchar();}    while (c <= ‘9‘ && c >= ‘0‘) {x = x*10 + c-‘0‘; c = getchar();}    x *= f;}const int maxn = 4e6+3;int n, m, head[maxn], cnt, ans, pre[maxn], sur[maxn], num[maxn];bool vis[maxn], del[maxn];struct edge {int nxt, to;}ed[maxn];inline void addedge(int x, int y) {    ed[++cnt].nxt = head[x], head[x] = cnt, ed[cnt].to = y;}inline void DEL(int x) {    sur[pre[x]] = sur[x];    pre[sur[x]] = pre[x];    del[x] = 1;}inline void BFS(int u) {    queue<int> Q;    Q.push(u), vis[u] = 1;    while (!Q.empty()) {        int now = Q.front();        Q.pop();        num[ans] ++;        for(int i=head[now]; i; i=ed[i].nxt)            vis[ed[i].to] = 1;        for(int i=sur[0]; i<=n; i=sur[i])            if(!vis[i] && !del[i]) Q.push(i), DEL(i);        for(int i=head[now]; i; i=ed[i].nxt)            vis[ed[i].to] = 0;    }}int main() {    read(n), read(m);    int u, v;    for(int i=1; i<=m; i++) {        read(u), read(v);        addedge(u, v), addedge(v, u);    }    for(int i=0; i<=n; i++)        pre[i] = i-1, sur[i] = i+1;    for(int i=1; i<=n; i++)        if(!del[i]) del[i] = 1, ans ++, BFS(i), DEL(i);    sort(num+1, num+1+ans);    printf("%d\n", ans);    for(int i=1; i<=ans; i++) printf("%d ", num[i]);    return 0;}

 

bzoj 1098 [POI2007] 辦公樓 biu

聯繫我們

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