Submission #1077890

# Submission time Handle Problem Language Result Execution time Memory
1077890 2024-08-27T10:08:55 Z anango Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
173 ms 24400 KB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;

class DSU {
private: int n; vector<int> parent,rank;
public:
    DSU(int sz) {
        n=sz; parent=vector<int>(n); iota(parent.begin(), parent.end(), (int)0); rank=vector<int>(n,0);
    }
    int find(int node) {
        return (node==parent[node])?(node):(parent[node] = find(parent[node]));
    }
    void link(int u, int v){ 
        u=find(u); v=find(v); if (rank[u]<rank[v]) swap(u,v);
        rank[u]+=rank[u]==rank[v]; parent[v] = u;
    }
    vector<int> getpars() {
        for (int i=0; i<n; i++) find(i);
        return parent;
    }
};

int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
	DSU dsu(n);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            if (p[i][j]!=0) {
                dsu.link(i,j);
            }
        }
    }
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            if (p[i][j]==3) {
                return 0;
            }
            if (dsu.find(i)==dsu.find(j) && p[i][j]==0) {
                return 0;
            } 
        }
    }
    vector<int> par = dsu.getpars();
    map<int,vector<int>> compar;
    for (int i=0; i<n; i++) {
        compar[par[i]].push_back(i);
    }
    vector<vector<int>> components;
    for (auto i:compar) {
        components.push_back(i.second);
    }
    vector<vector<int>> ans(n,vector<int>(n,0));
    for (auto component:components) {
        sort(component.begin(), component.end());
        int k = component.size();
        //if p[i][j] = 1, they are on the same vertex of the cycle
        //otherwise we have no additional info of which vertices are on the cycle, just choose it arbitrarily
        DSU dsu2(k);
        map<int,int> rev; for (int i=0; i<k; i++) rev[component[i]] = i;
        for (int i:component) {
            for (int j:component) {
                if (p[i][j]==1) {
                    dsu2.link(rev[i],rev[j]);
                }
            }
        }
        map<int,vector<int>> cycloc;
        vector<int> par2 = dsu2.getpars();
        for (int i=0; i<k; i++) {
            cycloc[par2[i]].push_back(i);
        }
        vector<vector<int>> cyclecom;
        for (auto i:cycloc) {
            cyclecom.push_back(i.second);
        }
        if (cyclecom.size()>1) {
            for (int i=0; i<cyclecom.size(); i++) {
                int rep = cyclecom[i][0];
                int rep1 = cyclecom[(i+1)%cyclecom.size()][0];
                ans[component[rep]][component[rep1]] = ans[component[rep1]][component[rep]] = 1;
            }
        }
        for (auto i:cyclecom) {
            for (int j=1; j<i.size(); j++) {
                int i1 = i[j-1];
                int i2 = i[j];
                ans[component[i1]][component[i2]] = ans[component[i2]][component[i1]] = 1;
            }
        }
        for (int i=0; i<k; i++) {
            for (int j=0; j<k; j++) {
                if (par2[i]==par2[j]) {
                    if (p[component[i]][component[j]]==2) {
                        return 0;
                    }
                }
                else {
                    if (p[component[i]][component[j]]==1) {
                        return 0;
                    }
                }
            }
        }
        if (cyclecom.size()==2) {
            return 0;
        }
    }
    build(ans);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:79:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |             for (int i=0; i<cyclecom.size(); i++) {
      |                           ~^~~~~~~~~~~~~~~~
supertrees.cpp:86:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |             for (int j=1; j<i.size(); j++) {
      |                           ~^~~~~~~~~
# 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 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 7 ms 1268 KB Output is correct
7 Correct 173 ms 24144 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 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 7 ms 1268 KB Output is correct
7 Correct 173 ms 24144 KB Output is correct
8 Correct 1 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 5 ms 1372 KB Output is correct
13 Correct 119 ms 24196 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 640 KB Output is correct
17 Correct 59 ms 10064 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 38 ms 6224 KB Output is correct
21 Correct 128 ms 23996 KB Output is correct
22 Correct 120 ms 24144 KB Output is correct
23 Correct 159 ms 24148 KB Output is correct
24 Correct 117 ms 24148 KB Output is correct
25 Correct 52 ms 10236 KB Output is correct
26 Correct 52 ms 10112 KB Output is correct
27 Correct 151 ms 23980 KB Output is correct
28 Correct 112 ms 24116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 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 6 ms 1448 KB Output is correct
9 Correct 112 ms 24148 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 5 ms 1372 KB Output is correct
13 Correct 120 ms 24068 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 61 ms 10236 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 31 ms 6224 KB Output is correct
22 Correct 118 ms 23964 KB Output is correct
23 Correct 115 ms 24148 KB Output is correct
24 Correct 138 ms 24400 KB Output is correct
25 Correct 52 ms 14216 KB Output is correct
26 Correct 49 ms 10076 KB Output is correct
27 Correct 116 ms 23972 KB Output is correct
28 Correct 129 ms 24056 KB Output is correct
29 Correct 52 ms 14212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 32 ms 6276 KB Output is correct
5 Correct 129 ms 24148 KB Output is correct
6 Correct 116 ms 24144 KB Output is correct
7 Correct 156 ms 24144 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 30 ms 6368 KB Output is correct
10 Correct 127 ms 24048 KB Output is correct
11 Correct 121 ms 24352 KB Output is correct
12 Correct 128 ms 24056 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 432 KB Output is correct
16 Correct 31 ms 6276 KB Output is correct
17 Correct 148 ms 24204 KB Output is correct
18 Correct 122 ms 24076 KB Output is correct
19 Correct 125 ms 24116 KB Output is correct
20 Correct 116 ms 24148 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 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 7 ms 1268 KB Output is correct
7 Correct 173 ms 24144 KB Output is correct
8 Correct 1 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 5 ms 1372 KB Output is correct
13 Correct 119 ms 24196 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 640 KB Output is correct
17 Correct 59 ms 10064 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 38 ms 6224 KB Output is correct
21 Correct 128 ms 23996 KB Output is correct
22 Correct 120 ms 24144 KB Output is correct
23 Correct 159 ms 24148 KB Output is correct
24 Correct 117 ms 24148 KB Output is correct
25 Correct 52 ms 10236 KB Output is correct
26 Correct 52 ms 10112 KB Output is correct
27 Correct 151 ms 23980 KB Output is correct
28 Correct 112 ms 24116 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 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 6 ms 1448 KB Output is correct
37 Correct 112 ms 24148 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 5 ms 1372 KB Output is correct
41 Correct 120 ms 24068 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 61 ms 10236 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 31 ms 6224 KB Output is correct
50 Correct 118 ms 23964 KB Output is correct
51 Correct 115 ms 24148 KB Output is correct
52 Correct 138 ms 24400 KB Output is correct
53 Correct 52 ms 14216 KB Output is correct
54 Correct 49 ms 10076 KB Output is correct
55 Correct 116 ms 23972 KB Output is correct
56 Correct 129 ms 24056 KB Output is correct
57 Correct 52 ms 14212 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 3 ms 856 KB Output is correct
61 Correct 67 ms 10232 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 29 ms 6276 KB Output is correct
66 Correct 56 ms 10064 KB Output is correct
67 Correct 50 ms 10072 KB Output is correct
68 Correct 51 ms 10116 KB Output is correct
69 Correct 55 ms 14372 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 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 7 ms 1268 KB Output is correct
7 Correct 173 ms 24144 KB Output is correct
8 Correct 1 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 5 ms 1372 KB Output is correct
13 Correct 119 ms 24196 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 640 KB Output is correct
17 Correct 59 ms 10064 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 38 ms 6224 KB Output is correct
21 Correct 128 ms 23996 KB Output is correct
22 Correct 120 ms 24144 KB Output is correct
23 Correct 159 ms 24148 KB Output is correct
24 Correct 117 ms 24148 KB Output is correct
25 Correct 52 ms 10236 KB Output is correct
26 Correct 52 ms 10112 KB Output is correct
27 Correct 151 ms 23980 KB Output is correct
28 Correct 112 ms 24116 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 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 6 ms 1448 KB Output is correct
37 Correct 112 ms 24148 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 5 ms 1372 KB Output is correct
41 Correct 120 ms 24068 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 61 ms 10236 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 31 ms 6224 KB Output is correct
50 Correct 118 ms 23964 KB Output is correct
51 Correct 115 ms 24148 KB Output is correct
52 Correct 138 ms 24400 KB Output is correct
53 Correct 52 ms 14216 KB Output is correct
54 Correct 49 ms 10076 KB Output is correct
55 Correct 116 ms 23972 KB Output is correct
56 Correct 129 ms 24056 KB Output is correct
57 Correct 52 ms 14212 KB Output is correct
58 Correct 1 ms 344 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 32 ms 6276 KB Output is correct
62 Correct 129 ms 24148 KB Output is correct
63 Correct 116 ms 24144 KB Output is correct
64 Correct 156 ms 24144 KB Output is correct
65 Correct 0 ms 348 KB Output is correct
66 Correct 30 ms 6368 KB Output is correct
67 Correct 127 ms 24048 KB Output is correct
68 Correct 121 ms 24352 KB Output is correct
69 Correct 128 ms 24056 KB Output is correct
70 Correct 0 ms 344 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 432 KB Output is correct
73 Correct 31 ms 6276 KB Output is correct
74 Correct 148 ms 24204 KB Output is correct
75 Correct 122 ms 24076 KB Output is correct
76 Correct 125 ms 24116 KB Output is correct
77 Correct 116 ms 24148 KB Output is correct
78 Correct 1 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 3 ms 856 KB Output is correct
81 Correct 67 ms 10232 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 29 ms 6276 KB Output is correct
86 Correct 56 ms 10064 KB Output is correct
87 Correct 50 ms 10072 KB Output is correct
88 Correct 51 ms 10116 KB Output is correct
89 Correct 55 ms 14372 KB Output is correct
90 Correct 0 ms 348 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 3 ms 604 KB Output is correct
93 Correct 55 ms 10232 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 0 ms 348 KB Output is correct
96 Correct 0 ms 348 KB Output is correct
97 Correct 13 ms 2908 KB Output is correct
98 Correct 50 ms 10068 KB Output is correct
99 Correct 52 ms 10064 KB Output is correct
100 Correct 50 ms 10232 KB Output is correct
101 Correct 62 ms 10068 KB Output is correct
102 Correct 50 ms 10064 KB Output is correct
103 Correct 51 ms 10076 KB Output is correct
104 Correct 50 ms 10064 KB Output is correct
105 Correct 69 ms 10068 KB Output is correct