Submission #1754689


Source Code Expand

Copy
import std.stdio;
import std.conv;
import std.string;
import std.format;
import std.algorithm;

import std.range : iota;
import std.array;

import std.container;
import std.typetuple, std.typecons;

class UnionFind {

  int[] id;
  int[] rank;
  int[] countNodes;

  this(int n) {
    id           = n.iota.array;
    rank         = new int[n];
    rank[]       = 0;
    countNodes   = new int[n];
    countNodes[] = 1;
  }

  int findSet(int i) {
    //writef("findSet: int i = %d, int[] id = %s\n", i, id.to!string);
    if (i != id[i]) {
      id[i] = findSet(id[i]);
    }
    return id[i];
  }

  // find, check if p and q has the same root
  bool isSame(int p, int q) {
    int rootP = findSet(p);
    int rootQ = findSet(q);
    //writef("isSame: int p = %d, int q = %d\n", p, q);
    //writef(" => rootP = %d, rootQ = %d\n", rootP, rootQ);
    return rootP == rootQ;
  }

  // union, to merge components containing p and q
  // set id of p's root to the id of q's root
  void unite(int p, int q) {
    //writef("unite: int p = %d, int q = %d\n", p, q);
    int i = findSet(p);
    int j = findSet(q);

    if (i == j) {
      return;
    }

    if (countNodes[i] > countNodes[j]) {
      countNodes[i] = countNodes[j] + countNodes[i];
      id[j] = i;
    } else {
      countNodes[j] = countNodes[j] + countNodes[i];
      id[i] = j;
    }
  }
}

void stringsTo(string, T...)(string str, ref T t) {
  auto s = str.split();
  assert(s.length == t.length);
  foreach(ref ti; t) {
    ti = s[0].to!(typeof(ti));
    s = s[1..$];
  }
}

alias Edge   = Tuple!(int, "index", int, "a", int, "b", int, "y");
alias Person = Tuple!(int, "index", int, "v", int, "w", int, "count");

void main() {
  string lines;
  string buf;
  while (!stdin.eof) {
   buf = stdin.readln();
   lines ~= buf;
  }

//  string lines = q"[4 5
//1 2 10
//1 2 1000
//2 3 10000
//2 3 100000
//3 1 200000
//4
//1 0
//2 10000
//3 100000
//4 0]";

  string[] array = splitLines(lines);

  int N,M;
  stringsTo(array[0], N, M);

  int Q = array[M+1].to!int;
  auto edges   = new BinaryHeap!(Array!Edge, "a.y < b.y")();
  auto persons = new BinaryHeap!(Array!Person, "a.w < b.w")();

  foreach (int i, string s ; array[1..M+1]) {
    int a,b,y;
    stringsTo(s, a, b, y);
    edges.insert(Edge(i, a, b, y));
  }

  foreach (int i, string s ; array[M+2..M+2+Q]) {
    int v,w;
    stringsTo(s, v, w);
    persons.insert(Person(i, v, w, -1));
  }

  auto uf   = new UnionFind(N);
  int[] ans = new int[Q];
  ans[]     = 1;

  while (!persons.empty) {
    Person p = persons.front();

    while(!edges.empty && edges.front.y > p.w) {
      auto e = edges.front();
      int max = max(e.a, e.b) - 1;
      int min = min(e.a, e.b) - 1;
      uf.unite(min, max);
      edges.removeFront();
      if (edges.empty()) {break;}
    }
    // union-by-size
    int count    = uf.countNodes[uf.findSet(p.v-1)];
    ans[p.index] = count;
    persons.removeFront();
    //writef("check count %d\n", p.count);
  }
  ans.each!(a => writeln(a));
}

Submission Info

Submission Time
Task D - 道路の老朽化対策について
User hiroyuking
Language D (DMD64 v2.070.1)
Score 100
Code Size 3140 Byte
Status
Exec Time 639 ms
Memory 29404 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample_01.txt, sample_02.txt, sample_03.txt
Subtask1 50 / 50 sample_01.txt, sample_02.txt, sample_03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt
All 50 / 50 sample_01.txt, sample_02.txt, sample_03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_11.txt, subtask2_12.txt
Case Name Status Exec Time Memory
sample_01.txt 1 ms 256 KB
sample_02.txt 1 ms 256 KB
sample_03.txt 1 ms 256 KB
subtask1_01.txt 6 ms 764 KB
subtask1_02.txt 6 ms 2684 KB
subtask1_03.txt 6 ms 764 KB
subtask1_04.txt 4 ms 636 KB
subtask1_05.txt 6 ms 764 KB
subtask1_06.txt 6 ms 764 KB
subtask1_07.txt 6 ms 764 KB
subtask2_01.txt 600 ms 17216 KB
subtask2_02.txt 617 ms 22592 KB
subtask2_03.txt 628 ms 29404 KB
subtask2_04.txt 639 ms 29052 KB
subtask2_05.txt 444 ms 16064 KB
subtask2_06.txt 582 ms 17600 KB
subtask2_07.txt 602 ms 17216 KB
subtask2_08.txt 615 ms 17216 KB
subtask2_09.txt 634 ms 29180 KB
subtask2_10.txt 634 ms 27900 KB
subtask2_11.txt 638 ms 28156 KB
subtask2_12.txt 629 ms 28540 KB