Submission #359294

# Submission time Handle Problem Language Result Execution time Memory
359294 2021-01-26T16:14:45 Z ljuba Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
285 ms 28288 KB
#include <bits/stdc++.h>
 
using namespace std;

typedef long long ll;
typedef long double ld;

typedef vector<int> vi;
typedef vector<ll> vll;

typedef vector<vi> vvi;
typedef vector<vll> vvll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<pii> vpii;
typedef vector<pll> vpll;

typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define fi first
#define se second

template<class T> bool ckmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<class T> bool ckmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for(auto z : x) cerr << (f++ ? "," : ""), __print(z); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if(sizeof...(v)) cerr << ", "; _print(v...);}
 
#ifdef ljuba
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif

const char nl = '\n';

#include "supertrees.h"

const int mxN = 1005;
bool vis[mxN];
int n;
vi cur;
vvi p;
vvi ans;

struct DSU {
    int rod[mxN], vel[mxN];
    
    DSU() {
        for(int i = 0; i < mxN; ++i) {
            rod[i] = i;
            vel[i] = 1;
        }
    }

    int find_set(int a) {
        if(a == rod[a])
            return a;
        return rod[a] = find_set(rod[rod[a]]);
    }

    bool unite(int a, int b) {
        a = find_set(a);
        b = find_set(b);

        if(a == b)
            return 0;
        if(vel[a] > vel[b])
            swap(a, b);
        vel[b] += vel[a];
        rod[a] = b;
        return 1;
    }
}dsu;

void dfs(int s) {
    cur.pb(s);
    dbg(s);
    vis[s] = 1;
    for(int i = 0; i < n; ++i) {
        if(!vis[i] && p[s][i])
            dfs(i);
    }
}

bool idi(int s) {
    cur.clear();
    dfs(s);
    for(auto z : cur) {
        for(auto z2 : cur) {
            if(!p[z][z2])
                return 0;
        }
    }
    //resenje bi trebalo da izgleda kao da dole imas lance, a iznad lanaca jos dva cvora tako da su oba povezana sa pocetkom svih lanaca

    vi lanac;

    dbg(cur);

    for(auto z : cur) {
        if(dsu.find_set(z) == z) {
            lanac.pb(z);
            vi comp;
            for(int i = 0; i < n; ++i) {
                if(dsu.find_set(i) == z) {
                    comp.pb(i);
                }
            }
            dbg(comp);
            for(int i = 0; i + 1 < sz(comp); ++i) {
                ans[comp[i]][comp[i+1]] = ans[comp[i+1]][comp[i]] = 1;
            }
            for(auto z : comp) {
                for(auto z2 : comp)
                    if(p[z][z2] != 1)
                        return 0;
            }
        }
    }
    
    dbg(lanac);

    if(sz(lanac) == 2)
        return 0;
    if(sz(lanac) == 1)
        return 1;
    for(int i = 0; i < sz(lanac); ++i) {
        ans[lanac[i]][lanac[(i+1)%sz(lanac)]] = ans[lanac[(i+1)%sz(lanac)]][lanac[i]] = 1;
    }

    return 1;
}

int construct(vvi p2) {
    p = p2;
    n = sz(p);
    ans = vvi(n, vi(n, 0));

    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            if(p[i][j] == 3)
                return 0;
        }
    }

    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            if(p[i][j] == 1)
                dsu.unite(i, j);
        }
    }

    for(int i = 0; i < n; ++i) {
        if(!vis[i]) {
            if(!idi(i))
                return 0;
        }
    }

    dbg(ans);
    build(ans);
    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1664 KB Output is correct
7 Correct 257 ms 28012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1664 KB Output is correct
7 Correct 257 ms 28012 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1516 KB Output is correct
13 Correct 255 ms 28140 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 1004 KB Output is correct
17 Correct 119 ms 18156 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 64 ms 7276 KB Output is correct
21 Correct 256 ms 28012 KB Output is correct
22 Correct 253 ms 28140 KB Output is correct
23 Correct 269 ms 28268 KB Output is correct
24 Correct 257 ms 28012 KB Output is correct
25 Correct 113 ms 18028 KB Output is correct
26 Correct 106 ms 18028 KB Output is correct
27 Correct 266 ms 28012 KB Output is correct
28 Correct 260 ms 28012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 12 ms 1516 KB Output is correct
9 Correct 256 ms 28288 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1516 KB Output is correct
13 Correct 253 ms 28012 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 1004 KB Output is correct
17 Correct 112 ms 18156 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 66 ms 7276 KB Output is correct
22 Correct 259 ms 27968 KB Output is correct
23 Correct 285 ms 28140 KB Output is correct
24 Correct 265 ms 28012 KB Output is correct
25 Correct 108 ms 18156 KB Output is correct
26 Correct 109 ms 18028 KB Output is correct
27 Correct 254 ms 28012 KB Output is correct
28 Correct 265 ms 28040 KB Output is correct
29 Correct 107 ms 18156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 79 ms 7276 KB Output is correct
5 Correct 270 ms 28140 KB Output is correct
6 Correct 261 ms 28040 KB Output is correct
7 Correct 270 ms 28012 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 65 ms 7276 KB Output is correct
10 Correct 273 ms 28120 KB Output is correct
11 Correct 280 ms 28012 KB Output is correct
12 Correct 274 ms 28140 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 66 ms 7368 KB Output is correct
17 Correct 262 ms 28140 KB Output is correct
18 Correct 262 ms 28012 KB Output is correct
19 Correct 260 ms 28012 KB Output is correct
20 Correct 272 ms 28092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1664 KB Output is correct
7 Correct 257 ms 28012 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1516 KB Output is correct
13 Correct 255 ms 28140 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 1004 KB Output is correct
17 Correct 119 ms 18156 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 64 ms 7276 KB Output is correct
21 Correct 256 ms 28012 KB Output is correct
22 Correct 253 ms 28140 KB Output is correct
23 Correct 269 ms 28268 KB Output is correct
24 Correct 257 ms 28012 KB Output is correct
25 Correct 113 ms 18028 KB Output is correct
26 Correct 106 ms 18028 KB Output is correct
27 Correct 266 ms 28012 KB Output is correct
28 Correct 260 ms 28012 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 12 ms 1516 KB Output is correct
37 Correct 256 ms 28288 KB Output is correct
38 Correct 1 ms 492 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 11 ms 1516 KB Output is correct
41 Correct 253 ms 28012 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 5 ms 1004 KB Output is correct
45 Correct 112 ms 18156 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 66 ms 7276 KB Output is correct
50 Correct 259 ms 27968 KB Output is correct
51 Correct 285 ms 28140 KB Output is correct
52 Correct 265 ms 28012 KB Output is correct
53 Correct 108 ms 18156 KB Output is correct
54 Correct 109 ms 18028 KB Output is correct
55 Correct 254 ms 28012 KB Output is correct
56 Correct 265 ms 28040 KB Output is correct
57 Correct 107 ms 18156 KB Output is correct
58 Correct 0 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 5 ms 1004 KB Output is correct
61 Correct 120 ms 18156 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 364 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 64 ms 7276 KB Output is correct
66 Correct 115 ms 18160 KB Output is correct
67 Correct 109 ms 18028 KB Output is correct
68 Correct 104 ms 18156 KB Output is correct
69 Correct 105 ms 18028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1664 KB Output is correct
7 Correct 257 ms 28012 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1516 KB Output is correct
13 Correct 255 ms 28140 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 1004 KB Output is correct
17 Correct 119 ms 18156 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 64 ms 7276 KB Output is correct
21 Correct 256 ms 28012 KB Output is correct
22 Correct 253 ms 28140 KB Output is correct
23 Correct 269 ms 28268 KB Output is correct
24 Correct 257 ms 28012 KB Output is correct
25 Correct 113 ms 18028 KB Output is correct
26 Correct 106 ms 18028 KB Output is correct
27 Correct 266 ms 28012 KB Output is correct
28 Correct 260 ms 28012 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 12 ms 1516 KB Output is correct
37 Correct 256 ms 28288 KB Output is correct
38 Correct 1 ms 492 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 11 ms 1516 KB Output is correct
41 Correct 253 ms 28012 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 5 ms 1004 KB Output is correct
45 Correct 112 ms 18156 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 66 ms 7276 KB Output is correct
50 Correct 259 ms 27968 KB Output is correct
51 Correct 285 ms 28140 KB Output is correct
52 Correct 265 ms 28012 KB Output is correct
53 Correct 108 ms 18156 KB Output is correct
54 Correct 109 ms 18028 KB Output is correct
55 Correct 254 ms 28012 KB Output is correct
56 Correct 265 ms 28040 KB Output is correct
57 Correct 107 ms 18156 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 79 ms 7276 KB Output is correct
62 Correct 270 ms 28140 KB Output is correct
63 Correct 261 ms 28040 KB Output is correct
64 Correct 270 ms 28012 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 65 ms 7276 KB Output is correct
67 Correct 273 ms 28120 KB Output is correct
68 Correct 280 ms 28012 KB Output is correct
69 Correct 274 ms 28140 KB Output is correct
70 Correct 1 ms 364 KB Output is correct
71 Correct 1 ms 364 KB Output is correct
72 Correct 1 ms 364 KB Output is correct
73 Correct 66 ms 7368 KB Output is correct
74 Correct 262 ms 28140 KB Output is correct
75 Correct 262 ms 28012 KB Output is correct
76 Correct 260 ms 28012 KB Output is correct
77 Correct 272 ms 28092 KB Output is correct
78 Correct 0 ms 364 KB Output is correct
79 Correct 1 ms 364 KB Output is correct
80 Correct 5 ms 1004 KB Output is correct
81 Correct 120 ms 18156 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct
84 Correct 1 ms 364 KB Output is correct
85 Correct 64 ms 7276 KB Output is correct
86 Correct 115 ms 18160 KB Output is correct
87 Correct 109 ms 18028 KB Output is correct
88 Correct 104 ms 18156 KB Output is correct
89 Correct 105 ms 18028 KB Output is correct
90 Correct 1 ms 364 KB Output is correct
91 Correct 1 ms 364 KB Output is correct
92 Correct 5 ms 1004 KB Output is correct
93 Correct 111 ms 18156 KB Output is correct
94 Correct 1 ms 492 KB Output is correct
95 Correct 1 ms 364 KB Output is correct
96 Correct 1 ms 364 KB Output is correct
97 Correct 26 ms 4844 KB Output is correct
98 Correct 103 ms 18028 KB Output is correct
99 Correct 117 ms 18028 KB Output is correct
100 Correct 102 ms 18028 KB Output is correct
101 Correct 102 ms 18028 KB Output is correct
102 Correct 103 ms 18028 KB Output is correct
103 Correct 130 ms 18156 KB Output is correct
104 Correct 107 ms 18028 KB Output is correct
105 Correct 125 ms 18156 KB Output is correct