csu 1601 War(并查集)
来源:互联网 发布:学日语的软件 编辑:程序博客网 时间:2024/05/18 17:02
1601: War
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 82 Solved: 24
[Submit][Status][Web Board]
Description
AME decided to destroy CH’s country. In CH’ country, There are N villages, which are numbered from 1 to N. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. To defend the country from the attack of AME, CH has decided to build some roads between some villages. Let us say that two villages belong to the same garrison area if they are connected.
Now AME has already worked out the overall plan including which road and in which order would be attacked and destroyed. CH wants to know the number of garrison areas in his country after each of AME’s attack.
Input
The first line contains two integers N and M — the number of villages and roads, (2 ≤ N ≤ 100000; 1 ≤ M ≤ 100000). Each of the next M lines contains two different integers u, v (1<=u, v<=N)—which means there is a road between u and v. The next line contains an integer Q which denotes the quantity of roads AME wants to destroy (1 ≤ Q ≤ M). The last line contains a series of numbers each of which denoting a road as its order of appearance — different integers separated by spaces.
Output
Output Q integers — the number of garrison areas in CH’s country after each of AME's attack. Each pair of numbers are separated by a single space.
Sample Input
3 11 2114 41 22 31 33 432 4 3
Sample Output
31 2 3
使用并查集来记录 这些点是否处于同一个联通分量
#include<cstdio>#include<cstdlib>#include<iostream>#include<cstring>#include<cmath>#include<algorithm>#include<queue>#include<vector>#include<set>#include<stack>#include<map>using namespace std;#define REP(x, y) for(int x = 0; x < y; x++)#define DWN(x, y) for(int x = y-1; x > -1; x--)const int maxn=100010;int num;int fa[maxn]; int find_fa(int x){ if(fa[x] == -1) return x; else return fa[x] = find_fa(fa[x]);} void Union(int a, int b){ int ba = find_fa(a); int bb=find_fa(b); if(ba!=bb) { fa[ba]=bb; num--; }} int ea[maxn],eb[maxn];int t[maxn];int vis[maxn]; int main(){// freopen("B.in", "r", stdin); int n,m; while(scanf("%d%d",&n,&m)!=EOF) { num=n; memset(vis,0,sizeof vis); memset(fa,-1,sizeof fa); for(int i=1;i<=m;i++) { scanf("%d%d",&ea[i],&eb[i]); } int q; scanf("%d",&q); int cnt=0; for(int i=0;i<q;i++) { int z; scanf("%d",&z); vis[z]=1; t[cnt++]=z; }// cout<<"cnt "<<cnt<<endl; for(int i=1;i<=m;i++) { if(!vis[i]) Union(ea[i],eb[i]); } stack<int> sta; for(int i=q-1;i>=0;i--) { int z=t[i]; sta.push(num); Union(ea[z],eb[z]);// cout<<"num "<<num<<endl; } while(!sta.empty()) { int z=sta.top(); sta.pop(); printf("%d",z); if(!sta.empty()) printf(" "); else puts(""); } } return 0;}
- csu 1601 War(并查集)
- CSU 1601 War (并查集)
- CSU 1601(并查集)
- UVa10158 War(并查集)
- uva 10158 - War(并查集)
- UVa10158 War(并查集)
- UVA 10158 War(并查集)
- UVA 10158 War (并查集)
- uva 10158 War (并查集)
- UVA---10158-War(并查集)
- uva10158(War)并查集
- 【】Star War 并查集
- UVA10158 - War(并查集)
- CSU1601: War(并查集)
- CSU1601: War(并查集)
- CSU 1326: The contest(并查集+分组背包)
- CSU 1045: 并查集(带权并查集)
- CSU 1601: War
- WEB端应该使用DataTable/DataSet吗?
- Android之ActionBar、Tabs、Fragment、ViewPager实现标签页切换并缓存页面
- 文章标题
- Android代码优化技术
- android数据库操作之cursor
- csu 1601 War(并查集)
- 枚举数组
- 哈哈哈了啊
- Path类和File类
- ASIHTTPRequest 详解, http 请求终结者
- cocos2d-x 3.4之排行榜的实现
- 曾做过一个IOS仿糗事百科的项目
- 关于onNewIntent的使用&PendingIntent flag赋值
- input框中加入小图标