Submission #70374

# Submission time Handle Problem Language Result Execution time Memory
70374 2018-08-22T17:37:21 Z top34051 Simurgh (IOI17_simurgh) C++17
100 / 100
303 ms 16344 KB
#include "simurgh.h"
#include<bits/stdc++.h>
using namespace std;

#define pii pair<int,int>
#define X first
#define Y second
const int maxn = 500 + 5;
const int maxm = 250000 + 5;

int n,m;
pii e[maxm];

int head[maxn];
vector<pii> way[maxn], edge[maxn];
int ban[maxm], res[maxm];

vector<int> tree;

int h[maxn], par[maxn], rec[maxn];

vector<int> p;
int cut[maxm];

int ask_count;

int ask(int x, int y) {
    for(auto &id : tree) if(id==x) id = y;
    ask_count++;
//    if(ask_count > 8000) assert(0);
    int ret = count_common_roads(tree);
    for(auto &id : tree) if(id==y) id = x;
    return ret;
}

void dfs(int u, int last, int lid) {
    h[u] = h[last] + 1;
    par[u] = last;
    rec[u] = lid;
    for(auto t : way[u]) {
        int v = t.X, id = t.Y;
        if(v==last) continue;
        dfs(v,u,id);
    }
}

void lca(int u, int v) {
    if(u==v) return ;
    if(h[u] >= h[v]) {
        p.push_back(rec[u]);
        lca(par[u],v);
    }
    else lca(v,u);
}

int findhead(int x) {
    if(x==head[x]) return x;
    return head[x] = findhead(head[x]);
}

int get(int x, int l, int r) {
    int val = 0;
    vector<int> vec;
    for(int i=0;i<n;i++) head[i] = i;
    for(int i=l;i<=r;i++) {
        int v = edge[x][i].X, id = edge[x][i].Y;
        head[findhead(x)] = findhead(v);
        vec.push_back(id);
        val -= res[id];
    }
    for(auto id : tree) {
    int u = e[id].X, v = e[id].Y;
        if(findhead(u)!=findhead(v)) {
        head[findhead(u)] = findhead(v);
            vec.push_back(id);
            val -= res[id];
        }
    }
    ask_count++;
    if(ask_count > 8000) assert(0);
    val += count_common_roads(vec);
    return val;
}

vector<int> find_roads(int N, vector<int> U, vector<int> V) {
	n = N; m = U.size();
    for(int i=0;i<n;i++) head[i] = i;
	for(int i=0;i<m;i++) {
		int u = U[i], v = V[i];
        e[i] = {u,v};
        if(findhead(u)!=findhead(v)) {
            head[findhead(u)] = findhead(v);
            way[u].push_back({v,i});
            way[v].push_back({u,i});
            tree.push_back(i);
            ban[i] = 1;
            res[i] = -1;
        }
        else {
            edge[u].push_back({v,i});
            edge[v].push_back({u,i});
        }
	}

    dfs(0,-1,-1);

    int ori = ask(-1,-1);
    for(int i=0;i<m;i++) {
        if(ban[i]) continue;
        int u = e[i].X, v = e[i].Y, wow = i;

        p.clear();
        lca(u,v);

        int good = 0;
        for(auto id : p) if(res[id]==-1) good = 1;
        if(!good) continue;

        int x = -1;
        for(auto id : p) if(res[id]!=-1) x = id;
        if(x==-1) {
            for(auto id : p) cut[id] = ask(id,wow);
            for(auto id : p) if(cut[id] - ori == 1) res[wow] = 1;
            for(auto id : p) res[id] = ori - cut[id] + res[wow];
        }
        else {
            res[wow] = ask(x,wow) - ori + res[x];
            for(auto id : p) if(res[id]==-1) res[id] = ori - ask(id,wow) + res[wow];
        }
    }
    for(int i=0;i<m;i++) {
        if(res[i] == -1) res[i] = 1;
//        if(ban[i]) printf("%d %d : %d\n",e[i].X,e[i].Y,res[i]);
    }

    for(int x=0;x<n;x++) {
//        printf("x = %d : ask count = %d\n",x,ask_count);
        for(int ext=get(x,0,edge[x].size()-1);ext>0;ext--) {
            int l = 0, r = edge[x].size()-1, mid, pos = -1;
            while(l<=r) {
                mid = (l+r)/2;
                if(get(x,0,mid) >= 1) {
                    pos = edge[x][mid].Y;
                    r = mid-1;
                }
                else l = mid+1;
            }
            if(pos==-1) break;
            res[pos] = 1;
//            printf("\tedge %d %d is good\n",e[pos].X,e[pos].Y);
        }
    }

    vector<int> ans;
    for(int i=0;i<m;i++) if(res[i]) ans.push_back(i);
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 412 KB correct
4 Correct 2 ms 428 KB correct
5 Correct 2 ms 460 KB correct
6 Correct 3 ms 460 KB correct
7 Correct 3 ms 508 KB correct
8 Correct 2 ms 636 KB correct
9 Correct 2 ms 636 KB correct
10 Correct 2 ms 636 KB correct
11 Correct 3 ms 636 KB correct
12 Correct 3 ms 636 KB correct
13 Correct 3 ms 636 KB correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 412 KB correct
4 Correct 2 ms 428 KB correct
5 Correct 2 ms 460 KB correct
6 Correct 3 ms 460 KB correct
7 Correct 3 ms 508 KB correct
8 Correct 2 ms 636 KB correct
9 Correct 2 ms 636 KB correct
10 Correct 2 ms 636 KB correct
11 Correct 3 ms 636 KB correct
12 Correct 3 ms 636 KB correct
13 Correct 3 ms 636 KB correct
14 Correct 5 ms 720 KB correct
15 Correct 4 ms 720 KB correct
16 Correct 5 ms 720 KB correct
17 Correct 4 ms 720 KB correct
18 Correct 3 ms 720 KB correct
19 Correct 5 ms 720 KB correct
20 Correct 4 ms 720 KB correct
21 Correct 4 ms 720 KB correct
22 Correct 4 ms 720 KB correct
23 Correct 4 ms 748 KB correct
24 Correct 4 ms 748 KB correct
25 Correct 4 ms 748 KB correct
26 Correct 3 ms 748 KB correct
27 Correct 2 ms 748 KB correct
28 Correct 3 ms 748 KB correct
29 Correct 2 ms 748 KB correct
30 Correct 4 ms 748 KB correct
31 Correct 3 ms 748 KB correct
32 Correct 2 ms 748 KB correct
33 Correct 3 ms 748 KB correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 412 KB correct
4 Correct 2 ms 428 KB correct
5 Correct 2 ms 460 KB correct
6 Correct 3 ms 460 KB correct
7 Correct 3 ms 508 KB correct
8 Correct 2 ms 636 KB correct
9 Correct 2 ms 636 KB correct
10 Correct 2 ms 636 KB correct
11 Correct 3 ms 636 KB correct
12 Correct 3 ms 636 KB correct
13 Correct 3 ms 636 KB correct
14 Correct 5 ms 720 KB correct
15 Correct 4 ms 720 KB correct
16 Correct 5 ms 720 KB correct
17 Correct 4 ms 720 KB correct
18 Correct 3 ms 720 KB correct
19 Correct 5 ms 720 KB correct
20 Correct 4 ms 720 KB correct
21 Correct 4 ms 720 KB correct
22 Correct 4 ms 720 KB correct
23 Correct 4 ms 748 KB correct
24 Correct 4 ms 748 KB correct
25 Correct 4 ms 748 KB correct
26 Correct 3 ms 748 KB correct
27 Correct 2 ms 748 KB correct
28 Correct 3 ms 748 KB correct
29 Correct 2 ms 748 KB correct
30 Correct 4 ms 748 KB correct
31 Correct 3 ms 748 KB correct
32 Correct 2 ms 748 KB correct
33 Correct 3 ms 748 KB correct
34 Correct 53 ms 1916 KB correct
35 Correct 59 ms 1916 KB correct
36 Correct 52 ms 1916 KB correct
37 Correct 16 ms 1916 KB correct
38 Correct 53 ms 1992 KB correct
39 Correct 60 ms 1992 KB correct
40 Correct 50 ms 1992 KB correct
41 Correct 52 ms 1992 KB correct
42 Correct 51 ms 1992 KB correct
43 Correct 12 ms 1992 KB correct
44 Correct 12 ms 1992 KB correct
45 Correct 12 ms 1992 KB correct
46 Correct 12 ms 1992 KB correct
47 Correct 13 ms 1992 KB correct
48 Correct 5 ms 1992 KB correct
49 Correct 8 ms 1992 KB correct
50 Correct 10 ms 1992 KB correct
51 Correct 14 ms 1992 KB correct
52 Correct 13 ms 1992 KB correct
53 Correct 14 ms 1992 KB correct
54 Correct 14 ms 1992 KB correct
55 Correct 16 ms 1992 KB correct
56 Correct 14 ms 1992 KB correct
57 Correct 16 ms 1992 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1992 KB correct
2 Correct 2 ms 1992 KB correct
3 Correct 163 ms 4604 KB correct
4 Correct 245 ms 6312 KB correct
5 Correct 253 ms 6332 KB correct
6 Correct 266 ms 6348 KB correct
7 Correct 247 ms 6464 KB correct
8 Correct 242 ms 6464 KB correct
9 Correct 286 ms 6464 KB correct
10 Correct 258 ms 6464 KB correct
11 Correct 271 ms 6464 KB correct
12 Correct 255 ms 6464 KB correct
13 Correct 4 ms 6464 KB correct
14 Correct 296 ms 6464 KB correct
15 Correct 282 ms 6512 KB correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 412 KB correct
4 Correct 2 ms 428 KB correct
5 Correct 2 ms 460 KB correct
6 Correct 3 ms 460 KB correct
7 Correct 3 ms 508 KB correct
8 Correct 2 ms 636 KB correct
9 Correct 2 ms 636 KB correct
10 Correct 2 ms 636 KB correct
11 Correct 3 ms 636 KB correct
12 Correct 3 ms 636 KB correct
13 Correct 3 ms 636 KB correct
14 Correct 5 ms 720 KB correct
15 Correct 4 ms 720 KB correct
16 Correct 5 ms 720 KB correct
17 Correct 4 ms 720 KB correct
18 Correct 3 ms 720 KB correct
19 Correct 5 ms 720 KB correct
20 Correct 4 ms 720 KB correct
21 Correct 4 ms 720 KB correct
22 Correct 4 ms 720 KB correct
23 Correct 4 ms 748 KB correct
24 Correct 4 ms 748 KB correct
25 Correct 4 ms 748 KB correct
26 Correct 3 ms 748 KB correct
27 Correct 2 ms 748 KB correct
28 Correct 3 ms 748 KB correct
29 Correct 2 ms 748 KB correct
30 Correct 4 ms 748 KB correct
31 Correct 3 ms 748 KB correct
32 Correct 2 ms 748 KB correct
33 Correct 3 ms 748 KB correct
34 Correct 53 ms 1916 KB correct
35 Correct 59 ms 1916 KB correct
36 Correct 52 ms 1916 KB correct
37 Correct 16 ms 1916 KB correct
38 Correct 53 ms 1992 KB correct
39 Correct 60 ms 1992 KB correct
40 Correct 50 ms 1992 KB correct
41 Correct 52 ms 1992 KB correct
42 Correct 51 ms 1992 KB correct
43 Correct 12 ms 1992 KB correct
44 Correct 12 ms 1992 KB correct
45 Correct 12 ms 1992 KB correct
46 Correct 12 ms 1992 KB correct
47 Correct 13 ms 1992 KB correct
48 Correct 5 ms 1992 KB correct
49 Correct 8 ms 1992 KB correct
50 Correct 10 ms 1992 KB correct
51 Correct 14 ms 1992 KB correct
52 Correct 13 ms 1992 KB correct
53 Correct 14 ms 1992 KB correct
54 Correct 14 ms 1992 KB correct
55 Correct 16 ms 1992 KB correct
56 Correct 14 ms 1992 KB correct
57 Correct 16 ms 1992 KB correct
58 Correct 2 ms 1992 KB correct
59 Correct 2 ms 1992 KB correct
60 Correct 163 ms 4604 KB correct
61 Correct 245 ms 6312 KB correct
62 Correct 253 ms 6332 KB correct
63 Correct 266 ms 6348 KB correct
64 Correct 247 ms 6464 KB correct
65 Correct 242 ms 6464 KB correct
66 Correct 286 ms 6464 KB correct
67 Correct 258 ms 6464 KB correct
68 Correct 271 ms 6464 KB correct
69 Correct 255 ms 6464 KB correct
70 Correct 4 ms 6464 KB correct
71 Correct 296 ms 6464 KB correct
72 Correct 282 ms 6512 KB correct
73 Correct 2 ms 6512 KB correct
74 Correct 293 ms 6512 KB correct
75 Correct 270 ms 7164 KB correct
76 Correct 173 ms 7164 KB correct
77 Correct 303 ms 8600 KB correct
78 Correct 295 ms 9744 KB correct
79 Correct 257 ms 10472 KB correct
80 Correct 280 ms 11308 KB correct
81 Correct 246 ms 11504 KB correct
82 Correct 298 ms 13036 KB correct
83 Correct 243 ms 13036 KB correct
84 Correct 72 ms 13036 KB correct
85 Correct 62 ms 13036 KB correct
86 Correct 52 ms 13036 KB correct
87 Correct 42 ms 13036 KB correct
88 Correct 41 ms 13036 KB correct
89 Correct 41 ms 13036 KB correct
90 Correct 34 ms 13036 KB correct
91 Correct 20 ms 13036 KB correct
92 Correct 16 ms 13036 KB correct
93 Correct 58 ms 13872 KB correct
94 Correct 48 ms 13872 KB correct
95 Correct 44 ms 13872 KB correct
96 Correct 42 ms 13872 KB correct
97 Correct 39 ms 13872 KB correct
98 Correct 49 ms 13872 KB correct
99 Correct 38 ms 13872 KB correct
100 Correct 23 ms 13872 KB correct
101 Correct 16 ms 13872 KB correct
102 Correct 60 ms 14980 KB correct
103 Correct 57 ms 15560 KB correct
104 Correct 56 ms 15904 KB correct
105 Correct 55 ms 16344 KB correct