답안 #296383

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
296383 2020-09-10T14:03:39 Z ToMmyDong Simurgh (IOI17_simurgh) C++17
100 / 100
430 ms 7148 KB
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
#define SQ(i) ((i)*(i))
#define MEM(a, b) memset(a, (b), sizeof(a))
#define SZ(i) static_cast<int>(i.size())
#define FOR(i, j, k, in) for (int i=j; i < (k) ; i+=in)
#define RFOR(i, j, k, in) for (int i=j; i >= (k) ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define REP1(i, j) FOR(i, 1, j+1, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define ALL(_a) _a.begin(), _a.end()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define X first
#define Y second
template<typename T, typename S>
istream &operator >> (istream &is, pair<T, S> &p) {
    return is >> p.X >> p.Y;
}
#ifdef tmd
#define TIME(i) Timer i(#i)
#define debug(...) do {\
    fprintf(stderr, "%s - %d (%s) = ", __PRETTY_FUNCTION__, __LINE__, #__VA_ARGS__);\
    _do(__VA_ARGS__);\
}while(0)
template<typename T> void _do(T &&_x) {cerr<<_x<<endl;}
template<typename T,typename ...S> void _do(T &&_x, S &&..._t) {cerr <<_x <<" ,"; _do(_t...);}
template<typename _a,typename _b> ostream& operator << (ostream &_s, const pair<_a, _b> &_p) {return _s << "(" << _p.X << "," << _p.Y << ")";}
template<typename It> ostream& _OUTC(ostream &_s, It _ita, It _itb)
{
    _s << "{";
    for (It _it=_ita; _it != _itb; _it++) {
        _s <<(_it == _ita?"":",")<< *_it;
    }
    _s << "}";
    return _s;
}
template<typename _a> ostream &operator << (ostream &_s, const vector<_a> &_c) {return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s, const array<_a,2> &_c) {return _OUTC(_s, ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s, const array<_a,3> &_c) {return _OUTC(_s, ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s, const array<_a,4> &_c) {return _OUTC(_s, ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s, const array<_a,5> &_c) {return _OUTC(_s, ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s, const set<_a> &_c) {return _OUTC(_s, ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s, const deque<_a> &_c) {return _OUTC(_s, ALL(_c));}
template<typename _a,typename _b> ostream &operator << (ostream &_s, const map<_a,_b> &_c) {return _OUTC(_s,ALL(_c));}
template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;}
#define IOS()
class Timer {
private:
    string scope_name;
    chrono::high_resolution_clock::time_point start_time;
public:
    Timer (string name) : scope_name(name) {
        start_time = chrono::high_resolution_clock::now();
    }
    ~Timer () {
        auto stop_time = chrono::high_resolution_clock::now();
        auto length = chrono::duration_cast<chrono::microseconds>(stop_time - start_time).count();
        double mlength = double(length) * 0.001;
        debug(scope_name, mlength);
    }
};
#else
#define TIME(i)
#define debug(...)
#define pary(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0)
#endif
#include "simurgh.h"

int n, dfs_time;
vector<vector<pii> > edge;
vector<int> fat, fid, dfn;
vector<pii> low, G;

vector<int> tree;
vector<int> gold;
void dfs (int nd, int par) {
    dfn[nd] = dfs_time++;
    low[nd] = {dfn[nd], -1};
    for (auto &[u,id] : edge[nd]) {
        if (u == par) continue;
        if (dfn[u] == -1) { // tree
            fat[u] = nd;
            fid[u] = id;
            dfs(u, nd);
            low[nd] = min(low[nd], low[u]);
            tree.eb(id);
        } else if (dfn[u] < dfn[nd]) {
            low[nd] = min(low[nd], pii(dfn[u], id));
        }
    }
    if (low[nd].X == dfn[nd] && par != -1) {
        gold[fid[nd]] = 1;
        debug(fid[nd]);
    }
}

struct Djs {
    vector<int> djs;
    Djs(int N) : djs(N) {
        iota(ALL(djs), 0);
    }
    int fnd (int x) {
        return x == djs[x] ? x : djs[x] = fnd(djs[x]);
    }
    bool mrg (int x, int y) {
        x = fnd(x), y = fnd(y);
        if (x == y) return false;
        djs[fnd(x)] = fnd(y);
        return true;
    }
};

void build_cycle (vector<int> cycle) {
    vector<int> res;
    debug(cycle);
    for (int i=0; i<SZ(cycle); i++) {
        Djs djs(n);
        vector<int> qry;
        for (int j=0; j<SZ(cycle); j++) {
            if (i != j) {
                qry.eb(cycle[j]);
                assert(djs.mrg(G[cycle[j]].X, G[cycle[j]].Y));
            }
        }
        for (int id : tree) {
            if (djs.mrg(G[id].X, G[id].Y)) {
                qry.eb(id);
            }
        }
        res.eb(count_common_roads(qry));
    }

    debug(res);
    int mn = *max_element(ALL(res));
    for (int i=0; i<SZ(cycle); i++) {
        gold[cycle[i]] = (res[i] == mn) ? -1 : 1;
    }
}

void build_alt (vector<int> cycle, int known, int want) {

    vector<int> res;
    debug(cycle, known, want);
    for (int i : {known, want}) {
        Djs djs(n);
        vector<int> qry;
        for (int j=0; j<SZ(cycle); j++) {
            if (i != cycle[j]) {
                qry.eb(cycle[j]);
                assert(djs.mrg(G[cycle[j]].X, G[cycle[j]].Y));
            }
        }
        for (int id : tree) {
            if (djs.mrg(G[id].X, G[id].Y)) {
                qry.eb(id);
            }
        }
        res.eb(count_common_roads(qry));
    }

    gold[want] = gold[known] * ((res[0] == res[1]) ? 1 : -1);
}

void check (int id) {
    if (gold[id]) return;
    int be = low[G[id].Y].Y;
    int nd = G[be].Y;
    vector<int> cycle = {be};

    int known = -1;
    while (nd != G[be].X) {
        if (gold[fid[nd]]) known = fid[nd];
        cycle.eb(fid[nd]);
        nd = fat[nd];
    }
    
    if (known == -1) build_cycle(cycle);
    else build_alt(cycle, known, id);
}

void bin_gold (int nd) {
    int cnt = 0;

    vector<int> E;
    for (auto &[_,id]: edge[nd]) {
        if (gold[id] == 0) E.eb(id);
    }

    int lst = 0;
    while (cnt < SZ(E)) {
        int L = lst, R = SZ(E) + 1;
        while (L < R - 1) {
            int M = (L + R) >> 1;
            vector<int> qry;
            Djs djs(n);
            for (int i=0; i<M; i++) {
                qry.eb(E[i]);
                assert(djs.mrg(G[E[i]].X, G[E[i]].Y)); 
            }
            int tree_gold = 0;
            for (int id : tree) {
                if (djs.mrg(G[id].X, G[id].Y)) {
                    tree_gold += gold[id] == 1;
                    qry.eb(id);
                }
            }

            int sum = count_common_roads(qry) - tree_gold;
            if (sum > cnt) R = M;
            else L = M;
        }
        debug(nd, cnt, R);
        if (R == SZ(E) + 1) break;
        else gold[E[R-1]] = 1;
        cnt++;
        lst = R;
    }
    for (int id : E) {
        if (gold[id] == 0) gold[id] = -1;
    }
}

std::vector<int> find_roads(int N, std::vector<int> u, std::vector<int> v) {
    n = N;
    dfs_time = 0;
    edge.resize(n);
    fat.resize(n);
    fid.resize(n);
    low.resize(n);
    dfn.resize(n, -1);
    gold.resize(SZ(u));
    for (int i=0; i<SZ(u); i++) {
        edge[u[i]].eb(v[i], i);
        edge[v[i]].eb(u[i], i);
        G.eb(v[i], u[i]);
    }
    debug(N, G);
    dfs(0, -1);
    for (int id=0; id<SZ(G); id++) {
        if (dfn[G[id].X] > dfn[G[id].Y]) {
            swap(G[id].X, G[id].Y);
        }
    }

    for (int id : tree) {
        check(id);
        debug(id, gold[id]);
    }

    for (int i=0; i<n; i++) {
        bin_gold(i);
    }
    debug(gold);

    vector<int> ans;
    for (int i=0; i<SZ(G); i++) {
        if (gold[i] == 1) ans.eb(i);
    }
    assert(ans.size() == n-1);
    return ans;

}

Compilation message

In file included from /usr/include/c++/9/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:33,
                 from simurgh.cpp:2:
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:270:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  270 |     assert(ans.size() == n-1);
      |            ~~~~~~~~~~~^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB correct
2 Correct 0 ms 256 KB correct
3 Correct 0 ms 256 KB correct
4 Correct 0 ms 256 KB correct
5 Correct 0 ms 256 KB correct
6 Correct 1 ms 256 KB correct
7 Correct 0 ms 256 KB correct
8 Correct 0 ms 256 KB correct
9 Correct 1 ms 256 KB correct
10 Correct 0 ms 256 KB correct
11 Correct 0 ms 256 KB correct
12 Correct 1 ms 256 KB correct
13 Correct 1 ms 256 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB correct
2 Correct 0 ms 256 KB correct
3 Correct 0 ms 256 KB correct
4 Correct 0 ms 256 KB correct
5 Correct 0 ms 256 KB correct
6 Correct 1 ms 256 KB correct
7 Correct 0 ms 256 KB correct
8 Correct 0 ms 256 KB correct
9 Correct 1 ms 256 KB correct
10 Correct 0 ms 256 KB correct
11 Correct 0 ms 256 KB correct
12 Correct 1 ms 256 KB correct
13 Correct 1 ms 256 KB correct
14 Correct 3 ms 384 KB correct
15 Correct 3 ms 512 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 2 ms 384 KB correct
20 Correct 2 ms 384 KB correct
21 Correct 2 ms 376 KB correct
22 Correct 2 ms 384 KB correct
23 Correct 2 ms 384 KB correct
24 Correct 2 ms 384 KB correct
25 Correct 1 ms 384 KB correct
26 Correct 2 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 2 ms 384 KB correct
31 Correct 2 ms 384 KB correct
32 Correct 2 ms 384 KB correct
33 Correct 2 ms 384 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB correct
2 Correct 0 ms 256 KB correct
3 Correct 0 ms 256 KB correct
4 Correct 0 ms 256 KB correct
5 Correct 0 ms 256 KB correct
6 Correct 1 ms 256 KB correct
7 Correct 0 ms 256 KB correct
8 Correct 0 ms 256 KB correct
9 Correct 1 ms 256 KB correct
10 Correct 0 ms 256 KB correct
11 Correct 0 ms 256 KB correct
12 Correct 1 ms 256 KB correct
13 Correct 1 ms 256 KB correct
14 Correct 3 ms 384 KB correct
15 Correct 3 ms 512 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 2 ms 384 KB correct
20 Correct 2 ms 384 KB correct
21 Correct 2 ms 376 KB correct
22 Correct 2 ms 384 KB correct
23 Correct 2 ms 384 KB correct
24 Correct 2 ms 384 KB correct
25 Correct 1 ms 384 KB correct
26 Correct 2 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 2 ms 384 KB correct
31 Correct 2 ms 384 KB correct
32 Correct 2 ms 384 KB correct
33 Correct 2 ms 384 KB correct
34 Correct 66 ms 1792 KB correct
35 Correct 65 ms 1792 KB correct
36 Correct 57 ms 1536 KB correct
37 Correct 16 ms 384 KB correct
38 Correct 66 ms 1792 KB correct
39 Correct 69 ms 1792 KB correct
40 Correct 56 ms 1664 KB correct
41 Correct 63 ms 2044 KB correct
42 Correct 62 ms 1792 KB correct
43 Correct 37 ms 1312 KB correct
44 Correct 33 ms 1024 KB correct
45 Correct 35 ms 1152 KB correct
46 Correct 32 ms 1024 KB correct
47 Correct 21 ms 640 KB correct
48 Correct 5 ms 384 KB correct
49 Correct 13 ms 512 KB correct
50 Correct 22 ms 640 KB correct
51 Correct 36 ms 1152 KB correct
52 Correct 33 ms 1024 KB correct
53 Correct 32 ms 1024 KB correct
54 Correct 38 ms 1280 KB correct
55 Correct 35 ms 1152 KB correct
56 Correct 36 ms 1196 KB correct
57 Correct 35 ms 1152 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB correct
2 Correct 1 ms 384 KB correct
3 Correct 209 ms 4724 KB correct
4 Correct 337 ms 6380 KB correct
5 Correct 333 ms 7020 KB correct
6 Correct 338 ms 7100 KB correct
7 Correct 333 ms 6892 KB correct
8 Correct 345 ms 7148 KB correct
9 Correct 339 ms 7028 KB correct
10 Correct 372 ms 7016 KB correct
11 Correct 339 ms 7024 KB correct
12 Correct 343 ms 7016 KB correct
13 Correct 1 ms 256 KB correct
14 Correct 345 ms 7008 KB correct
15 Correct 355 ms 6892 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB correct
2 Correct 0 ms 256 KB correct
3 Correct 0 ms 256 KB correct
4 Correct 0 ms 256 KB correct
5 Correct 0 ms 256 KB correct
6 Correct 1 ms 256 KB correct
7 Correct 0 ms 256 KB correct
8 Correct 0 ms 256 KB correct
9 Correct 1 ms 256 KB correct
10 Correct 0 ms 256 KB correct
11 Correct 0 ms 256 KB correct
12 Correct 1 ms 256 KB correct
13 Correct 1 ms 256 KB correct
14 Correct 3 ms 384 KB correct
15 Correct 3 ms 512 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 2 ms 384 KB correct
20 Correct 2 ms 384 KB correct
21 Correct 2 ms 376 KB correct
22 Correct 2 ms 384 KB correct
23 Correct 2 ms 384 KB correct
24 Correct 2 ms 384 KB correct
25 Correct 1 ms 384 KB correct
26 Correct 2 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 2 ms 384 KB correct
31 Correct 2 ms 384 KB correct
32 Correct 2 ms 384 KB correct
33 Correct 2 ms 384 KB correct
34 Correct 66 ms 1792 KB correct
35 Correct 65 ms 1792 KB correct
36 Correct 57 ms 1536 KB correct
37 Correct 16 ms 384 KB correct
38 Correct 66 ms 1792 KB correct
39 Correct 69 ms 1792 KB correct
40 Correct 56 ms 1664 KB correct
41 Correct 63 ms 2044 KB correct
42 Correct 62 ms 1792 KB correct
43 Correct 37 ms 1312 KB correct
44 Correct 33 ms 1024 KB correct
45 Correct 35 ms 1152 KB correct
46 Correct 32 ms 1024 KB correct
47 Correct 21 ms 640 KB correct
48 Correct 5 ms 384 KB correct
49 Correct 13 ms 512 KB correct
50 Correct 22 ms 640 KB correct
51 Correct 36 ms 1152 KB correct
52 Correct 33 ms 1024 KB correct
53 Correct 32 ms 1024 KB correct
54 Correct 38 ms 1280 KB correct
55 Correct 35 ms 1152 KB correct
56 Correct 36 ms 1196 KB correct
57 Correct 35 ms 1152 KB correct
58 Correct 0 ms 256 KB correct
59 Correct 1 ms 384 KB correct
60 Correct 209 ms 4724 KB correct
61 Correct 337 ms 6380 KB correct
62 Correct 333 ms 7020 KB correct
63 Correct 338 ms 7100 KB correct
64 Correct 333 ms 6892 KB correct
65 Correct 345 ms 7148 KB correct
66 Correct 339 ms 7028 KB correct
67 Correct 372 ms 7016 KB correct
68 Correct 339 ms 7024 KB correct
69 Correct 343 ms 7016 KB correct
70 Correct 1 ms 256 KB correct
71 Correct 345 ms 7008 KB correct
72 Correct 355 ms 6892 KB correct
73 Correct 1 ms 256 KB correct
74 Correct 430 ms 6892 KB correct
75 Correct 346 ms 6764 KB correct
76 Correct 164 ms 2940 KB correct
77 Correct 334 ms 6892 KB correct
78 Correct 332 ms 6896 KB correct
79 Correct 357 ms 7148 KB correct
80 Correct 332 ms 6836 KB correct
81 Correct 304 ms 6128 KB correct
82 Correct 333 ms 7020 KB correct
83 Correct 250 ms 3952 KB correct
84 Correct 194 ms 5112 KB correct
85 Correct 186 ms 4492 KB correct
86 Correct 153 ms 3068 KB correct
87 Correct 134 ms 2428 KB correct
88 Correct 118 ms 1792 KB correct
89 Correct 124 ms 1792 KB correct
90 Correct 111 ms 1664 KB correct
91 Correct 47 ms 512 KB correct
92 Correct 20 ms 384 KB correct
93 Correct 177 ms 4592 KB correct
94 Correct 152 ms 3060 KB correct
95 Correct 151 ms 2940 KB correct
96 Correct 115 ms 1664 KB correct
97 Correct 122 ms 1792 KB correct
98 Correct 129 ms 2428 KB correct
99 Correct 122 ms 1792 KB correct
100 Correct 74 ms 768 KB correct
101 Correct 32 ms 384 KB correct
102 Correct 173 ms 3704 KB correct
103 Correct 182 ms 3824 KB correct
104 Correct 182 ms 3824 KB correct
105 Correct 178 ms 3696 KB correct