Beispiel #1
0
        void Solve()
        {
            io.i(out N, out M);
            aby = new Tuple <int, int, int> [M];
            M.REP(i => {
                int a  = io.Int;
                int b  = io.Int;
                int y  = io.Int;
                aby[i] = Tuple.Create(a, b, y);
            });
            Q  = io.Int;
            vw = new Tuple <int, int> [Q];
            Q.REP(i => {
                int v = io.Int;
                int w = io.Int;
                vw[i] = Tuple.Create(v, w);
            });
            aby = aby.OrderByDescending(t => t.Item3).ToArray();
            var vwi = vw.Select((t, i) => new { t, i }).
                      OrderByDescending(a => a.t.Item2).ToArray();

            var ans = new int[Q];
            var uf  = new UnionFind();

            uf.Init(N);

            var vwindex = 0;

            for (int i = 0; i < M && vwindex < Q; ++i)
            {
                var curYear = aby[i].Item3;
                while (vwindex < Q && curYear <= vwi[vwindex].t.Item2)
                {
                    ans[vwi[vwindex].i] = uf.Size(vwi[vwindex].t.Item1);
                    vwindex++;
                }
                uf.Unite(aby[i].Item1, aby[i].Item2);
            }
            while (vwindex < Q)
            {
                ans[vwi[vwindex].i] = uf.Size(vwi[vwindex].t.Item1);
                vwindex++;
            }

            io.o(ans);
        }
Beispiel #2
0
    class Kruskal : ShortestPath {//TODO:test,
        public long Run()
        {
            var L = Adj.Select((l, i) => Tuple.Create(i, l[i].Item1, l[i].Item2)).OrderBy(t => t.Item3).ToList();
            //var li = new List<Tuple<int, int, long>>();
            //I.REP(i =>{ foreach (var t in Adj[i]) li.Add(Tuple.Create(i, t.Item1, t.Item2));});
            //li = li.OrderBy(t => t.Item3).ToList();//li = li.OrderByDescending(t => t.Item3).ToList();
            var uf = new UnionFind(); uf.Init(V); long ret = 0;

            foreach (var t in L)
            {
                if (!uf.Same(t.Item1, t.Item2))
                {
                    ret += t.Item3; uf.Unite(t.Item1, t.Item2);
                }
            }
            return(ret);
        }
Beispiel #3
0
    class Kruskal : ShortestPath {//TODO:test
        public long Run()
        {
            var li = new List <Tuple <int, int, long> >();

            I.REP(i => { foreach (var t in Adj[i])
                         {
                             li.Add(Tuple.Create(i, t.Item1, t.Item2));
                         }
                  });
            li = li.OrderBy(t => t.Item3).ToList();
            var uf = new UnionFind(); uf.Init(V); long ret = 0;

            foreach (var t in li)
            {
                if (!uf.Same(t.Item1, t.Item2))
                {
                    ret += t.Item3; uf.Unite(t.Item1, t.Item2);
                }
            }
            return(ret);
        }
Beispiel #4
0
        public long Run()
        {
            var uf = new UnionFind(); var ret = 0L; var L = new List <Tuple <int, int, long> >(); uf.Init(V);

            for (int i = 0; i < I; ++i)
            {
                for (int j = 0; j < Adj[i].Count; ++i)
                {
                    L.Add(Tuple.Create(i, Adj[i][j].to, Adj[i][j].cost));
                }
            }
            L = L.OrderBy(t => t.Item3).ToList();
            for (int i = 0; i < L.Count; ++i)
            {
                if (!uf.Find(L[i].Item1, L[i].Item2))
                {
                    ret += L[i].Item3; uf.Unite(L[i].Item1, L[i].Item2);
                }
            }
            return(ret);
        }