Submission #338343

# Submission time Handle Problem Language Result Execution time Memory
338343 2020-12-23T03:29:03 Z SHZhang Simurgh (IOI17_simurgh) C++14
100 / 100
552 ms 23032 KB
#include <vector>
#include <algorithm>
#include <map>
#include "simurgh.h"

using namespace std;

vector<int> graph[505];
bool vis[505];
int depth[505];
int parent[505];
int prtedge[505];
int u[250005], v[250005];

int n, m;

vector<pair<int, int> > extra_edge;
vector<int> tree_edge;

map<pair<int, int>, int> edge2id;
bool ans[250005];

void dfs(int node, int prt, int dep)
{
    vis[node] = true;
    depth[node] = dep;
    parent[node] = prt;
    for (int i = 0; i < graph[node].size(); i++) {
        int nxt = graph[node][i];
        if (!vis[nxt]) {
            tree_edge.push_back(edge2id[make_pair(node, nxt)]);
            dfs(nxt, node, dep + 1);
        } else if (nxt != prt && depth[nxt] < dep) {
            extra_edge.push_back(make_pair(node, nxt));
        }
    }
}

vector<int> good_edge;
vector<int> unkn_edge[505];

int uf[505];
int fin(int x)
{
    if (uf[x] == x) return x;
    return uf[x] = fin(uf[x]);
}

void un(int x, int y)
{
    x = fin(x); y = fin(y);
    if (x == y) return;
    uf[x] = y;
}

int search(vector<int> ids, int known_amt)
{
    if (ids.empty()) return 0;
    if (known_amt == -1) {
        for (int i = 0; i < n; i++) uf[i] = i;
        int base_val = 0;
        vector<int> query_set;
        for (int i = 0; i < ids.size(); i++) {
            query_set.push_back(ids[i]);
            un(u[ids[i]], v[ids[i]]);
        }
        for (int i = 0; i < tree_edge.size(); i++) {
            int edge = tree_edge[i];
            if (fin(u[edge]) == fin(v[edge])) continue;
            un(u[edge], v[edge]);
            query_set.push_back(edge);
            if (ans[edge]) base_val++;
        }
        int query_res = count_common_roads(query_set) - base_val;
        known_amt = query_res;
    }
    if (!known_amt) return 0;
    if (ids.size() == 1) {
        ans[ids[0]] = true; return 1;
    }
    vector<int> left, right;
    for (int i = 0; i < ids.size(); i++) {
        if (i < ids.size() / 2) {
            left.push_back(ids[i]);
        } else {
            right.push_back(ids[i]);
        }
    }
    int left_num = search(left, -1);
    search(right, known_amt - left_num);
    return known_amt;
}

vector<int> find_roads(int N, vector<int> U, vector<int> V) {
	n = N; m = U.size();
    //fprintf(stderr, "!!!%d\n", n);
    for (int i = 0; i < n; i++) prtedge[i] = -1;
    //fprintf(stderr, "HERE1 %d\n", n);
    for (int i = 0; i < m; i++) {
        u[i] = U[i]; v[i] = V[i];
        graph[u[i]].push_back(v[i]);
        graph[v[i]].push_back(u[i]);
        edge2id[make_pair(u[i], v[i])] = i;
        edge2id[make_pair(v[i], u[i])] = i;
        //fprintf(stderr, "HERE1 %d\n", n);
    }
    //fprintf(stderr, "HERE1 %d\n", n);
    dfs(0, -1, 0);
    vector<int> dfs_tree_qry;
    for (int i = 0; i < tree_edge.size(); i++) {
        //fprintf(stderr, "tree %d %d\n", u[tree_edge[i]], v[tree_edge[i]]);
        dfs_tree_qry.push_back(tree_edge[i]);
    }
    //fprintf(stderr, "HERE1 %d\n", n);
    int tree_base = count_common_roads(dfs_tree_qry);
    //fprintf(stderr, "HERE2 %d\n", n);
    for (int i = 0; i < extra_edge.size(); i++) {
        //fprintf(stderr, "extra %d %d\n", extra_edge[i].first, extra_edge[i].second);
        int curnode = extra_edge[i].first;
        vector<pair<int, int> > query_edge;
        vector<int> query_res;
        //int route_on = 0;
        bool has_zero = false;
        while (curnode != extra_edge[i].second) {
            if (prtedge[curnode] == -1) {
                prtedge[curnode] = i;
                query_edge.push_back(make_pair(curnode, parent[curnode]));
            } else {
                if (!has_zero && !ans[edge2id[make_pair(curnode, parent[curnode])]]) {
                    query_edge.push_back(make_pair(curnode, parent[curnode]));
                    has_zero = true;
                }
            }
            curnode = parent[curnode];
        }
        if (query_edge.size() - (int)has_zero == 0) {
            unkn_edge[extra_edge[i].first].push_back(extra_edge[i].second); continue;
        }
        good_edge.push_back(edge2id[extra_edge[i]]);
        int max_query_res = tree_base;
        for (int j = 0; j < query_edge.size(); j++) {
            //fprintf(stderr, "%d\n", edge2id[query_edge[j]]);
            vector<int> query_set;
            for (int k = 0; k < n - 1; k++) {
                if (dfs_tree_qry[k] != edge2id[query_edge[j]]) {
                    //fprintf(stderr, "%d ", dfs_tree_qry[k]);
                    query_set.push_back(dfs_tree_qry[k]);
                } else {
                    //fprintf(stderr, "EQUAL");
                }
                //fprintf(stderr, "here %d %d\n", k, n - 1);
            }
            query_set.push_back(edge2id[extra_edge[i]]);
            //fprintf(stderr, "before");
            int cnt = count_common_roads(query_set);
            //fprintf(stderr, "after");
            query_res.push_back(cnt);
            max_query_res = max(max_query_res, cnt);
        }
        for (int j = 0; j < query_edge.size(); j++) {
            if (query_res[j] != max_query_res) {
                //printf("%d %d\n", edge2id[extra_edge[i]], edge2id[query_edge[j]]);
                ans[edge2id[query_edge[j]]] = true;
            }
        }
        //printf("! %d %d\n", edge2id[extra_edge[i]], max_query_res);
        if (tree_base != max_query_res) ans[edge2id[extra_edge[i]]] = true;
    }
    for (int i = 1; i < n; i++) {
        good_edge.push_back(edge2id[make_pair(i, parent[i])]);
        if (prtedge[i] == -1) {
            ans[edge2id[make_pair(i, parent[i])]] = true;
        }
    }
    //fprintf(stderr, "HERE\n");
    for (int i = 0; i < n; i++) {
        vector<int> edges;
        for (int j = 0; j < unkn_edge[i].size(); j++) {
            edges.push_back(edge2id[make_pair(i, unkn_edge[i][j])]);
        }
        search(edges, -1);
    }
    vector<int> final_ans;
    for (int i = 0; i < m; i++) {
        if (ans[i]) {
            //fprintf(stderr, "%d\n", i);
            final_ans.push_back(i);
        }
    }
    return final_ans;
}

Compilation message

simurgh.cpp: In function 'void dfs(int, int, int)':
simurgh.cpp:28:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     for (int i = 0; i < graph[node].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~~~~~
simurgh.cpp: In function 'int search(std::vector<int>, int)':
simurgh.cpp:63:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |         for (int i = 0; i < ids.size(); i++) {
      |                         ~~^~~~~~~~~~~~
simurgh.cpp:67:27: 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 < tree_edge.size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~~
simurgh.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for (int i = 0; i < ids.size(); i++) {
      |                     ~~^~~~~~~~~~~~
simurgh.cpp:83:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         if (i < ids.size() / 2) {
      |             ~~^~~~~~~~~~~~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:110:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |     for (int i = 0; i < tree_edge.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~~~
simurgh.cpp:117:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |     for (int i = 0; i < extra_edge.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~~~~
simurgh.cpp:141:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |         for (int j = 0; j < query_edge.size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~~~~~
simurgh.cpp:160:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  160 |         for (int j = 0; j < query_edge.size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~~~~~
simurgh.cpp:178:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  178 |         for (int j = 0; j < unkn_edge[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB correct
2 Correct 0 ms 364 KB correct
3 Correct 1 ms 364 KB correct
4 Correct 1 ms 384 KB correct
5 Correct 1 ms 364 KB correct
6 Correct 0 ms 364 KB correct
7 Correct 0 ms 364 KB correct
8 Correct 1 ms 364 KB correct
9 Correct 0 ms 364 KB correct
10 Correct 1 ms 492 KB correct
11 Correct 1 ms 364 KB correct
12 Correct 1 ms 364 KB correct
13 Correct 0 ms 364 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB correct
2 Correct 0 ms 364 KB correct
3 Correct 1 ms 364 KB correct
4 Correct 1 ms 384 KB correct
5 Correct 1 ms 364 KB correct
6 Correct 0 ms 364 KB correct
7 Correct 0 ms 364 KB correct
8 Correct 1 ms 364 KB correct
9 Correct 0 ms 364 KB correct
10 Correct 1 ms 492 KB correct
11 Correct 1 ms 364 KB correct
12 Correct 1 ms 364 KB correct
13 Correct 0 ms 364 KB correct
14 Correct 4 ms 620 KB correct
15 Correct 3 ms 620 KB correct
16 Correct 3 ms 620 KB correct
17 Correct 3 ms 492 KB correct
18 Correct 2 ms 364 KB correct
19 Correct 3 ms 492 KB correct
20 Correct 2 ms 492 KB correct
21 Correct 3 ms 492 KB correct
22 Correct 2 ms 492 KB correct
23 Correct 2 ms 492 KB correct
24 Correct 2 ms 492 KB correct
25 Correct 1 ms 364 KB correct
26 Correct 2 ms 492 KB correct
27 Correct 2 ms 492 KB correct
28 Correct 1 ms 364 KB correct
29 Correct 1 ms 364 KB correct
30 Correct 2 ms 492 KB correct
31 Correct 2 ms 492 KB correct
32 Correct 2 ms 492 KB correct
33 Correct 2 ms 492 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB correct
2 Correct 0 ms 364 KB correct
3 Correct 1 ms 364 KB correct
4 Correct 1 ms 384 KB correct
5 Correct 1 ms 364 KB correct
6 Correct 0 ms 364 KB correct
7 Correct 0 ms 364 KB correct
8 Correct 1 ms 364 KB correct
9 Correct 0 ms 364 KB correct
10 Correct 1 ms 492 KB correct
11 Correct 1 ms 364 KB correct
12 Correct 1 ms 364 KB correct
13 Correct 0 ms 364 KB correct
14 Correct 4 ms 620 KB correct
15 Correct 3 ms 620 KB correct
16 Correct 3 ms 620 KB correct
17 Correct 3 ms 492 KB correct
18 Correct 2 ms 364 KB correct
19 Correct 3 ms 492 KB correct
20 Correct 2 ms 492 KB correct
21 Correct 3 ms 492 KB correct
22 Correct 2 ms 492 KB correct
23 Correct 2 ms 492 KB correct
24 Correct 2 ms 492 KB correct
25 Correct 1 ms 364 KB correct
26 Correct 2 ms 492 KB correct
27 Correct 2 ms 492 KB correct
28 Correct 1 ms 364 KB correct
29 Correct 1 ms 364 KB correct
30 Correct 2 ms 492 KB correct
31 Correct 2 ms 492 KB correct
32 Correct 2 ms 492 KB correct
33 Correct 2 ms 492 KB correct
34 Correct 80 ms 5612 KB correct
35 Correct 77 ms 5360 KB correct
36 Correct 59 ms 3948 KB correct
37 Correct 14 ms 620 KB correct
38 Correct 87 ms 5596 KB correct
39 Correct 69 ms 4856 KB correct
40 Correct 69 ms 3948 KB correct
41 Correct 76 ms 5484 KB correct
42 Correct 74 ms 5484 KB correct
43 Correct 34 ms 3180 KB correct
44 Correct 28 ms 2668 KB correct
45 Correct 32 ms 2924 KB correct
46 Correct 27 ms 2412 KB correct
47 Correct 15 ms 1260 KB correct
48 Correct 6 ms 492 KB correct
49 Correct 9 ms 620 KB correct
50 Correct 14 ms 1260 KB correct
51 Correct 32 ms 2924 KB correct
52 Correct 27 ms 2668 KB correct
53 Correct 25 ms 2412 KB correct
54 Correct 33 ms 3324 KB correct
55 Correct 33 ms 3052 KB correct
56 Correct 33 ms 3052 KB correct
57 Correct 35 ms 3052 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB correct
2 Correct 1 ms 364 KB correct
3 Correct 298 ms 14608 KB correct
4 Correct 520 ms 22752 KB correct
5 Correct 517 ms 22880 KB correct
6 Correct 528 ms 23008 KB correct
7 Correct 483 ms 23032 KB correct
8 Correct 471 ms 22880 KB correct
9 Correct 549 ms 22880 KB correct
10 Correct 518 ms 22752 KB correct
11 Correct 500 ms 22920 KB correct
12 Correct 547 ms 22752 KB correct
13 Correct 1 ms 364 KB correct
14 Correct 503 ms 22864 KB correct
15 Correct 509 ms 22928 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB correct
2 Correct 0 ms 364 KB correct
3 Correct 1 ms 364 KB correct
4 Correct 1 ms 384 KB correct
5 Correct 1 ms 364 KB correct
6 Correct 0 ms 364 KB correct
7 Correct 0 ms 364 KB correct
8 Correct 1 ms 364 KB correct
9 Correct 0 ms 364 KB correct
10 Correct 1 ms 492 KB correct
11 Correct 1 ms 364 KB correct
12 Correct 1 ms 364 KB correct
13 Correct 0 ms 364 KB correct
14 Correct 4 ms 620 KB correct
15 Correct 3 ms 620 KB correct
16 Correct 3 ms 620 KB correct
17 Correct 3 ms 492 KB correct
18 Correct 2 ms 364 KB correct
19 Correct 3 ms 492 KB correct
20 Correct 2 ms 492 KB correct
21 Correct 3 ms 492 KB correct
22 Correct 2 ms 492 KB correct
23 Correct 2 ms 492 KB correct
24 Correct 2 ms 492 KB correct
25 Correct 1 ms 364 KB correct
26 Correct 2 ms 492 KB correct
27 Correct 2 ms 492 KB correct
28 Correct 1 ms 364 KB correct
29 Correct 1 ms 364 KB correct
30 Correct 2 ms 492 KB correct
31 Correct 2 ms 492 KB correct
32 Correct 2 ms 492 KB correct
33 Correct 2 ms 492 KB correct
34 Correct 80 ms 5612 KB correct
35 Correct 77 ms 5360 KB correct
36 Correct 59 ms 3948 KB correct
37 Correct 14 ms 620 KB correct
38 Correct 87 ms 5596 KB correct
39 Correct 69 ms 4856 KB correct
40 Correct 69 ms 3948 KB correct
41 Correct 76 ms 5484 KB correct
42 Correct 74 ms 5484 KB correct
43 Correct 34 ms 3180 KB correct
44 Correct 28 ms 2668 KB correct
45 Correct 32 ms 2924 KB correct
46 Correct 27 ms 2412 KB correct
47 Correct 15 ms 1260 KB correct
48 Correct 6 ms 492 KB correct
49 Correct 9 ms 620 KB correct
50 Correct 14 ms 1260 KB correct
51 Correct 32 ms 2924 KB correct
52 Correct 27 ms 2668 KB correct
53 Correct 25 ms 2412 KB correct
54 Correct 33 ms 3324 KB correct
55 Correct 33 ms 3052 KB correct
56 Correct 33 ms 3052 KB correct
57 Correct 35 ms 3052 KB correct
58 Correct 1 ms 364 KB correct
59 Correct 1 ms 364 KB correct
60 Correct 298 ms 14608 KB correct
61 Correct 520 ms 22752 KB correct
62 Correct 517 ms 22880 KB correct
63 Correct 528 ms 23008 KB correct
64 Correct 483 ms 23032 KB correct
65 Correct 471 ms 22880 KB correct
66 Correct 549 ms 22880 KB correct
67 Correct 518 ms 22752 KB correct
68 Correct 500 ms 22920 KB correct
69 Correct 547 ms 22752 KB correct
70 Correct 1 ms 364 KB correct
71 Correct 503 ms 22864 KB correct
72 Correct 509 ms 22928 KB correct
73 Correct 1 ms 364 KB correct
74 Correct 540 ms 22948 KB correct
75 Correct 552 ms 22240 KB correct
76 Correct 183 ms 8440 KB correct
77 Correct 535 ms 22896 KB correct
78 Correct 526 ms 22804 KB correct
79 Correct 519 ms 22880 KB correct
80 Correct 516 ms 22120 KB correct
81 Correct 425 ms 19096 KB correct
82 Correct 491 ms 22200 KB correct
83 Correct 289 ms 11692 KB correct
84 Correct 226 ms 13796 KB correct
85 Correct 230 ms 12472 KB correct
86 Correct 129 ms 8676 KB correct
87 Correct 91 ms 6252 KB correct
88 Correct 81 ms 4976 KB correct
89 Correct 78 ms 4716 KB correct
90 Correct 69 ms 4076 KB correct
91 Correct 29 ms 876 KB correct
92 Correct 15 ms 492 KB correct
93 Correct 215 ms 12396 KB correct
94 Correct 129 ms 8552 KB correct
95 Correct 123 ms 8296 KB correct
96 Correct 73 ms 4332 KB correct
97 Correct 81 ms 4976 KB correct
98 Correct 96 ms 6248 KB correct
99 Correct 79 ms 5104 KB correct
100 Correct 40 ms 1516 KB correct
101 Correct 20 ms 620 KB correct
102 Correct 216 ms 11624 KB correct
103 Correct 203 ms 11620 KB correct
104 Correct 216 ms 11684 KB correct
105 Correct 235 ms 11520 KB correct