poj 2594Treasure Exploration(有向圖路徑可相交的最小路徑覆蓋)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   2014   

 
1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdio> 5 #define N 505 6 using namespace std; 7 8 int g[N][N]; 9 int n, m;10 int vis[N], linker[N];11 bool dfs(int u){12 for(int i=1; i<=n; ++i)13 if(g[u][i] && !vis[i]){14 vis[i]=1;15 if(!linker[i] || dfs(linker[i])){16 linker[i]=u;17 return true; 18 }19 }20 return false;21 }22 23 int main(){24 while(scanf("%d%d", &n, &m) && (n||m)){25 memset(g, 0, sizeof(g));26 while(m--){27 int u, v;28 scanf("%d%d", &u, &v);29 g[u][v]=1; 30 } 31 for(int k=1; k<=n; ++k)32 for(int i=1; i<=n; ++i)33 for(int j=1; j<=n; ++j)34 if(!g[i][j])35 g[i][j]=g[i][k]&&g[k][j];36 memset(linker, 0, sizeof(linker));37 int ans=0;38 for(int i=1; i<=n; ++i){39 memset(vis, 0, sizeof(vis));40 if(dfs(i)) ++ans; 41 }42 printf("%d\n", n-ans);43 }44 return 0; 45 }

 

聯繫我們

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