HDU 5438 Ponds dfs模拟

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 }
时间: 2024-12-23 08:28:44

HDU 5438 Ponds dfs模拟的相关文章

hdu 5438 Ponds dfs

Time Limit: 1500/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others) Problem Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and there will not be more than one pipe between two ponds. Ea

hdu 5438 Ponds(长春网络赛 拓扑+bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2237    Accepted Submission(s): 707 Problem Description Betty owns a lot of ponds, som

HDU 5438 Ponds (拓扑排序+DFS)2015 ACM/ICPC Asia Regional Changchun Online

[题目链接]:click here~~ [题目大意]: 题意:在一个无向图中有 p 个点, m 条边,每个点有一个值 vi .不断的删去度数小于2的点直到不能删为止.求新图中所有点个数为奇数的连通分量的点值的和. 1<p<10^4,1<m<10^5 [思路]删边考虑类似拓扑排序的写法,不过topsort是循环一遍1到n结点入度为0的结点,然后加入到队列中,这里只要改一下度数小于等于1,最后DFS 判断一下 挫挫的代码: /* * Problem: HDU No.5438 * Run

HDU 5438 Ponds (DFS,并查集)

题意:给定一个图,然后让你把边数为1的结点删除,然后求连通块结点数为奇的权值和. 析:这个题要注意,如果删除一些结点后,又形成了新的边数为1的结点,也应该要删除,这是坑,其他的,先用并查集判一下环,然后再找连通环. 代码如下: #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstrin

hdu 5438 Ponds 拓扑排序

Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1001&cid=621 Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and there will not be more than one

hdu 5438 Ponds 长春网赛1002

5438 好吉利的题号 不停的删掉度数小于等于1的点并更新各点度数,直至无法删点,然后dfs即可 #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; #define ll __int64 const int maxm=100008; const int maxn=10008; struct fuck{ int u,v,w,nex

HDU 5438 Ponds

Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 282    Accepted Submission(s): 86 Problem Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and

hdu 5439 Ponds(长春网络赛——拓扑排序+搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2837    Accepted Submission(s): 891 Problem Description Betty owns a lot of ponds, so

hdu 1175 连连看(模拟循环队列)

连连看 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18149    Accepted Submission(s): 4741 Problem Description "连连看"相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通过一条线连起来(这条