예제 #1
0
        public void Solve()
        {
            @in(out N, out M);
            @in(M, out A, out B);

            UnionFind uf = new UnionFind(N + 2);

            long[] ans = new long[M + 1];
            ans[M] = N * (N - 1) / 2;
            for (int i = (int)M - 1; i >= 0; i--)
            {
                if (uf.Root(A[i]) == uf.Root(B[i]))
                {
                    ans[i] = ans[i + 1];
                    continue;
                }

                ans[i] = ans[i + 1] - uf.Size(A[i]) * uf.Size(B[i]);
                uf.Unite(A[i], B[i]);
            }

            for (int i = 1; i <= M; i++)
            {
                Console.WriteLine(ans[i]);
            }
        }
예제 #2
0
파일: 4937589.cs 프로젝트: qifanyyy/CLCDSA
        public static void Main(string[] args)
        {
            nm = Array.ConvertAll(Console.ReadLine().Split().ToArray(), int.Parse);
            var ablst = new List <int[]>();

            for (int i = 0; i < nm[1]; i++)
            {
                int[] ab = Array.ConvertAll(Console.ReadLine().Split().ToArray(), int.Parse);
                ablst.Add(ab);
            }

            UnionFind uni = new UnionFind(nm[0]);

            long fuben  = ((long)nm[0] * ((long)nm[0] - 1)) / 2;
            var  anslst = new List <long>();

            for (int i = ablst.Count - 1; i >= 0; i--)
            {
                anslst.Add(fuben);
                int  a     = ablst[i][0] - 1;
                int  b     = ablst[i][1] - 1;
                long asize = uni.Size(a);
                long bsize = uni.Size(b);
                if (uni.Connect(a, b))
                {
                    //Console.WriteLine("a " + a + " b " + b +" size(a) : " + uni.Size(a) + " size(b) : " + uni.Size(b));
                    fuben -= asize * bsize;
                }
            }

            anslst.Reverse();
            anslst.ForEach(Console.WriteLine);
        }