Submission #135978

# Submission time Handle Problem Language Result Execution time Memory
135978 2019-07-24T14:56:35 Z IOrtroiii Simurgh (IOI17_simurgh) C++14
100 / 100
880 ms 10848 KB
#include "simurgh.h"

#include <bits/stdc++.h>

using namespace std;

int n, m;
vector<pair<int, int>> g[505];
int par[505];
int parId[505];
int high[505];
int color[505 * 505];
vector<int> tree;
int u[505 * 505], v[505 * 505];

struct Dsu {
   int par[505];
   void init() {
      for (int i = 0; i < n; ++i) {
         par[i] = i;
      }
   }
   int find(int u) {
      if (u != par[u]) {
         par[u] = find(par[u]);
      }
      return par[u];
   }
   bool merge(int u, int v) {
      u = find(u);
      v = find(v);
      if (u == v) {
         return false;
      } else {
         par[v] = u;
         return true;
      }
   }
} d;

void dfs(int u) {
   for (auto e : g[u]) {
      int v, id;
      tie(v, id) = e;
      if (v != par[u]) {
         par[v] = u;
         parId[v] = id;
         high[v] = high[u] + 1;
         dfs(v);
      }
   }
}

vector<int> findPath(int u, int v) {
   vector<int> ans;
   while (u != v) {
      if (high[u] > high[v]) {
         ans.emplace_back(parId[u]);
         u = par[u];
      } else {
         ans.emplace_back(parId[v]);
         v = par[v];
      }
   }
   return ans;
}

int get(vector<int> es) {
   d.init();
   for (int i : es) {
      d.merge(u[i], v[i]);
   }
   int offset = 0;
   for (int i : tree) {
      if (d.merge(u[i], v[i])) {
         offset += color[i];
         es.push_back(i);
      }
   }
   assert(es.size() == n - 1);
   return count_common_roads(es) - offset;
}

vector<int> ans;

void solve(int cnt, vector<int> go) {
   int n = go.size();
   if (cnt == 0) {
      return;
   } else if (cnt == n) {
      for (int i : go) {
         ans.emplace_back(i);
      }
      return;
   } else {
      vector<int> lgo = vector<int>(go.begin(), go.begin() + (n / 2));
      vector<int> rgo = vector<int>(go.begin() + (n / 2), go.end());
      int lcnt = get(lgo);
      int rcnt = cnt - lcnt;
      solve(lcnt, lgo);
      solve(rcnt, rgo);
   }
}

vector<int> find_roads(int n, vector<int> u, vector<int> v) {
   ::n = n;
   m = u.size();
	for (int i = 0; i < m; ++i) {
      ::u[i] = u[i];
      ::v[i] = v[i];
      color[i] = -1;
	}
	d.init();
	set<int> out;
	for (int i = 0; i < m; ++i) {
      if (d.merge(u[i], v[i])) {
         tree.push_back(i);
         g[u[i]].emplace_back(v[i], i);
         g[v[i]].emplace_back(u[i], i);
      } else {
         out.insert(i);
      }
	}
	dfs(0);
	for (int i : out) {
      vector<int> cur = findPath(u[i], v[i]);
      bool flag = false;
      for (int j : cur) {
         if (color[j] == -1) {
            flag = true;
         }
      }
      if (!flag) {
         continue;
      }
      cur.emplace_back(i);
      int numEdge = -1;
      for (int j : cur) {
         if (color[j] != -1) {
            vector<int> q;
            for (int k : cur) if (j != k) {
               q.emplace_back(k);
            }
            numEdge = get(q) + color[j];
            break;
         }
      }
      vector<pair<int, int>> vals;
      for (int j : cur) {
         if (color[j] == -1) {
            vector<int> q;
            for (int k : cur) if (j != k) {
               q.emplace_back(k);
            }
            vals.emplace_back(j, get(q));
            numEdge = max(numEdge, vals.back().second);
         }
      }
      for (auto p : vals) {
         color[p.first] = p.second < numEdge;
      }
	}
	for (int i : tree) {
      color[i] = abs(color[i]);
      if (color[i]) {
         ans.emplace_back(i);
      }
	}
	for (int i = 0; i < n; ++i) {
      vector<int> go;
      for (int j : out) {
         if (u[j] == i || v[j] == i) {
            go.emplace_back(j);
         }
      }
      if (go.size()) {
         for (int j : go) {
            out.erase(j);
         }
         solve(get(go), go);
      }
	}
	return ans;
}
// g++ -std=c++14 grader.cpp simurgh.cpp

Compilation message

In file included from /usr/include/c++/7/cassert:44:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
                 from simurgh.cpp:3:
simurgh.cpp: In function 'int get(std::vector<int>)':
simurgh.cpp:80:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    assert(es.size() == n - 1);
           ~~~~~~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 420 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 476 KB correct
9 Correct 2 ms 380 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 348 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 376 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 420 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 476 KB correct
9 Correct 2 ms 380 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 348 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 4 ms 504 KB correct
15 Correct 4 ms 504 KB correct
16 Correct 4 ms 504 KB correct
17 Correct 4 ms 376 KB correct
18 Correct 3 ms 376 KB correct
19 Correct 4 ms 504 KB correct
20 Correct 4 ms 376 KB correct
21 Correct 5 ms 508 KB correct
22 Correct 3 ms 376 KB correct
23 Correct 4 ms 504 KB correct
24 Correct 3 ms 376 KB correct
25 Correct 3 ms 376 KB correct
26 Correct 3 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 3 ms 376 KB correct
29 Correct 2 ms 376 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 3 ms 376 KB correct
32 Correct 3 ms 376 KB correct
33 Correct 3 ms 376 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 420 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 476 KB correct
9 Correct 2 ms 380 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 348 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 4 ms 504 KB correct
15 Correct 4 ms 504 KB correct
16 Correct 4 ms 504 KB correct
17 Correct 4 ms 376 KB correct
18 Correct 3 ms 376 KB correct
19 Correct 4 ms 504 KB correct
20 Correct 4 ms 376 KB correct
21 Correct 5 ms 508 KB correct
22 Correct 3 ms 376 KB correct
23 Correct 4 ms 504 KB correct
24 Correct 3 ms 376 KB correct
25 Correct 3 ms 376 KB correct
26 Correct 3 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 3 ms 376 KB correct
29 Correct 2 ms 376 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 3 ms 376 KB correct
32 Correct 3 ms 376 KB correct
33 Correct 3 ms 376 KB correct
34 Correct 132 ms 2684 KB correct
35 Correct 121 ms 2668 KB correct
36 Correct 86 ms 1940 KB correct
37 Correct 18 ms 376 KB correct
38 Correct 123 ms 2680 KB correct
39 Correct 97 ms 2424 KB correct
40 Correct 82 ms 1956 KB correct
41 Correct 118 ms 2680 KB correct
42 Correct 121 ms 2736 KB correct
43 Correct 49 ms 1656 KB correct
44 Correct 41 ms 1400 KB correct
45 Correct 48 ms 1528 KB correct
46 Correct 39 ms 1272 KB correct
47 Correct 20 ms 760 KB correct
48 Correct 7 ms 376 KB correct
49 Correct 11 ms 504 KB correct
50 Correct 20 ms 760 KB correct
51 Correct 49 ms 1528 KB correct
52 Correct 42 ms 1404 KB correct
53 Correct 38 ms 1144 KB correct
54 Correct 50 ms 1656 KB correct
55 Correct 49 ms 1528 KB correct
56 Correct 48 ms 1528 KB correct
57 Correct 50 ms 1528 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 422 ms 7004 KB correct
4 Correct 880 ms 10848 KB correct
5 Correct 854 ms 10848 KB correct
6 Correct 850 ms 10616 KB correct
7 Correct 716 ms 10768 KB correct
8 Correct 776 ms 10656 KB correct
9 Correct 842 ms 10732 KB correct
10 Correct 836 ms 10616 KB correct
11 Correct 837 ms 10744 KB correct
12 Correct 824 ms 10752 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 804 ms 10744 KB correct
15 Correct 842 ms 10760 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 420 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 476 KB correct
9 Correct 2 ms 380 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 348 KB correct
12 Correct 2 ms 376 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 4 ms 504 KB correct
15 Correct 4 ms 504 KB correct
16 Correct 4 ms 504 KB correct
17 Correct 4 ms 376 KB correct
18 Correct 3 ms 376 KB correct
19 Correct 4 ms 504 KB correct
20 Correct 4 ms 376 KB correct
21 Correct 5 ms 508 KB correct
22 Correct 3 ms 376 KB correct
23 Correct 4 ms 504 KB correct
24 Correct 3 ms 376 KB correct
25 Correct 3 ms 376 KB correct
26 Correct 3 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 3 ms 376 KB correct
29 Correct 2 ms 376 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 3 ms 376 KB correct
32 Correct 3 ms 376 KB correct
33 Correct 3 ms 376 KB correct
34 Correct 132 ms 2684 KB correct
35 Correct 121 ms 2668 KB correct
36 Correct 86 ms 1940 KB correct
37 Correct 18 ms 376 KB correct
38 Correct 123 ms 2680 KB correct
39 Correct 97 ms 2424 KB correct
40 Correct 82 ms 1956 KB correct
41 Correct 118 ms 2680 KB correct
42 Correct 121 ms 2736 KB correct
43 Correct 49 ms 1656 KB correct
44 Correct 41 ms 1400 KB correct
45 Correct 48 ms 1528 KB correct
46 Correct 39 ms 1272 KB correct
47 Correct 20 ms 760 KB correct
48 Correct 7 ms 376 KB correct
49 Correct 11 ms 504 KB correct
50 Correct 20 ms 760 KB correct
51 Correct 49 ms 1528 KB correct
52 Correct 42 ms 1404 KB correct
53 Correct 38 ms 1144 KB correct
54 Correct 50 ms 1656 KB correct
55 Correct 49 ms 1528 KB correct
56 Correct 48 ms 1528 KB correct
57 Correct 50 ms 1528 KB correct
58 Correct 2 ms 376 KB correct
59 Correct 2 ms 376 KB correct
60 Correct 422 ms 7004 KB correct
61 Correct 880 ms 10848 KB correct
62 Correct 854 ms 10848 KB correct
63 Correct 850 ms 10616 KB correct
64 Correct 716 ms 10768 KB correct
65 Correct 776 ms 10656 KB correct
66 Correct 842 ms 10732 KB correct
67 Correct 836 ms 10616 KB correct
68 Correct 837 ms 10744 KB correct
69 Correct 824 ms 10752 KB correct
70 Correct 2 ms 376 KB correct
71 Correct 804 ms 10744 KB correct
72 Correct 842 ms 10760 KB correct
73 Correct 2 ms 376 KB correct
74 Correct 849 ms 10680 KB correct
75 Correct 739 ms 10404 KB correct
76 Correct 284 ms 4004 KB correct
77 Correct 817 ms 10656 KB correct
78 Correct 780 ms 10780 KB correct
79 Correct 823 ms 10676 KB correct
80 Correct 743 ms 10372 KB correct
81 Correct 631 ms 8952 KB correct
82 Correct 810 ms 10232 KB correct
83 Correct 454 ms 5496 KB correct
84 Correct 367 ms 6392 KB correct
85 Correct 322 ms 5752 KB correct
86 Correct 238 ms 4088 KB correct
87 Correct 170 ms 2936 KB correct
88 Correct 142 ms 2436 KB correct
89 Correct 141 ms 2296 KB correct
90 Correct 118 ms 1912 KB correct
91 Correct 32 ms 504 KB correct
92 Correct 21 ms 508 KB correct
93 Correct 322 ms 5752 KB correct
94 Correct 245 ms 4216 KB correct
95 Correct 223 ms 3832 KB correct
96 Correct 124 ms 2056 KB correct
97 Correct 145 ms 2436 KB correct
98 Correct 172 ms 3036 KB correct
99 Correct 143 ms 2552 KB correct
100 Correct 51 ms 1016 KB correct
101 Correct 24 ms 504 KB correct
102 Correct 321 ms 5596 KB correct
103 Correct 322 ms 5596 KB correct
104 Correct 323 ms 5492 KB correct
105 Correct 324 ms 5624 KB correct