标签:
Soda has a bipartite graph with n vertices and m undirected edges. Now he wants to make the graph become a complete bipartite graph with most edges by adding some extra edges. Soda needs you to tell him the maximum number of edges he can add.
Note: There must be at most one edge between any pair of vertices both in the new graph and old graph.
/* 大意:找到最长的上升序列(要求连在一起) DP思想 dp[u] += dp[v] 从最长的开始找上升 */ #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; int n; int b[500010]; vector<int> G[500010]; int dp[500010]; struct edge{ int num, id; }a[500010]; bool cmp(edge i, edge j) { return i.num < j.num; } int main() { int x, y; while(~scanf("%d", &n)){ for(int i = 1; i < n ; i++) G[i].clear(); for(int i = 1; i <= n ; i++){ scanf("%d", &a[i].num); a[i].id = i; } for(int i = 1; i <= n; i++) b[i] = a[i].num; sort(a + 1, a + n + 1,cmp); for(int i = 1; i < n ; i++){ scanf("%d%d", &x, &y); G[y].push_back(x); G[x].push_back(y); } int max1 = 1; memset(dp, 0, sizeof(dp)); for(int i = n ; i >= 1; i--){ int u = a[i].id; dp[u] = 1; for(int j = 0 ; j < G[u].size(); j++){ int v = G[u][j]; if(b[v] > b[u]) { dp[u] += dp[v]; // printf("%d\n", dp[u]); } } max1 = max(dp[u], max1); } printf("%d\n", max1); } return 0; }
HDU5313——DP+vector——Bipartite Graph
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4694185.html