POJ,北大poj

來源:互聯網
上載者:User

POJ,北大poj

題目大意:有n個點,m條單向線段。現在問要從幾個點出發才能遍曆到所有的點

解題思路:二分圖最大匹配,只要一條匹配,就表示兩個點聯通,兩個點聯通只需要選取其中一個點即可,所以有多少條匹配,就可以減去多少個點

#include<cstdio>#include<cstring>using namespace std;const int N = 130;int g[N][N], vis[N], link[N];int n, m;void init() {    memset(g, 0, sizeof(g));    memset(link, 0, sizeof(link));    int x, y;    for(int i = 0; i < m; i++) {        scanf("%d%d", &x, &y);        g[x][y] = 1;    }}bool dfs(int u) {    for(int i = 1; i <= n; i++) {        if(!vis[i] && g[u][i]) {            vis[i] = 1;            if(!link[i] || dfs(link[i])) {                link[i] = u;                return true;            }        }    }    return false;}void hungary() {    int ans = 0;    for(int i = 1; i <= n; i++) {        memset(vis, 0, sizeof(vis));        if(dfs(i))            ans++;    }    printf("%d\n", n - ans);}int main() {    int test;    scanf("%d", &test);    while(test--) {        scanf("%d%d", &n, &m);        init();        hungary();    }    return 0;}

聯繫我們

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