답안 #491899

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
491899 2021-12-04T23:25:20 Z ivan_tudor Cats or Dogs (JOI18_catdog) C++14
38 / 100
3000 ms 12656 KB
#include "catdog.h"
using namespace std;
const int N = 100005;
vector<int> gr[N];
int par[N];
int root = 1;
int t[N];
int nr[3][N];
int col[N];
int ans;
void dfs_init(int nod, int dad){
  par[nod] = dad;
  for(auto x:gr[nod]){
    if(x == dad)
      continue;
    dfs_init(x, nod);
  }
  t[nod] = nr[0][nod] = nr[1][nod] = nr[2][nod] = 0;
  col[nod] = 0;
}
void initialize(int N, std::vector<int> A, std::vector<int> B){
  for(int i = 0; i < N - 1; i++){
    gr[A[i]].push_back(B[i]);
    gr[B[i]].push_back(A[i]);
  }
  dfs_init(root, 0);
  ans = 0;
};
int other(int tip){
  if(tip == 1)
    return 2;
  if(tip == 2)
    return 1;
  return 1;
}
void update_son(int nod, int oldtson, int newtson){
  if(nod == 0)
    return;
  if(oldtson == newtson)
    return;

  if(col[nod] != 0){
    nr[oldtson][nod] --;
    if(oldtson == other(col[nod]))
      ans--;
    nr[newtson][nod] ++;
    if(newtson == other(col[nod]))
      ans++;
    return;
  }

  int oldtip = t[nod];
  if(oldtip == 0)
    ans -= min(nr[1][nod], nr[2][nod]);
  else
    ans -= nr[other(oldtip)][nod];
  nr[oldtson][nod] --;
  nr[newtson][nod] ++;
  if(nr[1][nod] < nr[2][nod]){
    ans += nr[1][nod];
    t[nod] = 2;
  }
  else if(nr[2][nod] < nr[1][nod]){
    ans += nr[2][nod];
    t[nod] = 1;
  }
  else{
    t[nod] = 0;
    ans += nr[1][nod];
  }
  if(oldtip == t[nod])
    return;
  update_son(par[nod], oldtip, t[nod]);
}
void update(int nod, int cl){
  if(col[nod] == cl)
    return;
  // sterg ce era inainte

  int oldtip = t[nod];
  if(col[nod] == 0)
    ans -= min(nr[1][nod], nr[2][nod]);
  else
    ans -= nr[other(col[nod])][nod];
  col[nod] = cl; // pun culoarea noua
  if(cl != 0){
    t[nod] = cl;
    ans += nr[other(cl)][nod];
  }
  else{
    if(nr[1][nod] < nr[2][nod]){
      ans += nr[1][nod];
      t[nod] = 2;
    }
    else if(nr[2][nod] < nr[1][nod]){
      ans += nr[2][nod];
      t[nod] = 1;
    }
    else{
      t[nod] = 0;
      ans += nr[1][nod];
    }
  }
  if(oldtip == t[nod])
    return;
  update_son(par[nod], oldtip, t[nod]);
}
int cat(int v){
  update(v, 1);
  return ans;
}
int dog(int v){
  update(v, 2);
  return ans;
}
int neighbor(int v){
  update(v, 0);
  return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2640 KB Output is correct
3 Correct 1 ms 2640 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 2 ms 2580 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 2 ms 2632 KB Output is correct
8 Correct 1 ms 2636 KB Output is correct
9 Correct 1 ms 2636 KB Output is correct
10 Correct 1 ms 2636 KB Output is correct
11 Correct 1 ms 2636 KB Output is correct
12 Correct 1 ms 2636 KB Output is correct
13 Correct 1 ms 2636 KB Output is correct
14 Correct 1 ms 2632 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 1 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2640 KB Output is correct
3 Correct 1 ms 2640 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 2 ms 2580 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 2 ms 2632 KB Output is correct
8 Correct 1 ms 2636 KB Output is correct
9 Correct 1 ms 2636 KB Output is correct
10 Correct 1 ms 2636 KB Output is correct
11 Correct 1 ms 2636 KB Output is correct
12 Correct 1 ms 2636 KB Output is correct
13 Correct 1 ms 2636 KB Output is correct
14 Correct 1 ms 2632 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 1 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2640 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 1 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2764 KB Output is correct
29 Correct 2 ms 2648 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 1 ms 2636 KB Output is correct
32 Correct 3 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2640 KB Output is correct
3 Correct 1 ms 2640 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 2 ms 2580 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 2 ms 2632 KB Output is correct
8 Correct 1 ms 2636 KB Output is correct
9 Correct 1 ms 2636 KB Output is correct
10 Correct 1 ms 2636 KB Output is correct
11 Correct 1 ms 2636 KB Output is correct
12 Correct 1 ms 2636 KB Output is correct
13 Correct 1 ms 2636 KB Output is correct
14 Correct 1 ms 2632 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 1 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2640 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 1 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2764 KB Output is correct
29 Correct 2 ms 2648 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 1 ms 2636 KB Output is correct
32 Correct 3 ms 2636 KB Output is correct
33 Correct 52 ms 8060 KB Output is correct
34 Correct 31 ms 8124 KB Output is correct
35 Correct 37 ms 7092 KB Output is correct
36 Correct 68 ms 11704 KB Output is correct
37 Correct 12 ms 5068 KB Output is correct
38 Correct 75 ms 12608 KB Output is correct
39 Correct 71 ms 12492 KB Output is correct
40 Correct 74 ms 12604 KB Output is correct
41 Correct 72 ms 12596 KB Output is correct
42 Correct 83 ms 12656 KB Output is correct
43 Correct 74 ms 12596 KB Output is correct
44 Correct 73 ms 12596 KB Output is correct
45 Correct 77 ms 12528 KB Output is correct
46 Correct 73 ms 12528 KB Output is correct
47 Correct 80 ms 12484 KB Output is correct
48 Correct 51 ms 9868 KB Output is correct
49 Correct 45 ms 11456 KB Output is correct
50 Correct 14 ms 4768 KB Output is correct
51 Correct 20 ms 6180 KB Output is correct
52 Correct 10 ms 4424 KB Output is correct
53 Correct 58 ms 11368 KB Output is correct
54 Correct 30 ms 6656 KB Output is correct
55 Correct 58 ms 10032 KB Output is correct
56 Correct 39 ms 7352 KB Output is correct
57 Correct 78 ms 11064 KB Output is correct
58 Correct 14 ms 6216 KB Output is correct
59 Correct 19 ms 5904 KB Output is correct
60 Correct 43 ms 10672 KB Output is correct
61 Correct 46 ms 11072 KB Output is correct
62 Correct 31 ms 9284 KB Output is correct
63 Correct 1859 ms 8804 KB Output is correct
64 Execution timed out 3070 ms 10052 KB Time limit exceeded
65 Halted 0 ms 0 KB -