Description
Very long time. In a distant galaxy, a dark Empire relies on its super-weapons rulers throughout the galaxy. One day, with an occasional chance, a rebel army destroyed the empire's super weapons. And attacked nearly all the planets in the galaxy. These planets are connected to each other directly or indirectly through special etheric tunnels. But it didn't last very fast. The Empire has created his super weapon again.
With the power of this super weapon. The empire began to systematically destroy the rebel-occupied planet. Because the planet is constantly being destroyed, the communication channel between the two planets is also beginning to be unreliable. Now, the rebel leader has given you a mission: to give an Ethernet tunnel connection between the original two planets and the Order of the empire-hit planets. Try to find out the number of connections that the rebel-held planet will have after each strike as quickly as possible. (Suppose that two planets can be connected directly or indirectly through the existing etheric channels.) The two planets are in the same connected block).
Input
The first line of the input file consists of two integers. N (1 <= n <= 2M) and M (1 <= m <= 200,000) represent the number of planets and the number of etheric tunnels, respectively. Planets are numbered with 0~n-1 integers. The next M-line, each line consists of two integers X, Y, in which (0<=x<>y
Output
The first line of the output file is the number of connected blocks of the planet at the beginning. The next n rows. One integer per line. Indicates the number of connected blocks of the existing planet after the strike.
Sample Input8 13
0 1
1 6
6 5
5 0
0 6
1 2
2 3
3 4
4 5
7 1
9 |
7 6
3 6
5
1
6
3
5
7Sample Output1
1
1
2
3
3The puzzle : Obviously we can use and check the relationship between the maintenance of Unicom. Then we save all the operations and do it backwards. Change the cut edge to add edge.
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;int fa[1000001],point[ 1000001],next[1000001],cnt,a[1000001],aa[1000001];int n,m,x,y,ans,q;bool f[1000001];struct use{int st,en;} e[10000001];inline int find (int x) {if (fa[x]!=x) Fa[x]=find (fa[x]); return fa[x];} inline void Add (int x,int y) {next[++cnt]=point[x];p oint[x]=cnt; E[cnt].st=x;e[cnt].en=y;} int main () {scanf ("%d%d", &n,&m); ans=n; memset (F,true,sizeof (f)); for (int i=0;i<=n-1;i++) fa[i]=i; for (int i=1;i<=m;i++) {int r1,r2; scanf ("%d%d", &x,&y); R1=find (x); R2=find (y); if (R1!=R2) {fa[r1]=r2;ans--;} Add (x, y); add (y,x); } cout<<ans<<endl; ANS=N;SCANF ("%d", &q); for (int i=1;i<=q;i++) {scanf ("%d", &x); F[x]=false; A[i]=x; } ans-=q; for (int i=0;i<=n-1;i++) fa[i]=i; for (int i=1;i<=cnt;i++) if (f[e[i].st]&&f[e[i].eN]) {int r1,r2; R1=find (E[i].st); R2=find (E[i].en); if (R1!=R2) {fa[r1]=r2;ans-=1;} } Aa[q]=ans; for (int i=q;i>=1;i--) {int u,r1,r2; Ans+=1; U=a[i]; F[u]=true; for (int j=point[u];j;j=next[j]) {if (F[e[j].st]&&f[e[j].en]) { R1=find (E[j].st); R2=find (E[j].en); if (R1!=R2) {fa[r1]=r2;ans-=1;} }} Aa[i-1]=ans; } for (int i=1;i<=q;i++) printf ("%d\n", Aa[i]);}
"Bzoj1015" "JSOI2008" "Star Wars" "and check set + offline"