标签:
http://acm.hdu.edu.cn/showproblem.php?pid=2120
ice_cream‘s world is a rich country, it has many fertile lands. Today, the queen of ice_cream wants award land to diligent ACMers. So there are some watchtowers are set up, and wall between watchtowers be build, in order to partition the ice_cream’s world. But how many ACMers at most can be awarded by the queen is a big problem. One wall-surrounded land must be given to only one ACMer and no walls are crossed, if you can help the queen solve this problem, you will be get a land.
In the case, first two integers N, M (N<=1000, M<=10000) is represent the number of watchtower and the number of wall. The watchtower numbered from 0 to N-1. Next following M lines, every line contain two integers A, B mean between A and B has a wall(A and B are distinct). Terminate by end of file.
Output the maximum number of ACMers who will be awarded.
One answer one line.
8 10
0 1
1 2
1 3
2 4
3 4
0 5
5 6
6 7
3 6
4 7
3
用并查集统计环的个数。。
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<vector> #include<map> using std::map; using std::min; using std::find; using std::pair; using std::vector; using std::multimap; #define pb(e) push_back(e) #define sz(c) (int)(c).size() #define mp(a, b) make_pair(a, b) #define all(c) (c).begin(), (c).end() #define iter(c) __typeof((c).begin()) #define cls(arr, val) memset(arr, val, sizeof(arr)) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define rep(i, n) for(int i = 0; i < (int)n; i++) #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i) const int N = 1010; const int INF = 0x3f3f3f3f; struct UinonFind { int ans, par[N], rank[N]; inline void init(int n) { ans = 0; rep(i, n + 1) { par[i] = i; rank[i] = 0; } } inline int find(int x) { while(x != par[x]) { x = par[x] = par[par[x]]; } return x; } inline void unite(int x, int y) { x = find(x), y = find(y); if(x == y) { ans++; return; } if(rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; rank[x] += rank[x] == rank[y]; } } inline void solve(int n, int m) { init(n); int x, y; while(m--) { scanf("%d %d", &x, &y); unite(x, y); } printf("%d\n", ans); } }go; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout); #endif int n, m; while(~scanf("%d %d", &n, &m)) { go.solve(n, m); } return 0; }
标签:
原文地址:http://www.cnblogs.com/GadyPu/p/4792795.html