Submission #183242


Source Code Expand

var fs = require('fs')
  , _
;

(function() {
  var ls = fs.readFileSync('/dev/stdin', 'utf8').split('\n').slice(0, -1);
  GLOBAL.readline = function() {
    if (ls.length === 0) return false;
    _ = ls.shift();
    return _;
  };
  GLOBAL.readints = function() {
    if (ls.length === 0) return false;
    _ = words(ls.shift()).map(int);
    return _;
  }
}());

log = console.log;
warn = console.warn;
int = function(x) { return x|0 };
float = function(x) { return +x };

// string ->
function words(x) { return x.split(' ') };
function ints(x) { return words(x).map(int) };

// -> number
function add(x,y) { return x+y };
function max(x,y) { return x>y ? x : y };
function min(x,y) { return x<y ? x : y };

// -> [number]
function sort(ls) { return ls.sort(function(x,y) { return x - y }) };
function iota(n,b,s) {
  b = b||0;
  s = s===void 0?1:s;
  var ret = [];
  for (var i=0;i<n;++i) { ret[i] = b + i * s; }
  return ret;
};

var konst = {
  directed: 'directed',
  undirected: 'undirected',
}

function Graph(N) {
  this.type = konst.undirected;
  this.N = N || 0;
  this.E = {};
}

/// BASIC

// add edge !
Graph.prototype.add = function(u, v, capacity) {
  capacity = capacity || 1;
  var that = this;
  a(u,v);
  if (this.type === konst.undirected) {
    a(v,u);
  }
  function a(u,v) {
    if (!(u in that.E)) that.E[u] = {};
    if (!(v in that.E[u])) that.E[u][v] = 0;
    that.E[u][v] += capacity;
  }
};

// predicate whether has edge (u,v) or not
Graph.prototype.has = function(u, v) {
  return u in this.E && v in this.E[u] && this.E[u][v] > 0 };

// make complete graph
konst.complete = function (N) {
  var g = new Graph(N);
  for (var i=0; i<N-1; ++i) {
    for (var j=i+1; j<N; ++j) {
      g.add(i,j) }}
  return g;
};

/// ALGORITHMS

Graph.prototype.scc = scc;
Graph.prototype.maxflow = maxflow;

function scc() {
  var that = this
    , N = that.N
    , rev = graphReverse(that)
    , vs = []
    , used = []
    , cmp = []
  ;

  function graphReverse(g) {
    var r = {};
    r.E = {};
    for (var u in g.E) {
      for (var v in g.E[u]) {
        if (! (v in r.E)) r.E[v] = {};
        r.E[v][u] = g.E[u][v]
      }
    }
    return r;
  }
  function zeroset() {
    for (var i=0;i<N;++i) used[i] = false;
  }

  function dfs(v) {
    used[v] = true;
    for (var u in that.E[v]) {
      if (!used[u]) dfs(u);
    }
    vs.push(v);
  }

  function rdfs(v, k) {
    used[v] = true;
    cmp[v] = k;
    for (var u in rev.E[v]) {
      if (!used[u]) rdfs(u, k);
    }
  }


  zeroset();
  for (var v=0; v < N; ++v) {
    if (!used[v]) {
      dfs(v); }}
  zeroset();
  var k = 0;
  vs.reverse()
    .forEach(function(v) { if (!used[v]) rdfs(v, k++); })
    ;

  return cmp;

}

function maxflow(s, t) {
  s = s | 0;
  t = t | 0;
  var that = this
    , F  = {}
  ;
  for (var u=0; u<that.N; ++u) {
    F[u] = {};
    for (var v=0; v<that.N; ++v) {
      F[u][v] = 0;
    }
  }

  for (var sum=0, f; f=bfs(); sum += f);
  return sum;

  function bfs() {
    var M = {}    // M[v] is max-flow of `s -> v`
      , P = {}  // parent node of flow path
    ;
    M[s] = 1/0;
    P[s] = false;
    for (var q=[s]; q.length>0; ) {
      var u = q.shift();
      for (var v=0; v<that.N; ++v) {
        if (v in M) continue; // have searched
        var capa = that.has(u,v) ? that.E[u][v] : 0
          , rest = capa - F[u][v] + F[v][u]
        ;
        if (rest <= 0) continue; // cannot flow
        M[v] = Math.min(M[u], rest);
        P[v] = u;
        q.push(v);
        if (v === t) return defer();
      }
    }
    return false;

    function defer() {
      var f = M[t];
      // var path = t;
      for (var v=t;; v=P[v]) {
        var u = P[v];
        // path = u+'->'+path;
        F[u][v] += f;
        if (u === s) break;
      }
      // console.warn(f, path);
      return f;
    }
  }
};


readints();
N = _[0];
G = _[1];
E = _[2];

if (G === 0) {
  log(0);
  process.exit(0);
}

ps = readints();

var g = new Graph(N+1);
g.type = 'undirected';

for (i=0;i<E;++i) {
  readints();
  a = _[0];
  b = _[1];
  g.add(a,b);
}

ps.forEach(function(p) {
  g.add(p, N);
});

log(g.maxflow(0, N))

Submission Info

Submission Time
Task D - 浮気予防
User cympfh
Language JavaScript (Node.js 0.6.12)
Score 100
Code Size 4351 Byte
Status AC
Exec Time 790 ms
Memory 15232 KB

Judge Result

Set Name part All
Score / Max Score 99 / 99 1 / 1
Status
AC × 27
AC × 56
Set Name Test Cases
part test_01_AB.txt, test_02_AB.txt, test_03_AB.txt, test_04_AB.txt, test_05_AB.txt, test_06_AB.txt, test_07_AB.txt, test_08_AB.txt, test_09_AB.txt, test_10_AB.txt, test_11_AB.txt, test_12_AB.txt, test_13_AB.txt, test_14_AB.txt, test_15_AB.txt, test_16_AB.txt, test_17_AB.txt, test_18_AB.txt, test_19_AB.txt, test_20_AB.txt, test_21_AB.txt, test_22_AB.txt, test_23_AB.txt, test_24_AB.txt, test_25_AB.txt, test_41_AB.txt, test_47_AB.txt
All sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt, test_01_AB.txt, test_02_AB.txt, test_03_AB.txt, test_04_AB.txt, test_05_AB.txt, test_06_AB.txt, test_07_AB.txt, test_08_AB.txt, test_09_AB.txt, test_10_AB.txt, test_11_AB.txt, test_12_AB.txt, test_13_AB.txt, test_14_AB.txt, test_15_AB.txt, test_16_AB.txt, test_17_AB.txt, test_18_AB.txt, test_19_AB.txt, test_20_AB.txt, test_21_AB.txt, test_22_AB.txt, test_23_AB.txt, test_24_AB.txt, test_25_AB.txt, test_26_A.txt, test_27_A.txt, test_28_A.txt, test_29_A.txt, test_30_A.txt, test_31_A.txt, test_32_A.txt, test_33_A.txt, test_34_A.txt, test_35_A.txt, test_36_A.txt, test_37_A.txt, test_38_A.txt, test_39_A.txt, test_40_A.txt, test_41_AB.txt, test_42_A.txt, test_43_A.txt, test_44_A.txt, test_45_A.txt, test_46_A.txt, test_47_AB.txt, test_48_A.txt, test_49_A.txt, test_50_A.txt, test_51_A.txt
Case Name Status Exec Time Memory
sample_01.txt AC 790 ms 11184 KB
sample_02.txt AC 169 ms 11132 KB
sample_03.txt AC 167 ms 11120 KB
sample_04.txt AC 168 ms 11132 KB
sample_05.txt AC 168 ms 11252 KB
test_01_AB.txt AC 169 ms 11120 KB
test_02_AB.txt AC 169 ms 11124 KB
test_03_AB.txt AC 170 ms 11116 KB
test_04_AB.txt AC 170 ms 11180 KB
test_05_AB.txt AC 168 ms 11508 KB
test_06_AB.txt AC 166 ms 11124 KB
test_07_AB.txt AC 169 ms 11256 KB
test_08_AB.txt AC 169 ms 11116 KB
test_09_AB.txt AC 170 ms 11260 KB
test_10_AB.txt AC 174 ms 11504 KB
test_11_AB.txt AC 169 ms 11252 KB
test_12_AB.txt AC 176 ms 11260 KB
test_13_AB.txt AC 171 ms 11272 KB
test_14_AB.txt AC 171 ms 11360 KB
test_15_AB.txt AC 167 ms 11380 KB
test_16_AB.txt AC 170 ms 11120 KB
test_17_AB.txt AC 168 ms 11356 KB
test_18_AB.txt AC 172 ms 11328 KB
test_19_AB.txt AC 172 ms 11504 KB
test_20_AB.txt AC 172 ms 11192 KB
test_21_AB.txt AC 173 ms 11328 KB
test_22_AB.txt AC 180 ms 11180 KB
test_23_AB.txt AC 169 ms 11244 KB
test_24_AB.txt AC 172 ms 11132 KB
test_25_AB.txt AC 189 ms 11252 KB
test_26_A.txt AC 202 ms 15232 KB
test_27_A.txt AC 207 ms 15092 KB
test_28_A.txt AC 207 ms 15100 KB
test_29_A.txt AC 208 ms 15096 KB
test_30_A.txt AC 201 ms 14968 KB
test_31_A.txt AC 212 ms 15092 KB
test_32_A.txt AC 190 ms 11644 KB
test_33_A.txt AC 178 ms 13548 KB
test_34_A.txt AC 177 ms 11384 KB
test_35_A.txt AC 180 ms 12060 KB
test_36_A.txt AC 176 ms 11716 KB
test_37_A.txt AC 197 ms 13296 KB
test_38_A.txt AC 195 ms 11252 KB
test_39_A.txt AC 183 ms 11256 KB
test_40_A.txt AC 183 ms 11188 KB
test_41_AB.txt AC 172 ms 11132 KB
test_42_A.txt AC 199 ms 13300 KB
test_43_A.txt AC 170 ms 11260 KB
test_44_A.txt AC 181 ms 11888 KB
test_45_A.txt AC 173 ms 11264 KB
test_46_A.txt AC 168 ms 11264 KB
test_47_AB.txt AC 173 ms 11176 KB
test_48_A.txt AC 172 ms 11252 KB
test_49_A.txt AC 176 ms 11244 KB
test_50_A.txt AC 169 ms 11256 KB
test_51_A.txt AC 172 ms 11132 KB