Submission #1076816

# Submission time Handle Problem Language Result Execution time Memory
1076816 2024-08-26T16:53:14 Z Hectorungo_18 Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
138 ms 22100 KB
#include "supertrees.h"
#include <vector>
#include<bits/stdc++.h>
 
using namespace std;
 
 
 
const int N = 1e3+7;
int p[N];
int sz[N];
 
 
int pa(int u){
	p[u]=(p[u] == u ? u : pa(p[u]));
	return p[u];
}
 
bool sm(int u, int v){
	return pa(u) == pa(v);
}
 
void un(int u, int v){
	if(sm(u, v)) return;
	u = pa(u);
	v = pa(v);
	if(sz[v] > sz[u]) swap(u, v);
	sz[u]+=sz[v];
	p[v]=u;
}
 
int construct(vector<vector<int>> v) {
	int n = v.size();
	for(int i = 0; i <= n; i++){
		p[i]=i;
		sz[i]=1;
	}
	for(int i = 0; i < n; i++){
        bool ch = 0;
        if(pa(i)==i) ch = 1;
		for(int j = i+1; j < n; j++){
          if(v[i][j] == 3) return 0;
			if(ch){
                if(v[i][j] == 0) continue;
                if(pa(j) == j){
                    un(i, j);
                }
                else return 0;
			}
            else{
                if(v[i][j] == 0){
                  if(pa(i) == pa(j)) return 0;
                }
              else if(pa(i) != pa(j)) return 0;
              	
            }
		}
	}
 
    map<int, int> m;
    for(int i = 0; i < n; i++){
        vector<int> a(4, 0);
        for(int j = 0; j < n; j++){
            if(i == j) continue;
            a[v[i][j]]++;
        }
        if(a[1] > 0) m[i]=1;
        else if(a[2] > 0) m[i]=2;
        else if(a[3] > 0) m[i]=3;
        else m[i]=0;
    }
 
    vector<vector<int>> ans(n, vector<int> (n, 0));
    vector<int> sp(n);
    for(int i = 0; i < n; i++){
        sp[i]=pa(i);
    }
    for(int i = 0; i < n; i++){
        p[i]=i;
        sz[i]=i;
    }
    for(int i = 0; i < n; i++){
        if(sp[i] != i) continue;
        vector<int> w;
        for(int j = 0; j < n; j++){
            if(sp[j] == i) w.push_back(j);
        }
        vector<int> dd;
        for(int j = 0; j < w.size(); j++){
            int r = w[j];
 
            for(int k = 0; k < n; k++){
                if(k == r) continue;
                if(v[r][k] != 1) continue;
                if(pa(r) == pa(k)) continue;
                if(pa(r) == r){
                    un(r, k);
                }
                else{
                    if(pa(k) == k){
                        un(r, k);
                    }
                    else return 0;
                }
            }
        }
        for(int j = 0; j < w.size(); j++){
            if(w[j] == pa(w[j])) dd.push_back(w[j]);
        }
        if(dd.size() == 2) return 0;
        for(int j = 1; j < dd.size(); j++){
            ans[dd[j]][dd[j-1]] = 1;
            ans[dd[j-1]][dd[j]] = 1;
        }
        if(dd.size() > 2){
            ans[dd[0]][dd.back()] = 1;
            ans[dd.back()][dd[0]] = 1;
        }
        vector<vector<int>> adj(n);
        for(int i = 0; i < w.size(); i++){
            int r = w[i];
            adj[pa(r)].push_back(r);
        }
        for(int i = 0; i < n; i++){
            for(int j = 1; j < adj[i].size(); j++){
                ans[adj[i][j]][adj[i][j-1]] = 1;
                ans[adj[i][j-1]][adj[i][j]] = 1;
            }
        }
    }
    
    build(ans);
 
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:89:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |         for(int j = 0; j < w.size(); j++){
      |                        ~~^~~~~~~~~~
supertrees.cpp:107:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |         for(int j = 0; j < w.size(); j++){
      |                        ~~^~~~~~~~~~
supertrees.cpp:111:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |         for(int j = 1; j < dd.size(); j++){
      |                        ~~^~~~~~~~~~~
supertrees.cpp:120:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |         for(int i = 0; i < w.size(); i++){
      |                        ~~^~~~~~~~~~
supertrees.cpp:125:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |             for(int j = 1; j < adj[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 1 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 5 ms 1240 KB Output is correct
7 Correct 117 ms 22096 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 1 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 5 ms 1240 KB Output is correct
7 Correct 117 ms 22096 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 5 ms 1116 KB Output is correct
13 Correct 119 ms 22100 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 57 ms 8200 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 32 ms 5760 KB Output is correct
21 Correct 124 ms 22100 KB Output is correct
22 Correct 119 ms 22100 KB Output is correct
23 Correct 138 ms 22024 KB Output is correct
24 Correct 118 ms 22100 KB Output is correct
25 Correct 51 ms 8272 KB Output is correct
26 Correct 59 ms 8200 KB Output is correct
27 Correct 127 ms 22032 KB Output is correct
28 Correct 133 ms 22096 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 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 5 ms 1200 KB Output is correct
9 Correct 120 ms 22100 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 1116 KB Output is correct
13 Correct 124 ms 22100 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 52 ms 8220 KB Output is correct
18 Correct 0 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 5712 KB Output is correct
22 Correct 117 ms 22096 KB Output is correct
23 Correct 118 ms 22100 KB Output is correct
24 Correct 124 ms 22044 KB Output is correct
25 Correct 54 ms 12316 KB Output is correct
26 Correct 48 ms 8276 KB Output is correct
27 Correct 119 ms 22100 KB Output is correct
28 Correct 122 ms 22096 KB Output is correct
29 Correct 59 ms 12316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 30 ms 5788 KB Output is correct
5 Correct 120 ms 22040 KB Output is correct
6 Correct 116 ms 22100 KB Output is correct
7 Correct 123 ms 22096 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 29 ms 5720 KB Output is correct
10 Correct 120 ms 22100 KB Output is correct
11 Correct 115 ms 22028 KB Output is correct
12 Correct 123 ms 22096 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 28 ms 5720 KB Output is correct
17 Correct 114 ms 22024 KB Output is correct
18 Correct 117 ms 22096 KB Output is correct
19 Correct 114 ms 22100 KB Output is correct
20 Correct 117 ms 22096 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 1 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 5 ms 1240 KB Output is correct
7 Correct 117 ms 22096 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 5 ms 1116 KB Output is correct
13 Correct 119 ms 22100 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 57 ms 8200 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 32 ms 5760 KB Output is correct
21 Correct 124 ms 22100 KB Output is correct
22 Correct 119 ms 22100 KB Output is correct
23 Correct 138 ms 22024 KB Output is correct
24 Correct 118 ms 22100 KB Output is correct
25 Correct 51 ms 8272 KB Output is correct
26 Correct 59 ms 8200 KB Output is correct
27 Correct 127 ms 22032 KB Output is correct
28 Correct 133 ms 22096 KB Output is correct
29 Correct 1 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 5 ms 1200 KB Output is correct
37 Correct 120 ms 22100 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 1116 KB Output is correct
41 Correct 124 ms 22100 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 52 ms 8220 KB Output is correct
46 Correct 0 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 5712 KB Output is correct
50 Correct 117 ms 22096 KB Output is correct
51 Correct 118 ms 22100 KB Output is correct
52 Correct 124 ms 22044 KB Output is correct
53 Correct 54 ms 12316 KB Output is correct
54 Correct 48 ms 8276 KB Output is correct
55 Correct 119 ms 22100 KB Output is correct
56 Correct 122 ms 22096 KB Output is correct
57 Correct 59 ms 12316 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 2 ms 604 KB Output is correct
61 Correct 50 ms 8216 KB Output is correct
62 Correct 0 ms 344 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 28 ms 5848 KB Output is correct
66 Correct 48 ms 8272 KB Output is correct
67 Correct 48 ms 8276 KB Output is correct
68 Correct 48 ms 8272 KB Output is correct
69 Correct 51 ms 12320 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 1 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 5 ms 1240 KB Output is correct
7 Correct 117 ms 22096 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 5 ms 1116 KB Output is correct
13 Correct 119 ms 22100 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 57 ms 8200 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 32 ms 5760 KB Output is correct
21 Correct 124 ms 22100 KB Output is correct
22 Correct 119 ms 22100 KB Output is correct
23 Correct 138 ms 22024 KB Output is correct
24 Correct 118 ms 22100 KB Output is correct
25 Correct 51 ms 8272 KB Output is correct
26 Correct 59 ms 8200 KB Output is correct
27 Correct 127 ms 22032 KB Output is correct
28 Correct 133 ms 22096 KB Output is correct
29 Correct 1 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 5 ms 1200 KB Output is correct
37 Correct 120 ms 22100 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 1116 KB Output is correct
41 Correct 124 ms 22100 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 52 ms 8220 KB Output is correct
46 Correct 0 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 5712 KB Output is correct
50 Correct 117 ms 22096 KB Output is correct
51 Correct 118 ms 22100 KB Output is correct
52 Correct 124 ms 22044 KB Output is correct
53 Correct 54 ms 12316 KB Output is correct
54 Correct 48 ms 8276 KB Output is correct
55 Correct 119 ms 22100 KB Output is correct
56 Correct 122 ms 22096 KB Output is correct
57 Correct 59 ms 12316 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 30 ms 5788 KB Output is correct
62 Correct 120 ms 22040 KB Output is correct
63 Correct 116 ms 22100 KB Output is correct
64 Correct 123 ms 22096 KB Output is correct
65 Correct 1 ms 344 KB Output is correct
66 Correct 29 ms 5720 KB Output is correct
67 Correct 120 ms 22100 KB Output is correct
68 Correct 115 ms 22028 KB Output is correct
69 Correct 123 ms 22096 KB Output is correct
70 Correct 0 ms 344 KB Output is correct
71 Correct 0 ms 344 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 28 ms 5720 KB Output is correct
74 Correct 114 ms 22024 KB Output is correct
75 Correct 117 ms 22096 KB Output is correct
76 Correct 114 ms 22100 KB Output is correct
77 Correct 117 ms 22096 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 2 ms 604 KB Output is correct
81 Correct 50 ms 8216 KB Output is correct
82 Correct 0 ms 344 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 28 ms 5848 KB Output is correct
86 Correct 48 ms 8272 KB Output is correct
87 Correct 48 ms 8276 KB Output is correct
88 Correct 48 ms 8272 KB Output is correct
89 Correct 51 ms 12320 KB Output is correct
90 Correct 1 ms 348 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 4 ms 600 KB Output is correct
93 Correct 52 ms 8220 KB Output is correct
94 Correct 1 ms 348 KB Output is correct
95 Correct 1 ms 348 KB Output is correct
96 Correct 1 ms 348 KB Output is correct
97 Correct 24 ms 2688 KB Output is correct
98 Correct 48 ms 10124 KB Output is correct
99 Correct 98 ms 10112 KB Output is correct
100 Correct 49 ms 10240 KB Output is correct
101 Correct 49 ms 10068 KB Output is correct
102 Correct 46 ms 10252 KB Output is correct
103 Correct 46 ms 10064 KB Output is correct
104 Correct 46 ms 10068 KB Output is correct
105 Correct 53 ms 10064 KB Output is correct