답안 #594011

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
594011 2022-07-11T22:04:08 Z keta_tsimakuridze Simurgh (IOI17_simurgh) C++14
100 / 100
229 ms 79272 KB
#include "simurgh.h"
#include<bits/stdc++.h>
#define pii pair<int,int>
#define f first
#define s second
using namespace std;
const int N = 1e6 + 5;
int f[N], t[N], CNT, P[N], h[N], Nn, up[N], imp[N], p[N];
pii mn[N];
vector<int> golden;
vector<pii> V[N], adj[N], E;
vector<int> edges, x[N];
void dfs0(int u, int p) {
    f[u] = 1;
    for(int i = 0; i < V[u].size(); i++) {
        if(V[u][i].f == p) continue;
        if(!f[V[u][i].f]) {
            dfs0(V[u][i].f, u); //cout << "++" << V[u][i].s << endl;
            edges.push_back(V[u][i].s);
            t[V[u][i].s] = 1;
        }
    }
}
int rem(int e1, int e2) {
    vector<int> x;
    for(int i = 0; i < edges.size(); i++) {
        if(e1 != edges[i]) x.push_back(edges[i]);
    }
    x.push_back(e2);
    return count_common_roads(x) - CNT;
}
void add(int a, int b, int x) {
    adj[a].push_back({b, x});
    adj[b].push_back({a, -x});
}
void dfs(int u, int p) {
    mn[u] = {Nn + 1, 0};
    P[u] = p;
    if(u) h[u] = h[p] + 1; //cout << u << endl;
    for(int i = 0; i < V[u].size(); i++) {
        int e = V[u][i].s, v = V[u][i].f;
        if(v == p) continue;
        if(t[e]) {
            up[v] = e;
            dfs(v, u);
            if(mn[v].f > h[u]) golden[e] = 1;
            else add(e, mn[v].s, rem(e, mn[v].s)), imp[mn[v].s] = 1;
            mn[u] = min(mn[u], mn[v]);
        } else {
            if(h[v] > h[u] && imp[e]) {
            int x = v;
            while(P[x] != u) {
                x = P[x];
            }
            add(up[x], e, rem(up[x], e));
            }
            if(h[v] < h[u]) mn[u] = min(mn[u], {h[v], e});
        }

    }
}
int find(int u) {
    return (p[u] == u ? u : p[u] = find(p[u]));
}
void merge(int u, int v) {
    u = find(u), v = find(v);
    p[u] = v;
}
int ask(vector<int> x, int mid) {
    for(int i = 0; i < Nn; i++) {
        p[i] = i;
    }
    vector<int> v;
    for(int i = 0; i < mid; i++) v.push_back(x[i]), merge(E[x[i]].f, E[x[i]].s);
    int c = 0;
    for(int i = 0; i < edges.size(); i++) {
        if(find(E[edges[i]].f) != find(E[edges[i]].s)) {
            v.push_back(edges[i]);
            merge(E[edges[i]].f, E[edges[i]].s);
            c += golden[edges[i]];
        }
    }
    return count_common_roads(v) - c;
}
std::vector<int> find_roads(int nn, std::vector<int> u, std::vector<int> vv) {
    int m = u.size(); Nn = nn;
    golden.resize(m);
    for(int i = 0; i < m; i++) {
        V[u[i]].push_back({vv[i], i});
        V[vv[i]].push_back({u[i], i});
        E.push_back({vv[i], u[i]});
    }
    dfs0(0, -1);
    CNT = count_common_roads(edges);
    dfs(0, -1);
    vector<int> vis(m);
    for(int i = 0; i < m; i++) {
        if(!vis[i]) {
            queue<int> q;
            q.push(i);
            h[i] = 0;
            int mx = 0, mn = 0;
            vector<int> x;
            while(q.size()) {
                int u = q.front(); q.pop();
                mx = max(mx, h[u]); mn = min(mn, h[u]);
                for(int j = 0; j < adj[u].size(); j++) {
                    if(vis[adj[u][j].f]) continue;
                    h[adj[u][j].f] = h[u] + adj[u][j].s;
                    vis[adj[u][j].f] = 1;
                    q.push(adj[u][j].f);
                }
                x.push_back(u);
            }
            if(mn == mx) continue;
            for(int j = 0; j < x.size(); j++) if(h[x[j]] == mx) golden[x[j]] = 1;
        }
    }
    for(int i = 0; i < m; i++) {
        if(t[i]) continue;
        x[min(u[i], vv[i])].push_back(i);
    }
    for(int i = 0; i < Nn; i++) {
        int cn = ask(x[i], x[i].size());// cout << cn << " _ " << i << endl;
        int L = 0;
        for(int j = 1; j <= cn; j++) {
            int l = L, r = (int)x[i].size() - 1, id = 0;
            while(l <= r) {
                int mid = (l + r) / 2;

                if(ask(x[i], mid + 1) >= j) id = mid, r = mid - 1;
                else l = mid + 1;
            }
            golden[x[i][id]] = 1;
            L = id + 1;
        }
    }

    vector<int> ans;
    for(int i = 0; i < m; i++) if(golden[i]) ans.push_back(i);
    return ans;
}

Compilation message

simurgh.cpp: In function 'void dfs0(int, int)':
simurgh.cpp:15:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |     for(int i = 0; i < V[u].size(); i++) {
      |                    ~~^~~~~~~~~~~~~
simurgh.cpp: In function 'int rem(int, int)':
simurgh.cpp:26:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for(int i = 0; i < edges.size(); i++) {
      |                    ~~^~~~~~~~~~~~~~
simurgh.cpp: In function 'void dfs(int, int)':
simurgh.cpp:40:22: 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 < V[u].size(); i++) {
      |                    ~~^~~~~~~~~~~~~
simurgh.cpp: In function 'int ask(std::vector<int>, int)':
simurgh.cpp:76:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |     for(int i = 0; i < edges.size(); i++) {
      |                    ~~^~~~~~~~~~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:107:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |                 for(int j = 0; j < adj[u].size(); j++) {
      |                                ~~^~~~~~~~~~~~~~~
simurgh.cpp:116:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |             for(int j = 0; j < x.size(); j++) if(h[x[j]] == mx) golden[x[j]] = 1;
      |                            ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 70740 KB correct
2 Correct 33 ms 70712 KB correct
3 Correct 32 ms 70780 KB correct
4 Correct 34 ms 70716 KB correct
5 Correct 33 ms 70780 KB correct
6 Correct 33 ms 70788 KB correct
7 Correct 32 ms 70760 KB correct
8 Correct 32 ms 70808 KB correct
9 Correct 34 ms 70732 KB correct
10 Correct 35 ms 70768 KB correct
11 Correct 33 ms 70692 KB correct
12 Correct 33 ms 70724 KB correct
13 Correct 37 ms 70796 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 70740 KB correct
2 Correct 33 ms 70712 KB correct
3 Correct 32 ms 70780 KB correct
4 Correct 34 ms 70716 KB correct
5 Correct 33 ms 70780 KB correct
6 Correct 33 ms 70788 KB correct
7 Correct 32 ms 70760 KB correct
8 Correct 32 ms 70808 KB correct
9 Correct 34 ms 70732 KB correct
10 Correct 35 ms 70768 KB correct
11 Correct 33 ms 70692 KB correct
12 Correct 33 ms 70724 KB correct
13 Correct 37 ms 70796 KB correct
14 Correct 35 ms 70868 KB correct
15 Correct 36 ms 70824 KB correct
16 Correct 35 ms 70808 KB correct
17 Correct 35 ms 70836 KB correct
18 Correct 33 ms 70756 KB correct
19 Correct 34 ms 70900 KB correct
20 Correct 34 ms 70844 KB correct
21 Correct 34 ms 70860 KB correct
22 Correct 32 ms 70880 KB correct
23 Correct 33 ms 70868 KB correct
24 Correct 32 ms 70860 KB correct
25 Correct 32 ms 70716 KB correct
26 Correct 32 ms 70732 KB correct
27 Correct 37 ms 70840 KB correct
28 Correct 36 ms 70720 KB correct
29 Correct 32 ms 70764 KB correct
30 Correct 34 ms 70732 KB correct
31 Correct 38 ms 70980 KB correct
32 Correct 35 ms 70860 KB correct
33 Correct 36 ms 70768 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 70740 KB correct
2 Correct 33 ms 70712 KB correct
3 Correct 32 ms 70780 KB correct
4 Correct 34 ms 70716 KB correct
5 Correct 33 ms 70780 KB correct
6 Correct 33 ms 70788 KB correct
7 Correct 32 ms 70760 KB correct
8 Correct 32 ms 70808 KB correct
9 Correct 34 ms 70732 KB correct
10 Correct 35 ms 70768 KB correct
11 Correct 33 ms 70692 KB correct
12 Correct 33 ms 70724 KB correct
13 Correct 37 ms 70796 KB correct
14 Correct 35 ms 70868 KB correct
15 Correct 36 ms 70824 KB correct
16 Correct 35 ms 70808 KB correct
17 Correct 35 ms 70836 KB correct
18 Correct 33 ms 70756 KB correct
19 Correct 34 ms 70900 KB correct
20 Correct 34 ms 70844 KB correct
21 Correct 34 ms 70860 KB correct
22 Correct 32 ms 70880 KB correct
23 Correct 33 ms 70868 KB correct
24 Correct 32 ms 70860 KB correct
25 Correct 32 ms 70716 KB correct
26 Correct 32 ms 70732 KB correct
27 Correct 37 ms 70840 KB correct
28 Correct 36 ms 70720 KB correct
29 Correct 32 ms 70764 KB correct
30 Correct 34 ms 70732 KB correct
31 Correct 38 ms 70980 KB correct
32 Correct 35 ms 70860 KB correct
33 Correct 36 ms 70768 KB correct
34 Correct 68 ms 72828 KB correct
35 Correct 68 ms 72776 KB correct
36 Correct 61 ms 72264 KB correct
37 Correct 41 ms 70832 KB correct
38 Correct 73 ms 72912 KB correct
39 Correct 64 ms 72568 KB correct
40 Correct 60 ms 72264 KB correct
41 Correct 67 ms 72752 KB correct
42 Correct 66 ms 72820 KB correct
43 Correct 46 ms 71984 KB correct
44 Correct 46 ms 71720 KB correct
45 Correct 47 ms 71808 KB correct
46 Correct 46 ms 71632 KB correct
47 Correct 42 ms 71116 KB correct
48 Correct 40 ms 70860 KB correct
49 Correct 38 ms 70864 KB correct
50 Correct 42 ms 71236 KB correct
51 Correct 47 ms 71844 KB correct
52 Correct 47 ms 71692 KB correct
53 Correct 46 ms 71600 KB correct
54 Correct 48 ms 71996 KB correct
55 Correct 46 ms 71780 KB correct
56 Correct 46 ms 71760 KB correct
57 Correct 46 ms 71840 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 70808 KB correct
2 Correct 32 ms 70704 KB correct
3 Correct 141 ms 76616 KB correct
4 Correct 221 ms 79204 KB correct
5 Correct 206 ms 79168 KB correct
6 Correct 195 ms 79168 KB correct
7 Correct 193 ms 79224 KB correct
8 Correct 193 ms 79132 KB correct
9 Correct 205 ms 79184 KB correct
10 Correct 208 ms 79168 KB correct
11 Correct 206 ms 79192 KB correct
12 Correct 215 ms 79180 KB correct
13 Correct 33 ms 70800 KB correct
14 Correct 199 ms 79192 KB correct
15 Correct 208 ms 79200 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 70740 KB correct
2 Correct 33 ms 70712 KB correct
3 Correct 32 ms 70780 KB correct
4 Correct 34 ms 70716 KB correct
5 Correct 33 ms 70780 KB correct
6 Correct 33 ms 70788 KB correct
7 Correct 32 ms 70760 KB correct
8 Correct 32 ms 70808 KB correct
9 Correct 34 ms 70732 KB correct
10 Correct 35 ms 70768 KB correct
11 Correct 33 ms 70692 KB correct
12 Correct 33 ms 70724 KB correct
13 Correct 37 ms 70796 KB correct
14 Correct 35 ms 70868 KB correct
15 Correct 36 ms 70824 KB correct
16 Correct 35 ms 70808 KB correct
17 Correct 35 ms 70836 KB correct
18 Correct 33 ms 70756 KB correct
19 Correct 34 ms 70900 KB correct
20 Correct 34 ms 70844 KB correct
21 Correct 34 ms 70860 KB correct
22 Correct 32 ms 70880 KB correct
23 Correct 33 ms 70868 KB correct
24 Correct 32 ms 70860 KB correct
25 Correct 32 ms 70716 KB correct
26 Correct 32 ms 70732 KB correct
27 Correct 37 ms 70840 KB correct
28 Correct 36 ms 70720 KB correct
29 Correct 32 ms 70764 KB correct
30 Correct 34 ms 70732 KB correct
31 Correct 38 ms 70980 KB correct
32 Correct 35 ms 70860 KB correct
33 Correct 36 ms 70768 KB correct
34 Correct 68 ms 72828 KB correct
35 Correct 68 ms 72776 KB correct
36 Correct 61 ms 72264 KB correct
37 Correct 41 ms 70832 KB correct
38 Correct 73 ms 72912 KB correct
39 Correct 64 ms 72568 KB correct
40 Correct 60 ms 72264 KB correct
41 Correct 67 ms 72752 KB correct
42 Correct 66 ms 72820 KB correct
43 Correct 46 ms 71984 KB correct
44 Correct 46 ms 71720 KB correct
45 Correct 47 ms 71808 KB correct
46 Correct 46 ms 71632 KB correct
47 Correct 42 ms 71116 KB correct
48 Correct 40 ms 70860 KB correct
49 Correct 38 ms 70864 KB correct
50 Correct 42 ms 71236 KB correct
51 Correct 47 ms 71844 KB correct
52 Correct 47 ms 71692 KB correct
53 Correct 46 ms 71600 KB correct
54 Correct 48 ms 71996 KB correct
55 Correct 46 ms 71780 KB correct
56 Correct 46 ms 71760 KB correct
57 Correct 46 ms 71840 KB correct
58 Correct 33 ms 70808 KB correct
59 Correct 32 ms 70704 KB correct
60 Correct 141 ms 76616 KB correct
61 Correct 221 ms 79204 KB correct
62 Correct 206 ms 79168 KB correct
63 Correct 195 ms 79168 KB correct
64 Correct 193 ms 79224 KB correct
65 Correct 193 ms 79132 KB correct
66 Correct 205 ms 79184 KB correct
67 Correct 208 ms 79168 KB correct
68 Correct 206 ms 79192 KB correct
69 Correct 215 ms 79180 KB correct
70 Correct 33 ms 70800 KB correct
71 Correct 199 ms 79192 KB correct
72 Correct 208 ms 79200 KB correct
73 Correct 34 ms 70816 KB correct
74 Correct 205 ms 79256 KB correct
75 Correct 217 ms 79004 KB correct
76 Correct 128 ms 73936 KB correct
77 Correct 229 ms 79236 KB correct
78 Correct 209 ms 79100 KB correct
79 Correct 215 ms 79272 KB correct
80 Correct 205 ms 79008 KB correct
81 Correct 182 ms 78176 KB correct
82 Correct 211 ms 79108 KB correct
83 Correct 156 ms 75204 KB correct
84 Correct 104 ms 76240 KB correct
85 Correct 103 ms 75844 KB correct
86 Correct 89 ms 74084 KB correct
87 Correct 87 ms 73416 KB correct
88 Correct 77 ms 72632 KB correct
89 Correct 79 ms 72604 KB correct
90 Correct 74 ms 72404 KB correct
91 Correct 54 ms 71048 KB correct
92 Correct 48 ms 70812 KB correct
93 Correct 101 ms 76028 KB correct
94 Correct 89 ms 74180 KB correct
95 Correct 91 ms 74084 KB correct
96 Correct 82 ms 72548 KB correct
97 Correct 78 ms 72720 KB correct
98 Correct 83 ms 73416 KB correct
99 Correct 78 ms 72676 KB correct
100 Correct 61 ms 71244 KB correct
101 Correct 49 ms 70916 KB correct
102 Correct 91 ms 75080 KB correct
103 Correct 95 ms 75076 KB correct
104 Correct 91 ms 75040 KB correct
105 Correct 91 ms 75096 KB correct