답안 #499600

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
499600 2021-12-29T00:54:14 Z CatalinT Simurgh (IOI17_simurgh) C++17
51 / 100
146 ms 4996 KB
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <cassert>
#include <iomanip>
#include <cmath>
#include <bitset>
 
using namespace std;

#include "simurgh.h"

using int64 = long long;

int N, M;
vector<vector<int>> eidx;
vector<vector<int>> adj;
vector<int> par;
vector<int> depth;
vector<char> used;

vector<int> status;

vector<int> tree_idx;
vector<int> back_idx;
vector<int> all_idx;

vector<char> added;

int get(int u, int v) {
    assert(eidx[u][v] != -1);
    return eidx[u][v];
}

void dfs(int n, int p) {
    //cerr << n << " " << p << "\n";
    used[n] = true;
    for (auto v : adj[n]) {
        if (v != p) {
            int e = get(n, v);
           
            if (!used[v]) {
                // Forward edge
                par[v] = n;
                depth[v] = depth[n] + 1;
                tree_idx.push_back(e);
                all_idx.push_back(e);
                dfs(v, n);
            } else if (!added[e]) {
                added[e] = true;
                back_idx.push_back(e);
                all_idx.push_back(e);
            }
        }
    }
}

vector<int> get_cycle(int u, int v) {
    vector<int> edges;
    
    auto go_up = [&] (int & el) {
        edges.push_back(get(el, par[el]));
        el = par[el];
    };

    while(depth[u] > depth[v]) {
        go_up(u);
    }
    while(depth[u] < depth[v]) {
        go_up(v);
    }
    while(u != v) {
        go_up(u);
        go_up(v);
    }

    return edges;
}

std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
    auto edge_str = [&](int e) {
        return to_string(u[e]) + " - " + to_string(v[e]) + " idx: " + to_string(e);
    };

    N = n;
    M = u.size();

    adj.resize(N, vector<int>{});
    eidx.resize(N, vector<int>(N, -1));
    par.resize(N, -1);
    used.resize(N, 0);
    depth.resize(N, 0);
    
    status.resize(M, -1);
    added.resize(M, 0);

    for (int i = 0; i < M; i++) {
        eidx[ u[i] ][ v[i] ] = eidx[ v[i] ][ u[i] ] = i;
        adj[ u[i] ].push_back( v[i] );
        adj[ v[i] ].push_back( u[i] );
    }

    // 1. Find DFS tree
    dfs(0, 0);

    assert(tree_idx.size() == N - 1);
    assert(tree_idx.size() + back_idx.size() == M);
    assert(all_idx.size() == M);

    // cerr << "tree edges:\n";
    for (auto e : tree_idx) {
        // cerr << edge_str(e) << "\n";
        assert(status[e] == -1);
    }

    // 50 points

    int tree_ans = count_common_roads(tree_idx);
    // cerr << "tree_ans: " << tree_ans << "\n";

    for (auto e : back_idx) {
        // get cycle
        auto cycle = get_cycle(u[e], v[e]);
        int unknown = 0;
        for (auto c : cycle)
            unknown += (status[c] == -1);

        // cerr << "Back edge: " << edge_str(e) << "\n";
        // cerr << "cycle\n";
        // for (auto c : cycle) {
        //     cerr << edge_str(c) << "\n";
        // }

        auto replace = [] (int e, int c, vector<int>& query) {
            auto p = find(query.begin(), query.end(), c);
            assert(p != query.end());
            *p = e;
        };

        auto replace_and_query = [&] (int e, int c) {
            auto query = tree_idx;
            replace(e, c, query);
            return count_common_roads(query) - tree_ans;
        };

        if (!unknown) { // all is known
            // cerr << "all known\n";
            // Just 1 query needed
            status[e] = replace_and_query(e, cycle[0]) + status[cycle[0]];
        } else if (unknown < cycle.size()) { // at least one is known
            // cerr << "at least one is known\n";
            // <= C++14
            int cand = -1;
            for (auto c : cycle) {
                if (status[c] != -1) {
                    cand = c;
                    break;
                }
            }

            status[e] = replace_and_query(e, cand) + status[cand];

            // use this to find the unknowns
            for (auto c : cycle) {
                if (status[c] == -1) {
                    status[c] = -replace_and_query(e, c) + status[e];
                }
            }
        } else { // all is unknown
            // cerr << "all is unknown\n";

            int neg = 0;
            int pos = 0;

            for (auto c : cycle) {
                status[c] = replace_and_query(e, c);
                // cerr << edge_str(c) << " delta: " << status[c] << "\n";
                neg += (status[c] < 0);
                pos += (status[c] > 0);
            }

            if (!pos && !neg) {
                // cerr << "nothing is different\n";
                assert (!pos);
                assert (!neg);
                for (auto c : cycle) {
                    status[c] = 0;
                }
                status[e] = 0;
            } else {
                assert(!pos || !neg);
                if (pos) {
                    for (auto c : cycle) {
                        status[c] = 1 - status[c];
                    }
                    status[e] = 1;
                    // cerr << "positive e\n";
                } else {
                    for (auto c : cycle) {
                        status[c] = -status[c];
                    }
                    status[e] = 0;
                    // cerr << "negative e\n";
                }

                // cerr << "find out\n";
                // for (auto c : cycle) {
                //     cerr << edge_str(c) << " -> " << status[c] << "\n";
                // }
                // cerr << "\n";
            }
        }
    }

    vector<int> res;

    int rem_unknown = 0;
    int rem_good = 0;

    for (auto e : all_idx) {
        if (status[e] == -1) {
            rem_unknown++;
            res.push_back(e);
        } else if (status[e] == 1) {
            rem_good++;
            res.push_back(e);
        }
    }

    assert(rem_good + rem_unknown == N - 1);

    return res;
}


Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from simurgh.cpp:16:
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:118:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  118 |     assert(tree_idx.size() == N - 1);
      |            ~~~~~~~~~~~~~~~~^~~~~~~~
simurgh.cpp:119:46: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  119 |     assert(tree_idx.size() + back_idx.size() == M);
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
simurgh.cpp:120:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  120 |     assert(all_idx.size() == M);
      |            ~~~~~~~~~~~~~~~^~~~
simurgh.cpp:162:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |         } else if (unknown < cycle.size()) { // at least one is known
      |                    ~~~~~~~~^~~~~~~~~~~~~~
simurgh.cpp:93:10: warning: variable 'edge_str' set but not used [-Wunused-but-set-variable]
   93 |     auto edge_str = [&](int e) {
      |          ^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB correct
2 Correct 0 ms 204 KB correct
3 Correct 0 ms 204 KB correct
4 Correct 0 ms 204 KB correct
5 Correct 0 ms 204 KB correct
6 Correct 0 ms 204 KB correct
7 Correct 0 ms 292 KB correct
8 Correct 0 ms 296 KB correct
9 Correct 0 ms 204 KB correct
10 Correct 0 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 292 KB correct
13 Correct 0 ms 204 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB correct
2 Correct 0 ms 204 KB correct
3 Correct 0 ms 204 KB correct
4 Correct 0 ms 204 KB correct
5 Correct 0 ms 204 KB correct
6 Correct 0 ms 204 KB correct
7 Correct 0 ms 292 KB correct
8 Correct 0 ms 296 KB correct
9 Correct 0 ms 204 KB correct
10 Correct 0 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 292 KB correct
13 Correct 0 ms 204 KB correct
14 Correct 2 ms 332 KB correct
15 Correct 2 ms 332 KB correct
16 Correct 2 ms 288 KB correct
17 Correct 2 ms 332 KB correct
18 Correct 1 ms 332 KB correct
19 Correct 2 ms 332 KB correct
20 Correct 2 ms 296 KB correct
21 Correct 2 ms 460 KB correct
22 Correct 1 ms 324 KB correct
23 Correct 1 ms 332 KB correct
24 Correct 1 ms 292 KB correct
25 Correct 1 ms 204 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 2 ms 332 KB correct
28 Correct 1 ms 204 KB correct
29 Correct 1 ms 204 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 332 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 332 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB correct
2 Correct 0 ms 204 KB correct
3 Correct 0 ms 204 KB correct
4 Correct 0 ms 204 KB correct
5 Correct 0 ms 204 KB correct
6 Correct 0 ms 204 KB correct
7 Correct 0 ms 292 KB correct
8 Correct 0 ms 296 KB correct
9 Correct 0 ms 204 KB correct
10 Correct 0 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 292 KB correct
13 Correct 0 ms 204 KB correct
14 Correct 2 ms 332 KB correct
15 Correct 2 ms 332 KB correct
16 Correct 2 ms 288 KB correct
17 Correct 2 ms 332 KB correct
18 Correct 1 ms 332 KB correct
19 Correct 2 ms 332 KB correct
20 Correct 2 ms 296 KB correct
21 Correct 2 ms 460 KB correct
22 Correct 1 ms 324 KB correct
23 Correct 1 ms 332 KB correct
24 Correct 1 ms 292 KB correct
25 Correct 1 ms 204 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 2 ms 332 KB correct
28 Correct 1 ms 204 KB correct
29 Correct 1 ms 204 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 332 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 332 KB correct
34 Correct 142 ms 1860 KB correct
35 Correct 146 ms 1848 KB correct
36 Correct 96 ms 1564 KB correct
37 Correct 7 ms 588 KB correct
38 Correct 139 ms 1968 KB correct
39 Correct 119 ms 1740 KB correct
40 Correct 97 ms 1612 KB correct
41 Correct 138 ms 1868 KB correct
42 Correct 140 ms 1924 KB correct
43 Correct 70 ms 1356 KB correct
44 Correct 64 ms 1196 KB correct
45 Correct 65 ms 1272 KB correct
46 Correct 49 ms 1076 KB correct
47 Correct 21 ms 848 KB correct
48 Correct 3 ms 460 KB correct
49 Correct 6 ms 640 KB correct
50 Correct 22 ms 844 KB correct
51 Correct 70 ms 1324 KB correct
52 Correct 55 ms 1100 KB correct
53 Correct 47 ms 1116 KB correct
54 Correct 70 ms 1356 KB correct
55 Correct 68 ms 1228 KB correct
56 Correct 69 ms 1284 KB correct
57 Correct 77 ms 1288 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 296 KB correct
2 Correct 1 ms 204 KB correct
3 Incorrect 121 ms 4996 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB correct
2 Correct 0 ms 204 KB correct
3 Correct 0 ms 204 KB correct
4 Correct 0 ms 204 KB correct
5 Correct 0 ms 204 KB correct
6 Correct 0 ms 204 KB correct
7 Correct 0 ms 292 KB correct
8 Correct 0 ms 296 KB correct
9 Correct 0 ms 204 KB correct
10 Correct 0 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 292 KB correct
13 Correct 0 ms 204 KB correct
14 Correct 2 ms 332 KB correct
15 Correct 2 ms 332 KB correct
16 Correct 2 ms 288 KB correct
17 Correct 2 ms 332 KB correct
18 Correct 1 ms 332 KB correct
19 Correct 2 ms 332 KB correct
20 Correct 2 ms 296 KB correct
21 Correct 2 ms 460 KB correct
22 Correct 1 ms 324 KB correct
23 Correct 1 ms 332 KB correct
24 Correct 1 ms 292 KB correct
25 Correct 1 ms 204 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 2 ms 332 KB correct
28 Correct 1 ms 204 KB correct
29 Correct 1 ms 204 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 332 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 332 KB correct
34 Correct 142 ms 1860 KB correct
35 Correct 146 ms 1848 KB correct
36 Correct 96 ms 1564 KB correct
37 Correct 7 ms 588 KB correct
38 Correct 139 ms 1968 KB correct
39 Correct 119 ms 1740 KB correct
40 Correct 97 ms 1612 KB correct
41 Correct 138 ms 1868 KB correct
42 Correct 140 ms 1924 KB correct
43 Correct 70 ms 1356 KB correct
44 Correct 64 ms 1196 KB correct
45 Correct 65 ms 1272 KB correct
46 Correct 49 ms 1076 KB correct
47 Correct 21 ms 848 KB correct
48 Correct 3 ms 460 KB correct
49 Correct 6 ms 640 KB correct
50 Correct 22 ms 844 KB correct
51 Correct 70 ms 1324 KB correct
52 Correct 55 ms 1100 KB correct
53 Correct 47 ms 1116 KB correct
54 Correct 70 ms 1356 KB correct
55 Correct 68 ms 1228 KB correct
56 Correct 69 ms 1284 KB correct
57 Correct 77 ms 1288 KB correct
58 Correct 0 ms 296 KB correct
59 Correct 1 ms 204 KB correct
60 Incorrect 121 ms 4996 KB WA in grader: NO
61 Halted 0 ms 0 KB -