Submission #927365

# Submission time Handle Problem Language Result Execution time Memory
927365 2024-02-14T19:06:37 Z VMaksimoski008 Connecting Supertrees (IOI20_supertrees) C++14
40 / 100
195 ms 27224 KB
#include <bits/stdc++.h>
#include "supertrees.h"
 
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long
 
using namespace std;
 
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
 
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
 
struct DSU {
    int n, comp;
    vector<int> par, size;
 
    void config(int _n) {
        n = _n + 1;
        comp = _n;
        par.resize(n+1);
        size.resize(n+1, 1);
        for(int i=1; i<=n; i++) par[i] = i;
    }
 
    int get(int u) {
        if(u == par[u]) return u;
        return par[u] = get(par[u]);
    }
 
    bool uni(int u, int v) {
        u = get(u), v = get(v);
 
        if(u == v) return false;
        if(size[u] < size[v]) swap(u, v);
 
        size[u] += size[v];
        par[v] = u;
        comp--;
 
        return true;
    }
 
    int getComp() { return comp; }
    int getSize(int u) { return size[get(u)]; }
    bool sameSet(int u, int v) { return get(u) == get(v); }
};

vector<vector<int> > graph;
vector<bool> vis;
vector<int> G;

void dfs(int u) {
    vis[u] = 1;
    G.push_back(u);

    for(int &v : graph[u]) {
        if(vis[v]) continue;
        dfs(v);
    }
}
 
int construct(vector<vector<int> > p) {
    int n = p.size();
    vector<int> cnt(3);
 
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++) cnt[p[i][j]]++;
 
    if(cnt[0] + cnt[1] == n*n) {
        DSU dsu; dsu.config(n);
        vector<vector<int> > B(n, vector<int>(n, 0));
 
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                if(p[i][j] == 1)
                    if(dsu.uni(i, j)) B[i][j] = B[j][i] = 1;
            }
        }
 
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                if(p[i][j] == 1 && !dsu.sameSet(i, j)) return 0;
                if(p[i][j] == 0 && dsu.sameSet(i, j)) return 0;
            }
        }
 
        build(B);
        return 1;
    } else {
        vector<vector<int> > B(n, vector<int>(n, 0));
        graph.resize(n);
        vis.resize(n);

        for(int i=0; i<n; i++) {
            for(int j=i+1; j<n; j++) {
                if(p[i][j] == 2) {
                    graph[i].push_back(j);
                    graph[j].push_back(i);
                }
            }
        }

        for(int i=0; i<n; i++) {
            if(vis[i]) continue;
            G.clear();
            dfs(i);

            if(G.size() == 2) return 0;

            for(int i=0; i<G.size(); i++)
                for(int j=i+1; j<G.size(); j++)
                    if(p[G[i]][G[j]] == 0) return 0;
            
            if(G.size() > 2) {
                B[G[0]][G.back()] = B[G.back()][G[0]] = 1;
                for(int i=1; i<G.size(); i++)
                    B[G[i]][G[i-1]] = B[G[i-1]][G[i]] = 1;
            }
        }

        build(B);
        return 1;
    }
 
    return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:119:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |             for(int i=0; i<G.size(); i++)
      |                          ~^~~~~~~~~
supertrees.cpp:120:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |                 for(int j=i+1; j<G.size(); j++)
      |                                ~^~~~~~~~~
supertrees.cpp:125:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |                 for(int i=1; i<G.size(); i++)
      |                              ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 7 ms 1112 KB Output is correct
7 Correct 169 ms 22056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 7 ms 1112 KB Output is correct
7 Correct 169 ms 22056 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 167 ms 22280 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 4 ms 860 KB Output is correct
17 Correct 82 ms 12036 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 41 ms 5772 KB Output is correct
21 Correct 167 ms 22100 KB Output is correct
22 Correct 163 ms 22056 KB Output is correct
23 Correct 176 ms 22096 KB Output is correct
24 Correct 162 ms 22100 KB Output is correct
25 Correct 73 ms 12080 KB Output is correct
26 Correct 71 ms 12052 KB Output is correct
27 Correct 172 ms 21972 KB Output is correct
28 Correct 171 ms 22100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 7 ms 1116 KB Output is correct
9 Correct 164 ms 22568 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 172 ms 26308 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 860 KB Output is correct
17 Correct 82 ms 15124 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 40 ms 5972 KB Output is correct
22 Correct 165 ms 24484 KB Output is correct
23 Correct 164 ms 24148 KB Output is correct
24 Correct 170 ms 27224 KB Output is correct
25 Correct 72 ms 14168 KB Output is correct
26 Correct 74 ms 14592 KB Output is correct
27 Correct 195 ms 24060 KB Output is correct
28 Correct 175 ms 26232 KB Output is correct
29 Correct 72 ms 14164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 42 ms 5876 KB Output is correct
5 Correct 172 ms 22036 KB Output is correct
6 Correct 165 ms 22096 KB Output is correct
7 Correct 172 ms 22036 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 40 ms 5908 KB Output is correct
10 Correct 162 ms 22676 KB Output is correct
11 Correct 160 ms 22096 KB Output is correct
12 Correct 169 ms 25172 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Incorrect 0 ms 348 KB Too few ways to get from 4 to 5, should be 1 found 0
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 7 ms 1112 KB Output is correct
7 Correct 169 ms 22056 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 167 ms 22280 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 4 ms 860 KB Output is correct
17 Correct 82 ms 12036 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 41 ms 5772 KB Output is correct
21 Correct 167 ms 22100 KB Output is correct
22 Correct 163 ms 22056 KB Output is correct
23 Correct 176 ms 22096 KB Output is correct
24 Correct 162 ms 22100 KB Output is correct
25 Correct 73 ms 12080 KB Output is correct
26 Correct 71 ms 12052 KB Output is correct
27 Correct 172 ms 21972 KB Output is correct
28 Correct 171 ms 22100 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 7 ms 1116 KB Output is correct
37 Correct 164 ms 22568 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 7 ms 1372 KB Output is correct
41 Correct 172 ms 26308 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 4 ms 860 KB Output is correct
45 Correct 82 ms 15124 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 40 ms 5972 KB Output is correct
50 Correct 165 ms 24484 KB Output is correct
51 Correct 164 ms 24148 KB Output is correct
52 Correct 170 ms 27224 KB Output is correct
53 Correct 72 ms 14168 KB Output is correct
54 Correct 74 ms 14592 KB Output is correct
55 Correct 195 ms 24060 KB Output is correct
56 Correct 175 ms 26232 KB Output is correct
57 Correct 72 ms 14164 KB Output is correct
58 Correct 1 ms 344 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 4 ms 932 KB Output is correct
61 Correct 80 ms 16504 KB Output is correct
62 Correct 0 ms 344 KB Output is correct
63 Incorrect 1 ms 348 KB Too few ways to get from 0 to 1, should be 1 found 0
64 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 7 ms 1112 KB Output is correct
7 Correct 169 ms 22056 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 167 ms 22280 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 4 ms 860 KB Output is correct
17 Correct 82 ms 12036 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 41 ms 5772 KB Output is correct
21 Correct 167 ms 22100 KB Output is correct
22 Correct 163 ms 22056 KB Output is correct
23 Correct 176 ms 22096 KB Output is correct
24 Correct 162 ms 22100 KB Output is correct
25 Correct 73 ms 12080 KB Output is correct
26 Correct 71 ms 12052 KB Output is correct
27 Correct 172 ms 21972 KB Output is correct
28 Correct 171 ms 22100 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 7 ms 1116 KB Output is correct
37 Correct 164 ms 22568 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 7 ms 1372 KB Output is correct
41 Correct 172 ms 26308 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 4 ms 860 KB Output is correct
45 Correct 82 ms 15124 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 40 ms 5972 KB Output is correct
50 Correct 165 ms 24484 KB Output is correct
51 Correct 164 ms 24148 KB Output is correct
52 Correct 170 ms 27224 KB Output is correct
53 Correct 72 ms 14168 KB Output is correct
54 Correct 74 ms 14592 KB Output is correct
55 Correct 195 ms 24060 KB Output is correct
56 Correct 175 ms 26232 KB Output is correct
57 Correct 72 ms 14164 KB Output is correct
58 Correct 1 ms 344 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 42 ms 5876 KB Output is correct
62 Correct 172 ms 22036 KB Output is correct
63 Correct 165 ms 22096 KB Output is correct
64 Correct 172 ms 22036 KB Output is correct
65 Correct 0 ms 344 KB Output is correct
66 Correct 40 ms 5908 KB Output is correct
67 Correct 162 ms 22676 KB Output is correct
68 Correct 160 ms 22096 KB Output is correct
69 Correct 169 ms 25172 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Incorrect 0 ms 348 KB Too few ways to get from 4 to 5, should be 1 found 0
72 Halted 0 ms 0 KB -