标签:
2015 ACM/ICPC Asia Regional Changchun Online
题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和.
思路:两个dfs模拟就行了
1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #include <string> 10 #include <cstring> 11 #include <map> 12 #include <stack> 13 #include <set> 14 #define LL long long 15 #define eps 1e-8 16 #define INF 0x3f3f3f3f 17 #define MAXN 10005 18 using namespace std; 19 vector<int> G[MAXN]; 20 struct Node{ 21 int pos, du; 22 Node(int pos = 0, int du = 0):pos(pos), du(du){}; 23 }; 24 bool compare(Node x, Node y){ 25 return x.du < y.du; 26 } 27 int find(int x){ 28 if(father[x] == x) return x; 29 father[x] = find(father[x]); 30 return father[x]; 31 } 32 Node node[MAXN]; 33 int main() 34 { 35 #ifndef ONLINE_JUDGE 36 freopen("in.txt", "r", stdin); 37 //freopen("out.txt", "w", stdout); 38 #endif // OPEN_FILE 39 int T; 40 int p, m; 41 while (T--){ 42 scanf("%d%d", &p, &m); 43 for (int i = 1; i <= p; i++){ 44 scanf("%d", &v[i]); 45 } 46 int x, y; 47 for (int i = 1; i <= m; i++){ 48 scanf("%d%d", &x, &y); 49 du[x]++; 50 du[y]++; 51 G[x].push_back(y); 52 G[y].push_back(x); 53 } 54 for(int i = 1; i <= n; i++){ 55 node[i] = Node(i, du[i]); 56 } 57 sort(node + 1, node + n + 1, compare); 58 for(int i = 1; i <= n; i++){ 59 int x = node[i].pos; 60 if(du[x] > 1) continue; 61 rm[x] = true; 62 for(int i = 0; i < G[x].size(); i++){ 63 du[G[x][i]]--; 64 } 65 } 66 for(int i = 1; i <= n; i++){ 67 if(rm[i]) continue; 68 x = find(i); 69 for(int j = 0; j < G[i].size(); j++){ 70 int y = G[x][j]; 71 if(rm[y]) continue; 72 y = find(y); 73 if(x == y) continue; 74 father[x] = y; 75 } 76 } 77 memset(cnt, 0, sizeof(cnt)); 78 for(int i = 1; i <= n; i++){ 79 cnt[father[i]]++; 80 } 81 for(int i = 1; i <= n; i++){ 82 if(cnt[father[i]] & 1){ 83 ans += v[i]; 84 } 85 } 86 printf("%d\n", ans); 87 } 88 }
标签:
原文地址:http://www.cnblogs.com/macinchang/p/4804940.html