B Ponds
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. Each pond has a value v. Now Betty wants to remove some ponds because she does not have enough money. But each time when she removes a pond, she can only remove the ponds which are connected with less than two ponds, or the pond will explode. Note that Betty should keep removing ponds until no more ponds can be removed. After that, please help her calculate the sum of the value for each connected component consisting of a odd number of ponds
Input
The first line of input will contain a number T(1≤T≤30) which is the number of test cases. For each test case, the first line contains two number separated by a blank. One is the number p(1≤p≤104) which represents the number of ponds she owns, and the other is the number m(1≤m≤105) which represents the number of pipes. The next line contains p numbers v1,...,vp, where vi(1≤vi≤108) indicating the value of pond i. Each of the last m lines contain two numbers a and b, which indicates that pond a and pond b are connected by a pipe.
Output
For each test case, output the sum of the value of all connected components consisting of odd number of ponds after removing all the ponds connected with less than two pipes.
Sample Input
1
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7
Sample Output
21
Source
2015 ACM/ICPC Asia Regional Changchun Online
题目类型:拓扑排序删点+DFS
算法分析:先使用链式前向星建图,然后使用拓扑排序的方法将所有不符合的点删去(标记),易知此时的连通分量都是环,然后使用DFS将所有的分量中点的数量是奇数的val和累加起来即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define LL long long #define ULL unsigned long long #define DB(ccc) cout << #ccc << " = " << ccc << endl const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e4 + 66; //分别表示顶点v。w和下一个邻接的位置 struct Node { LL v, w, next; }; Node edge[maxn*10]; LL head[maxn], edgelen;//分别表示顶点的表头节点和边的数量 //将所有的数组均初始化为-1 void Init () { memset (head, -1, sizeof (head)); memset (edge, -1, sizeof (edge)); edgelen = 0; } //使用头插法构建 void AddEdge (LL u, LL v, LL w) { edge[edgelen].v = v; edge[edgelen].w = w; edge[edgelen].next = head[u]; head[u] = edgelen++; } LL val[maxn], deg[maxn], res, temp, cnt; bool vvis[maxn]; void dfs (LL cur) { for (LL i = head[cur]; i != -1; i = edge[i].next) { if (!vvis[edge[i].v] && deg[edge[i].v] >= 2) { temp += val[edge[i].v]; cnt++; vvis[edge[i].v] = true; dfs (edge[i].v); } } } int main() { // CPPFF; LL t; cin >> t; while (t--) { Init (); memset (deg, 0, sizeof (deg)); LL n, m; cin >> n >> m; for (LL i = 1; i <= n; i++) cin>> val[i]; for (LL i = 1; i <= m; i++) { LL u, v; cin >> u >> v; AddEdge (u, v, 1); AddEdge (v, u, 1); deg[u]++; deg[v]++; } queue <LL>qu; for (LL i = 1; i <= n; i++) if (deg[i] < 2) qu.push (i); while (!qu.empty ()) { LL tt = qu.front (); qu.pop (); for (LL i = head[tt]; i != -1; i = edge[i].next) { if (deg[edge[i].v] >= 2) { deg[edge[i].v]--; if (deg[edge[i].v] < 2) qu.push (edge[i].v); } } } res = 0; memset (vvis, false, sizeof (vvis)); for (LL i = 1; i <= n; i++) { if (deg[i] >= 2 && !vvis[i]) { temp = cnt = 0; dfs (i); if (cnt & 1) res += temp; } } cout << res << endl; } return 0; } |
G The Water Problem
Problem Description
In Land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with a1,a2,a3,...,anrepresenting the size of the water source. Given a set of queries each containing 2 integers l and r, please find out the biggest water source between al and ar.
Input
First you are given an integer T(T≤10) indicating the number of test cases. For each test case, there is a number n(0≤n≤1000) on a line representing the number of water sources. n integers follow, respectively a1,a2,a3,...,an, and each integer is in {1,...,106}. On the next line, there is a number q(0≤q≤1000)representing the number of queries. After that, there will be q lines with two integers l and r(1≤l≤r≤n) indicating the range of which you should find out the biggest water source.
Output
For each query, output an integer representing the size of the biggest water source.
Sample Input
3
1
100
1
1 1
5
1 2 3 4 5
5
1 2
1 3
2 4
3 4
3 5
3
1 999999 1
4
1 1
1 2
2 3
3 3
Sample Output
100
2
3
4
4
5
1
999999
999999
1
Source
2015 ACM/ICPC Asia Regional Changchun Online
题目类型:水题
算法分析:数据比较小,所以可以直接暴力枚举
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define LL long long #define ULL unsigned long long #define DB(ccc) cout << #ccc << " = " << ccc << endl const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e3 + 66; int a[maxn]; int main() { // CPPFF; int t; cin >>t; while (t--) { int n; cin >>n; for (int i = 1; i <= n; i++) cin >> a[i]; int q; cin >>q; for (int i = 1; i <= q; i++) { int u, v; cin >>u >>v; int maxval = -INF; for (int j = u; j <= v; j++) maxval = max (maxval, a[j]); cout << maxval << endl; } } return 0; } |
- « 上一篇:BestCoder Round #55(0/4)