Submission #291994

# Submission time Handle Problem Language Result Execution time Memory
291994 2020-09-06T07:09:48 Z AlexLuchianov Simurgh (IOI17_simurgh) C++14
100 / 100
386 ms 5624 KB
#include "simurgh.h"
#include <cassert>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <vector>
#include <iostream>

class Dsu{
public:
  std::vector<int> mult;
  Dsu(int n) {
    mult.resize(n);
    for(int i = 0; i < n; i++)
      mult[i] = i;
  }
  int jump(int gala) {
    if(gala != mult[gala])
      mult[gala] = jump(mult[gala]);
    return mult[gala];
  }
  bool connect(int gala, int galb) {
    return jump(gala) == jump(galb);
  }
  void unite(int gala, int galb) {
    gala = jump(gala);
    galb = jump(galb);
    if(gala == galb)
      return ;
    mult[gala] = galb;
  }
};

std::vector<std::pair<int,int>> edges;

std::vector<int> get_skeleton(int n) {
  Dsu dsu(n);
  std::vector<int> skeleton;

  for(int i = 0; i < edges.size(); i++) {
    int x = edges[i].first;
    int y = edges[i].second;
    if(dsu.connect(x, y) == 0) {
      dsu.unite(x, y);
      skeleton.push_back(i);
    }
  }
  return skeleton;
}
std::vector<int> level, far, state;
std::vector<std::vector<int>> g;

void dfs(int node, int parent) {
  for(int h = 0; h < g[node].size(); h++) {
    int id = g[node][h];
    int to = edges[id].first + edges[id].second - node;
    if(to != parent) {
      level[to] = level[node] + 1;
      far[to] = id;
      dfs(to, node);
    }
  }
}

void mark_skeleton(std::vector<int> &skeleton, int n) {
  g.resize(n);
  for(int i = 0; i < skeleton.size(); i++) {
    int id = skeleton[i];
    g[edges[id].first].push_back(id);
    g[edges[id].second].push_back(id);
  }
  dfs(0, -1); 
}

std::vector<int> with(std::vector<int> base, int val) {
  base.push_back(val);
  return base;
}

std::vector<int> without(std::vector<int> base, int val) {
  base.erase(std::find(base.begin(), base.end(), val));
  return base;
}

std::vector<int> exchange(std::vector<int> base, int val, int val2) {
  for(int i = 0; i < base.size(); i++)
    if(base[i] == val)
      base[i] = val2;
  return base;
}

void update(std::vector<int> &skeleton, int id) {
  int x = edges[id].first, y = edges[id].second;
  std::vector<int> path;
  if(level[x] < level[y])
    std::swap(x, y);
  while(level[x] != level[y]) {
    path.push_back(far[x]);
    x = edges[far[x]].first + edges[far[x]].second - x;
  }
  while(x != y) { 
    path.push_back(far[x]);
    x = edges[far[x]].first + edges[far[x]].second - x;
    path.push_back(far[y]);
    y = edges[far[y]].first + edges[far[y]].second - y;
  }
  bool chance = 0;
  for(int i = 0; i < path.size(); i++) { 
    int id2 = path[i];
    if(state[id2] == 0)
      chance = 1;
  }
  if(chance == 0)
    return ;
  int base = count_common_roads(skeleton);
  for(int i = 0; i < path.size(); i++) {
    int id2 = path[i];
    if(0 < state[id2]) {
      state[id] = state[id2] - base + count_common_roads(exchange(skeleton, id2, id));
      break;
    }
  }
  if(0 < state[id]) {
    for(int i = 0; i < path.size(); i++) {
      int id2 = path[i];
      if(0 == state[id2]) {
        state[id2] = state[id] - count_common_roads(exchange(skeleton, id2, id)) + base;
      }
    }
  } else {
    std::vector<int> eval(path.size());
    int smax = base;
    for(int i = 0; i < path.size(); i++) {
      int id2 = path[i];
      eval[i] = count_common_roads(exchange(skeleton, id2, id));
      smax = std::max(eval[i], smax);
    }
    if(smax == base)
      state[id] = 1;
    else
      state[id] = 2;
    for(int i = 0; i < path.size(); i++)
      if(smax == eval[i])
        state[path[i]] = 1;
      else
        state[path[i]] = 2;
  }
}

int eval(std::vector<int> aux, std::vector<int> &skeleton, int n){
  Dsu dsu(n);
  for(int i = 0; i < aux.size(); i++) 
    dsu.unite(edges[aux[i]].first, edges[aux[i]].second);
  int cost = 0;
  for(int i = 0; i < skeleton.size(); i++) {
    int id = skeleton[i];
    if(dsu.connect(edges[id].first, edges[id].second) == 0) {
      dsu.unite(edges[id].first, edges[id].second);
      aux.push_back(id);
      cost += state[id] - 1;
    }
  }

  return count_common_roads(aux) - cost;
}

std::vector<int> first_k(std::vector<int> aux, int k) {
  std::vector<int> result;
  for(int i = 0; i < k; i++)
    result.push_back(aux[i]);
  return result;
}

std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
  edges.resize(u.size());
  state.resize(u.size());
  far.resize(n);
  level.resize(n);

  for(int i = 0;i < edges.size(); i++)
    edges[i] = {u[i], v[i]};
  std::vector<int> skeleton = get_skeleton(n);
  mark_skeleton(skeleton, n);
  
  for(int i = 0; i < edges.size(); i++) 
    if(std::find(skeleton.begin(), skeleton.end(), i) == skeleton.end())
      update(skeleton, i);
  for(int i = 0; i < skeleton.size(); i++)
    if(state[skeleton[i]] == 0)
      state[skeleton[i]] = 2;
  

  std::vector<std::vector<int>> ad(n);
  for(int i = 0; i < u.size(); i++) {
    if(0 < state[i])
      continue;
    if(u[i] < v[i])
      ad[u[i]].push_back(i);
    else
      ad[v[i]].push_back(i);
  }

  for(int i = 0; i < n; i++) {
    int total = eval(ad[i], skeleton, n);
    for(int j = 0; j < total; j++) {
      int x = 0;
      for(int jump = (1 << 10); 0 < jump; jump /= 2) {
        if(x + jump <= ad[i].size() && eval(first_k(ad[i], x + jump), skeleton, n) <= j)
          x += jump;
      }
      state[ad[i][x]] = 2;
    }
  }


  std::vector<int> sol;
  for(int i = 0; i < u.size(); i++)
    if(state[i] == 2)
      sol.push_back(i);

  return sol;
}

Compilation message

simurgh.cpp: In function 'std::vector<int> get_skeleton(int)':
simurgh.cpp:40:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |   for(int i = 0; i < edges.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~
simurgh.cpp: In function 'void dfs(int, int)':
simurgh.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |   for(int h = 0; h < g[node].size(); h++) {
      |                  ~~^~~~~~~~~~~~~~~~
simurgh.cpp: In function 'void mark_skeleton(std::vector<int>&, int)':
simurgh.cpp:67:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |   for(int i = 0; i < skeleton.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~~~
simurgh.cpp: In function 'std::vector<int> exchange(std::vector<int>, int, int)':
simurgh.cpp:86:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |   for(int i = 0; i < base.size(); i++)
      |                  ~~^~~~~~~~~~~~~
simurgh.cpp: In function 'void update(std::vector<int>&, int)':
simurgh.cpp:108:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |   for(int i = 0; i < path.size(); i++) {
      |                  ~~^~~~~~~~~~~~~
simurgh.cpp:116:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |   for(int i = 0; i < path.size(); i++) {
      |                  ~~^~~~~~~~~~~~~
simurgh.cpp:124:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |     for(int i = 0; i < path.size(); i++) {
      |                    ~~^~~~~~~~~~~~~
simurgh.cpp:133:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |     for(int i = 0; i < path.size(); i++) {
      |                    ~~^~~~~~~~~~~~~
simurgh.cpp:142:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |     for(int i = 0; i < path.size(); i++)
      |                    ~~^~~~~~~~~~~~~
simurgh.cpp: In function 'int eval(std::vector<int>, std::vector<int>&, int)':
simurgh.cpp:152:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |   for(int i = 0; i < aux.size(); i++)
      |                  ~~^~~~~~~~~~~~
simurgh.cpp:155:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  155 |   for(int i = 0; i < skeleton.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:180:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  180 |   for(int i = 0;i < edges.size(); i++)
      |                 ~~^~~~~~~~~~~~~~
simurgh.cpp:185:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  185 |   for(int i = 0; i < edges.size(); i++)
      |                  ~~^~~~~~~~~~~~~~
simurgh.cpp:188:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  188 |   for(int i = 0; i < skeleton.size(); i++)
      |                  ~~^~~~~~~~~~~~~~~~~
simurgh.cpp:194:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  194 |   for(int i = 0; i < u.size(); i++) {
      |                  ~~^~~~~~~~~~
simurgh.cpp:208:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  208 |         if(x + jump <= ad[i].size() && eval(first_k(ad[i], x + jump), skeleton, n) <= j)
      |            ~~~~~~~~~^~~~~~~~~~~~~~~
simurgh.cpp:217:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  217 |   for(int i = 0; i < u.size(); i++)
      |                  ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB correct
2 Correct 1 ms 256 KB correct
3 Correct 1 ms 256 KB correct
4 Correct 1 ms 256 KB correct
5 Correct 1 ms 256 KB correct
6 Correct 1 ms 256 KB correct
7 Correct 0 ms 256 KB correct
8 Correct 1 ms 256 KB correct
9 Correct 1 ms 256 KB correct
10 Correct 1 ms 256 KB correct
11 Correct 1 ms 256 KB correct
12 Correct 0 ms 256 KB correct
13 Correct 0 ms 256 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB correct
2 Correct 1 ms 256 KB correct
3 Correct 1 ms 256 KB correct
4 Correct 1 ms 256 KB correct
5 Correct 1 ms 256 KB correct
6 Correct 1 ms 256 KB correct
7 Correct 0 ms 256 KB correct
8 Correct 1 ms 256 KB correct
9 Correct 1 ms 256 KB correct
10 Correct 1 ms 256 KB correct
11 Correct 1 ms 256 KB correct
12 Correct 0 ms 256 KB correct
13 Correct 0 ms 256 KB correct
14 Correct 3 ms 384 KB correct
15 Correct 3 ms 384 KB correct
16 Correct 3 ms 384 KB correct
17 Correct 2 ms 384 KB correct
18 Correct 2 ms 384 KB correct
19 Correct 3 ms 384 KB correct
20 Correct 3 ms 384 KB correct
21 Correct 2 ms 384 KB correct
22 Correct 1 ms 384 KB correct
23 Correct 1 ms 384 KB correct
24 Correct 1 ms 384 KB correct
25 Correct 1 ms 384 KB correct
26 Correct 1 ms 384 KB correct
27 Correct 2 ms 384 KB correct
28 Correct 1 ms 384 KB correct
29 Correct 1 ms 384 KB correct
30 Correct 1 ms 384 KB correct
31 Correct 1 ms 384 KB correct
32 Correct 1 ms 384 KB correct
33 Correct 1 ms 384 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB correct
2 Correct 1 ms 256 KB correct
3 Correct 1 ms 256 KB correct
4 Correct 1 ms 256 KB correct
5 Correct 1 ms 256 KB correct
6 Correct 1 ms 256 KB correct
7 Correct 0 ms 256 KB correct
8 Correct 1 ms 256 KB correct
9 Correct 1 ms 256 KB correct
10 Correct 1 ms 256 KB correct
11 Correct 1 ms 256 KB correct
12 Correct 0 ms 256 KB correct
13 Correct 0 ms 256 KB correct
14 Correct 3 ms 384 KB correct
15 Correct 3 ms 384 KB correct
16 Correct 3 ms 384 KB correct
17 Correct 2 ms 384 KB correct
18 Correct 2 ms 384 KB correct
19 Correct 3 ms 384 KB correct
20 Correct 3 ms 384 KB correct
21 Correct 2 ms 384 KB correct
22 Correct 1 ms 384 KB correct
23 Correct 1 ms 384 KB correct
24 Correct 1 ms 384 KB correct
25 Correct 1 ms 384 KB correct
26 Correct 1 ms 384 KB correct
27 Correct 2 ms 384 KB correct
28 Correct 1 ms 384 KB correct
29 Correct 1 ms 384 KB correct
30 Correct 1 ms 384 KB correct
31 Correct 1 ms 384 KB correct
32 Correct 1 ms 384 KB correct
33 Correct 1 ms 384 KB correct
34 Correct 76 ms 1536 KB correct
35 Correct 73 ms 1528 KB correct
36 Correct 64 ms 1152 KB correct
37 Correct 17 ms 384 KB correct
38 Correct 74 ms 1528 KB correct
39 Correct 69 ms 1408 KB correct
40 Correct 63 ms 1152 KB correct
41 Correct 72 ms 1536 KB correct
42 Correct 72 ms 1564 KB correct
43 Correct 20 ms 896 KB correct
44 Correct 18 ms 888 KB correct
45 Correct 20 ms 896 KB correct
46 Correct 16 ms 820 KB correct
47 Correct 10 ms 512 KB correct
48 Correct 5 ms 384 KB correct
49 Correct 8 ms 384 KB correct
50 Correct 11 ms 512 KB correct
51 Correct 19 ms 896 KB correct
52 Correct 18 ms 896 KB correct
53 Correct 18 ms 768 KB correct
54 Correct 21 ms 1024 KB correct
55 Correct 21 ms 896 KB correct
56 Correct 21 ms 896 KB correct
57 Correct 22 ms 896 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB correct
2 Correct 1 ms 256 KB correct
3 Correct 231 ms 3704 KB correct
4 Correct 365 ms 5532 KB correct
5 Correct 368 ms 5496 KB correct
6 Correct 353 ms 5500 KB correct
7 Correct 372 ms 5496 KB correct
8 Correct 359 ms 5496 KB correct
9 Correct 365 ms 5624 KB correct
10 Correct 373 ms 5496 KB correct
11 Correct 371 ms 5476 KB correct
12 Correct 372 ms 5624 KB correct
13 Correct 1 ms 256 KB correct
14 Correct 357 ms 5416 KB correct
15 Correct 371 ms 5540 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB correct
2 Correct 1 ms 256 KB correct
3 Correct 1 ms 256 KB correct
4 Correct 1 ms 256 KB correct
5 Correct 1 ms 256 KB correct
6 Correct 1 ms 256 KB correct
7 Correct 0 ms 256 KB correct
8 Correct 1 ms 256 KB correct
9 Correct 1 ms 256 KB correct
10 Correct 1 ms 256 KB correct
11 Correct 1 ms 256 KB correct
12 Correct 0 ms 256 KB correct
13 Correct 0 ms 256 KB correct
14 Correct 3 ms 384 KB correct
15 Correct 3 ms 384 KB correct
16 Correct 3 ms 384 KB correct
17 Correct 2 ms 384 KB correct
18 Correct 2 ms 384 KB correct
19 Correct 3 ms 384 KB correct
20 Correct 3 ms 384 KB correct
21 Correct 2 ms 384 KB correct
22 Correct 1 ms 384 KB correct
23 Correct 1 ms 384 KB correct
24 Correct 1 ms 384 KB correct
25 Correct 1 ms 384 KB correct
26 Correct 1 ms 384 KB correct
27 Correct 2 ms 384 KB correct
28 Correct 1 ms 384 KB correct
29 Correct 1 ms 384 KB correct
30 Correct 1 ms 384 KB correct
31 Correct 1 ms 384 KB correct
32 Correct 1 ms 384 KB correct
33 Correct 1 ms 384 KB correct
34 Correct 76 ms 1536 KB correct
35 Correct 73 ms 1528 KB correct
36 Correct 64 ms 1152 KB correct
37 Correct 17 ms 384 KB correct
38 Correct 74 ms 1528 KB correct
39 Correct 69 ms 1408 KB correct
40 Correct 63 ms 1152 KB correct
41 Correct 72 ms 1536 KB correct
42 Correct 72 ms 1564 KB correct
43 Correct 20 ms 896 KB correct
44 Correct 18 ms 888 KB correct
45 Correct 20 ms 896 KB correct
46 Correct 16 ms 820 KB correct
47 Correct 10 ms 512 KB correct
48 Correct 5 ms 384 KB correct
49 Correct 8 ms 384 KB correct
50 Correct 11 ms 512 KB correct
51 Correct 19 ms 896 KB correct
52 Correct 18 ms 896 KB correct
53 Correct 18 ms 768 KB correct
54 Correct 21 ms 1024 KB correct
55 Correct 21 ms 896 KB correct
56 Correct 21 ms 896 KB correct
57 Correct 22 ms 896 KB correct
58 Correct 1 ms 256 KB correct
59 Correct 1 ms 256 KB correct
60 Correct 231 ms 3704 KB correct
61 Correct 365 ms 5532 KB correct
62 Correct 368 ms 5496 KB correct
63 Correct 353 ms 5500 KB correct
64 Correct 372 ms 5496 KB correct
65 Correct 359 ms 5496 KB correct
66 Correct 365 ms 5624 KB correct
67 Correct 373 ms 5496 KB correct
68 Correct 371 ms 5476 KB correct
69 Correct 372 ms 5624 KB correct
70 Correct 1 ms 256 KB correct
71 Correct 357 ms 5416 KB correct
72 Correct 371 ms 5540 KB correct
73 Correct 0 ms 256 KB correct
74 Correct 384 ms 5496 KB correct
75 Correct 365 ms 5240 KB correct
76 Correct 183 ms 2168 KB correct
77 Correct 371 ms 5624 KB correct
78 Correct 370 ms 5500 KB correct
79 Correct 386 ms 5604 KB correct
80 Correct 359 ms 5368 KB correct
81 Correct 343 ms 4728 KB correct
82 Correct 365 ms 5368 KB correct
83 Correct 277 ms 2936 KB correct
84 Correct 99 ms 3320 KB correct
85 Correct 91 ms 3064 KB correct
86 Correct 76 ms 2296 KB correct
87 Correct 62 ms 1656 KB correct
88 Correct 53 ms 1408 KB correct
89 Correct 53 ms 1404 KB correct
90 Correct 47 ms 1280 KB correct
91 Correct 27 ms 488 KB correct
92 Correct 22 ms 384 KB correct
93 Correct 91 ms 3192 KB correct
94 Correct 70 ms 2328 KB correct
95 Correct 69 ms 2168 KB correct
96 Correct 52 ms 1272 KB correct
97 Correct 53 ms 1408 KB correct
98 Correct 59 ms 1656 KB correct
99 Correct 57 ms 1528 KB correct
100 Correct 31 ms 640 KB correct
101 Correct 23 ms 384 KB correct
102 Correct 91 ms 2936 KB correct
103 Correct 89 ms 3012 KB correct
104 Correct 96 ms 3064 KB correct
105 Correct 91 ms 2988 KB correct