Submission #304243

# Submission time Handle Problem Language Result Execution time Memory
304243 2020-09-21T04:58:10 Z jhnah917 Split the Attractions (IOI19_split) C++14
18 / 100
246 ms 22516 KB
#ifndef LOCAL
#include "split.h"
#endif
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;

typedef pair<int, int> p;

namespace UnionFind{
    int par[101010], w[101010];
    void uf_init(){ iota(par, par+101010, 0); fill(w, w+101010, 1); }
    int uf_find(int v){ return v == par[v] ? v : par[v] = uf_find(par[v]); }
    bool uf_merge(int u, int v){
        u = uf_find(u); v = uf_find(v);
        if(u == v) return 0;
        par[u] = v; w[v] += w[u]; return 1;
    }
}
using namespace UnionFind;

int n, m, a, b, c, sz[101010], chk[101010], use_at_answer[101010]; p tmp[3];
vector<int> comp_graph[101010], tree[101010], all_graph[101010], dfn;

int dfs_get_size(int v, int t = -1){
    sz[v] = 1;
    for(auto i : tree[v]) if(i != t) sz[v] += dfs_get_size(i, v);
    return sz[v];
}

int dfs_get_centroid(int v, int t = -1){
    for(auto i : tree[v]) if(i != t && sz[i]*2 >= n) return dfs_get_centroid(i, v);
    return v;
}

void dfs_on_spanning_tree(int v, int t = -1){
    dfn.push_back(v);
    for(auto i : tree[v]) if(i != t) uf_merge(v, i), dfs_on_spanning_tree(i, v);
}

void dfs_on_compressed_graph(int v){
    chk[v] = 1; dfn.push_back(v);
    for(auto i : comp_graph[v]) if(!chk[i]) dfs_on_compressed_graph(i);
}

void dfs_on_all_graph(int v){
    chk[v] = 1; dfn.push_back(v);
    for(auto i : all_graph[v]) if(!chk[i] && use_at_answer[v] == use_at_answer[i]) dfs_on_all_graph(i);
}

vector<int> get_answer(){
    vector<int> ret(n, tmp[2].y);
    memset(chk, 0, sizeof chk);
    for(int i=0; i<n; i++) if(!chk[i]) {
        dfn.clear(); dfs_on_all_graph(i);
        if(use_at_answer[i]) for(int j=0; j < tmp[0].x; j++) ret[dfn[j]] = tmp[0].y;
        else for(int j=0; j<tmp[1].x; j++) ret[dfn[j]] = tmp[1].y;
    }
    return ret;
}

vector<int> attempt(int cent, const vector<int> &P, const vector<int> &Q){
    for(int i=0; i<n; i++) use_at_answer[i] = 0;
    for(int i=0; i<n; i++) comp_graph[i].clear();
    for(int i=0; i<m; i++){
        int s = P[i], e = Q[i];
        if(s == cent || e == cent || uf_find(s) == uf_find(e)) continue;
        comp_graph[uf_find(s)].push_back(uf_find(e));
        comp_graph[uf_find(e)].push_back(uf_find(s));
    }
    for(int i=0; i<n; i++) if(i == uf_find(i)) compress(comp_graph[i]);
    
    for(int i=0; i<n; i++) if(!chk[i] && i == uf_find(i) && i != cent) {
        dfn.clear(); dfs_on_compressed_graph(i); int all = 0;
        for(auto j : dfn){
            use_at_answer[j] = 1; all += w[uf_find(j)];
            if(all >= tmp[0].x) break;
        }
        if(all >= tmp[0].x) return get_answer();
        for(auto j : dfn) use_at_answer[j] = 0;
    }
    return vector<int>();
}

vector<int> find_split(int N, int A, int B, int C, vector<int> P, vector<int> Q){
    n = N; m = P.size(); a = A; b = B; c = C;
    tmp[0] = p(a, 1); tmp[1] = p(b, 2); tmp[2] = p(c, 3); sort(tmp, tmp+3);
    uf_init();
    for(int i=0; i<m; i++){
        int s = P[i], e = Q[i];
        if(uf_merge(s, e)) tree[s].push_back(e), tree[e].push_back(s);
        all_graph[s].push_back(e); all_graph[e].push_back(s);
    }
    uf_init();
    
    dfs_get_size(0); int cent = dfs_get_centroid(0);
    for(auto i : tree[cent]){
        dfn.clear(); dfs_on_spanning_tree(i, cent);
        if(dfn.size() < tmp[0].x) continue;
        for(auto j : dfn) use_at_answer[j] = 1;
        return get_answer();
    }
    
    for(int i=0; i<n; i++){
        auto now = attempt(i, P, Q);
        if(now.size()) return now;
    }
    return vector<int>(n, 0);
}

#ifdef LOCAL
int main() {
    int n, m, a, b, c;
    assert(5 == scanf("%d%d%d%d%d", &n, &m, &a, &b, &c));
    vector<int> p(m), q(m);
    for (int i=0; i<m; i++) assert(2 == scanf("%d%d", &p[i], &q[i]));
    
    vector<int> result = find_split(n, a, b, c, p, q);
    
    for (int i=0; i<(int)result.size(); i++) printf("%s%d", ((i>0)?" ":""), result[i]);
    printf("\n");
    return 0;
}
#endif

Compilation message

split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:102:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  102 |         if(dfn.size() < tmp[0].x) continue;
      |                       ^
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8576 KB ok, correct split
2 Correct 7 ms 8576 KB ok, correct split
3 Correct 6 ms 8576 KB ok, correct split
4 Correct 7 ms 8608 KB ok, correct split
5 Correct 7 ms 8576 KB ok, correct split
6 Correct 7 ms 8576 KB ok, correct split
7 Correct 208 ms 22428 KB ok, correct split
8 Correct 206 ms 21076 KB ok, correct split
9 Correct 193 ms 20620 KB ok, correct split
10 Correct 203 ms 22516 KB ok, correct split
11 Correct 198 ms 21748 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 7 ms 8576 KB ok, correct split
2 Correct 6 ms 8576 KB ok, correct split
3 Correct 6 ms 8576 KB ok, correct split
4 Correct 229 ms 21424 KB ok, correct split
5 Correct 170 ms 18548 KB ok, correct split
6 Correct 206 ms 22516 KB ok, correct split
7 Correct 196 ms 20972 KB ok, correct split
8 Correct 246 ms 20500 KB ok, correct split
9 Correct 188 ms 18000 KB ok, correct split
10 Correct 109 ms 18792 KB ok, correct split
11 Correct 112 ms 18792 KB ok, correct split
12 Correct 100 ms 18840 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 7 ms 8576 KB ok, correct split
2 Correct 183 ms 18520 KB ok, correct split
3 Correct 58 ms 12664 KB ok, correct split
4 Correct 6 ms 8576 KB ok, correct split
5 Correct 198 ms 19828 KB ok, correct split
6 Correct 199 ms 19664 KB ok, correct split
7 Correct 212 ms 19940 KB ok, correct split
8 Correct 209 ms 20340 KB ok, correct split
9 Correct 210 ms 19700 KB ok, correct split
10 Incorrect 98 ms 11804 KB invalid split: #1=3243, #2=30090, #3=0
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8576 KB ok, correct split
2 Correct 6 ms 8192 KB ok, no valid answer
3 Correct 7 ms 8576 KB ok, correct split
4 Correct 6 ms 8576 KB ok, correct split
5 Correct 6 ms 8704 KB ok, correct split
6 Correct 7 ms 8576 KB ok, correct split
7 Correct 6 ms 8640 KB ok, correct split
8 Correct 6 ms 8576 KB ok, correct split
9 Correct 9 ms 8960 KB ok, correct split
10 Correct 9 ms 8960 KB ok, correct split
11 Correct 6 ms 8704 KB ok, correct split
12 Incorrect 11 ms 8960 KB invalid split: #1=5, #2=151, #3=2245
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8576 KB ok, correct split
2 Correct 7 ms 8576 KB ok, correct split
3 Correct 6 ms 8576 KB ok, correct split
4 Correct 7 ms 8608 KB ok, correct split
5 Correct 7 ms 8576 KB ok, correct split
6 Correct 7 ms 8576 KB ok, correct split
7 Correct 208 ms 22428 KB ok, correct split
8 Correct 206 ms 21076 KB ok, correct split
9 Correct 193 ms 20620 KB ok, correct split
10 Correct 203 ms 22516 KB ok, correct split
11 Correct 198 ms 21748 KB ok, correct split
12 Correct 7 ms 8576 KB ok, correct split
13 Correct 6 ms 8576 KB ok, correct split
14 Correct 6 ms 8576 KB ok, correct split
15 Correct 229 ms 21424 KB ok, correct split
16 Correct 170 ms 18548 KB ok, correct split
17 Correct 206 ms 22516 KB ok, correct split
18 Correct 196 ms 20972 KB ok, correct split
19 Correct 246 ms 20500 KB ok, correct split
20 Correct 188 ms 18000 KB ok, correct split
21 Correct 109 ms 18792 KB ok, correct split
22 Correct 112 ms 18792 KB ok, correct split
23 Correct 100 ms 18840 KB ok, correct split
24 Correct 7 ms 8576 KB ok, correct split
25 Correct 183 ms 18520 KB ok, correct split
26 Correct 58 ms 12664 KB ok, correct split
27 Correct 6 ms 8576 KB ok, correct split
28 Correct 198 ms 19828 KB ok, correct split
29 Correct 199 ms 19664 KB ok, correct split
30 Correct 212 ms 19940 KB ok, correct split
31 Correct 209 ms 20340 KB ok, correct split
32 Correct 210 ms 19700 KB ok, correct split
33 Incorrect 98 ms 11804 KB invalid split: #1=3243, #2=30090, #3=0
34 Halted 0 ms 0 KB -