public static void Main(string[] args)
        {
            string[]      s = Console.ReadLine().Split(' ');
            int           N = int.Parse(s[0]);
            int           M = int.Parse(s[1]);
            List <Bridge> l = new List <Bridge>();

            for (int i = 0; i < M; i++)
            {
                s = Console.ReadLine().Split(' ');
                l.Add(new Bridge(int.Parse(s[0]), int.Parse(s[1])));
            }

            long        lastNum = (long)N * (long)(N - 1) / 2;
            List <long> reslist = new List <long>();

            reslist.Add(lastNum);

            UnionFind uf = new UnionFind(N + 1);

            for (int i = M - 1; i > 0; i--)
            {
                int anum = uf.Size(l[i].A), bnum = uf.Size(l[i].B);
                lastNum = (uf.Same(l[i].A, l[i].B))? lastNum:
                          lastNum - (long)anum * (long)bnum;
                uf.Unite(l[i].A, l[i].B);
                reslist.Add(lastNum);
            }

            for (int i = reslist.Count - 1; i >= 0; i--)
            {
                Console.WriteLine(reslist[i]);
            }
        }
示例#2
0
        public UnionFind <T> Root()
        {
            if (isRoot)
            {
                return(this);
            }
            var root = Parent.Root();

            Parent = root;
            return(root);
        }
示例#3
0
        public static bool Connect(UnionFind <T> a, UnionFind <T> b)
        {
            var aRoot = a.Root();
            var bRoot = b.Root();

            if (aRoot == bRoot)
            {
                return(false);
            }
            aRoot.isRoot = false;
            aRoot.Parent = bRoot;
            bRoot.size  += aRoot.size;
            return(true);
        }
示例#4
0
        static void Solve()
        {
            int n, m;

            ReadMulti(out n, out m);

            var a = new int[m];
            var b = new int[m];

            for (int i = 0; i < m; i++)
            {
                ReadMulti(out a[i], out b[i]);
                a[i]--;
                b[i]--;
            }

            var ans = new long[m];

            ans[m - 1] = (long)n * (n - 1) / 2;
            UnionFind uni = new UnionFind(n);

            for (int i = m - 1; i >= 1; i--)
            {
                ans[i - 1] = ans[i];

                long g1  = uni.Data[uni.Root(a[i])];
                long g2  = uni.Data[uni.Root(b[i])];
                long dif = g1 * g2;
                if (uni.Connect(a[i], b[i]))
                {
                    ans[i - 1] -= dif;
                }
            }

            foreach (var item in ans)
            {
                WriteAnswer(item);
            }
        }