POJ3041Asteroids【最小點覆蓋】

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   art   

大意:

X.X 
.X. 
.X.     如左圖X代表怪物你可以用雷射去消滅它們,一次可以消滅同一行或同一列的所有怪物,問最少多少次才可以把所有怪物消滅完

 

思路:把x,y座標分別看成左集合與右集合,若兩個座標有聯絡就建一條邊

最後求得是最小點覆蓋

正確性證明:每個怪物的座標都是由一對x,y確定的,在二分圖中每條邊就代表一對座標點,所以只要把所有的邊被覆蓋就可以了

 

代碼:

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <set> 6 using namespace std; 7  8 const int maxn = 505; 9 10 struct Node {11     int to, next;12 }p[10005];13 14 int head[10005];15 16 int tot;17 18 int X[maxn];19 int x_cnt;20 int vis[maxn];21 int link[maxn];22 23 void AddEdge(int u, int v) {24     p[tot].to = v;25     p[tot].next = head[u];26     head[u] = tot ++;27 }28 29 bool Find(int u) {30     for(int i = head[u]; i; i = p[i].next) {31         int v = p[i].to;32         if(!vis[v]) {33             vis[v] = 1;34             if(link[v] == -1 || Find(link[v])) {35                 link[v] = u;36                 return true;37             }38         }39     }40     return false;41 }42 int n;43 44 int solve() {45     int cnt = 0;46     memset(link, -1, sizeof(link));47     memset(vis, 0, sizeof(vis));48     if(Find(X[0])) cnt++;49     for(int i = 1; i < x_cnt; i++) {50         if(X[i] == X[i - 1]) continue;51         memset(vis, 0, sizeof(vis));52         if(Find(X[i])) cnt++;53     }54     return cnt;55 }56 57 int main() {58     int k;59     int u, v;60     //freopen("3041.txt","r",stdin);61     while(EOF != scanf("%d %d",&n, &k)) {62         tot = 1;63         x_cnt = 0;64         memset(head, 0, sizeof(head));65         //memset(X, 0, sizeof(X));66         for(int i = 0; i < k; i++) {67             scanf("%d %d",&u, &v);68             AddEdge(u, v);69             X[x_cnt++] = u;70         }71         sort(X, X + x_cnt);72         printf("%d\n",solve());73     }74     return 0;75 }
View Code

 

聯繫我們

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