Submission #985777

# Submission time Handle Problem Language Result Execution time Memory
985777 2024-05-18T19:13:58 Z Zbyszek99 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
176 ms 30292 KB
#include "supertrees.h"
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;

vector<vector<int>> ans;
int n;
vector<int> graf1[1000];
bitset<1000> odw;
vector<vector<int>> p;
int tree[1000];
int pop_tree[1000];
int rep_t[1000];
int rep[1000];
int typ_pol[1000];
vector<int> cycle[1000];
vector<int> v_on_tree[1000];
int akttree = 0;

int find_(int v)
{
	if(rep[v] == v) return v;
	rep[v] = find_(rep[v]); return rep[v];
}

int union_(int a, int b, int a2, int b2)
{
	rep[find_(b)] = find_(a);
	if(typ_pol[find_(b)] == -1 || typ_pol[find_(a)] == -1)
	{
		typ_pol[find_(b)] = max(typ_pol[find_(b)],typ_pol[find_(a)]);
		typ_pol[find_(a)] = max(typ_pol[find_(b)],typ_pol[find_(a)]);
	}
	if(typ_pol[find_(b)] == -1)
	{
		typ_pol[find_(b)] = p[a2][b2];
		typ_pol[find_(a)] = p[a2][b2];
	}
	//cout << typ_pol[find_(a)] << " " << typ_pol[find_(b)] << " " << a2 << " " << b2 <<  " typy\n"; 
	if(typ_pol[find_(a)] != typ_pol[find_(b)] || p[a2][b2] != typ_pol[find_(a)])
	{
		return -1;
	}
	return 0;
}

void dfs(int v)
{
	odw[v] = 1;
 	tree[v] = akttree;
	v_on_tree[akttree].push_back(v);
	rep_t[akttree] = v;
	for(auto& it: graf1[v]) if(odw[it] == 0) dfs(it); 
}

int construct(vector<vector<int>> p2) {
	p = p2;
	n = (int)p.size();
	ans.resize(n);
	rep(i,n)
	{
		typ_pol[i] = -1;
		pop_tree[i] = -1;
		rep(j,n)
		ans[i].push_back(0);
		rep[i] = i;
	}
	rep(i,n) rep(j,n)
	{
		if(p[i][j] == 3) return 0;
		if(p[i][j] == 1 && i != j) graf1[i].push_back(j); 
	}
	rep(i,n)
	{
		if(odw[i] == 0) 
		{
			dfs(i);
			akttree++;
		}
	}
	rep(i,n) rep(j,n)
	{
		if(tree[i] == tree[j] && p[i][j] != 1) 
		{
			//cout << " zle tree\n";
			return 0;
		}
	}
	rep(i,n) rep(j,n)
	{
		if(p[i][j] >= 2 && tree[i] != tree[j]) 
		{
		//	cout << i << " " << j << " " << tree[i] << " " << tree[j] << " union\n";
			if(union_(tree[i],tree[j],i,j) == -1)
			{
				//cout << tree[i] << " " << tree[j] << " " << i << " " << j << " zle union\n"; 
				return 0;
			}
		}
	}
	rep(i,n)
	{
		if(pop_tree[tree[i]] != -1)
		{
			ans[i][pop_tree[tree[i]]] = 1;
			ans[pop_tree[tree[i]]][i] = 1;
		}
		pop_tree[tree[i]] = i;
	}
	rep(i,akttree)
	{
		rep(j,akttree)
		{
			if(find_(i) == find_(j) && i < j)
			{
				for(auto& it1: v_on_tree[i])
				{
					for(auto& it2: v_on_tree[j])
					{
						if(p[it1][it2] == 0 || p[it1][it2] == 1) return 0;
					}
				}
			}
		}
	}
	rep(i,akttree)
	{
		//cout << find_(i) << " " << rep_t[i]<< " find\n";
		cycle[find_(i)].push_back(rep_t[i]);
	}
	rep(i,n)
	{
		int ile = (int)cycle[i].size();
		
		if(ile == 0) continue;
		if(ile == 1) continue;
		//cout << ile << " " << i << " cycle\n";
		int typ = p[cycle[i][0]][cycle[i][1]];
		if(typ == 0)
		{
			//cout <<  " typ 0\n";
			return 0;
		}
		if(typ == 2)
		{
			if(ile < 3) 
			{
				//cout << " ile < 3\n";
				return 0;
			}
			for(int j = 0; j < ile-1; j++)
			{
				ans[cycle[i][j]][cycle[i][j+1]] = 1;
				ans[cycle[i][j+1]][cycle[i][j]] = 1;
			}
			ans[cycle[i][0]][cycle[i][ile-1]] = 1;
			ans[cycle[i][ile-1]][cycle[i][0]] = 1;
		}	
	}
	build(ans);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 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 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 7 ms 1628 KB Output is correct
7 Correct 159 ms 30292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 7 ms 1628 KB Output is correct
7 Correct 159 ms 30292 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 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 7 ms 1628 KB Output is correct
13 Correct 158 ms 26204 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 1116 KB Output is correct
17 Correct 77 ms 18776 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 42 ms 6972 KB Output is correct
21 Correct 165 ms 26728 KB Output is correct
22 Correct 161 ms 26196 KB Output is correct
23 Correct 162 ms 28240 KB Output is correct
24 Correct 159 ms 26548 KB Output is correct
25 Correct 65 ms 16724 KB Output is correct
26 Correct 64 ms 16212 KB Output is correct
27 Correct 164 ms 29440 KB Output is correct
28 Correct 159 ms 26212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 600 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 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 7 ms 1628 KB Output is correct
9 Correct 157 ms 26300 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 8 ms 1636 KB Output is correct
13 Correct 174 ms 26304 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 1116 KB Output is correct
17 Correct 93 ms 17720 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 512 KB Output is correct
21 Correct 44 ms 7512 KB Output is correct
22 Correct 166 ms 27472 KB Output is correct
23 Correct 162 ms 27472 KB Output is correct
24 Correct 176 ms 27476 KB Output is correct
25 Correct 74 ms 17748 KB Output is correct
26 Correct 71 ms 17604 KB Output is correct
27 Correct 161 ms 27412 KB Output is correct
28 Correct 176 ms 27476 KB Output is correct
29 Correct 87 ms 17748 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 39 ms 6992 KB Output is correct
5 Correct 154 ms 26708 KB Output is correct
6 Correct 160 ms 26196 KB Output is correct
7 Correct 165 ms 28240 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 40 ms 6980 KB Output is correct
10 Correct 163 ms 26228 KB Output is correct
11 Correct 159 ms 26192 KB Output is correct
12 Correct 174 ms 26284 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 40 ms 6996 KB Output is correct
17 Correct 162 ms 26196 KB Output is correct
18 Correct 164 ms 26708 KB Output is correct
19 Correct 159 ms 26200 KB Output is correct
20 Correct 159 ms 26460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 7 ms 1628 KB Output is correct
7 Correct 159 ms 30292 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 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 7 ms 1628 KB Output is correct
13 Correct 158 ms 26204 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 1116 KB Output is correct
17 Correct 77 ms 18776 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 42 ms 6972 KB Output is correct
21 Correct 165 ms 26728 KB Output is correct
22 Correct 161 ms 26196 KB Output is correct
23 Correct 162 ms 28240 KB Output is correct
24 Correct 159 ms 26548 KB Output is correct
25 Correct 65 ms 16724 KB Output is correct
26 Correct 64 ms 16212 KB Output is correct
27 Correct 164 ms 29440 KB Output is correct
28 Correct 159 ms 26212 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 1 ms 600 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 344 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 1628 KB Output is correct
37 Correct 157 ms 26300 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 8 ms 1636 KB Output is correct
41 Correct 174 ms 26304 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 1116 KB Output is correct
45 Correct 93 ms 17720 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 512 KB Output is correct
49 Correct 44 ms 7512 KB Output is correct
50 Correct 166 ms 27472 KB Output is correct
51 Correct 162 ms 27472 KB Output is correct
52 Correct 176 ms 27476 KB Output is correct
53 Correct 74 ms 17748 KB Output is correct
54 Correct 71 ms 17604 KB Output is correct
55 Correct 161 ms 27412 KB Output is correct
56 Correct 176 ms 27476 KB Output is correct
57 Correct 87 ms 17748 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 1 ms 344 KB Output is correct
60 Correct 3 ms 1368 KB Output is correct
61 Correct 76 ms 19588 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 46 ms 7324 KB Output is correct
66 Correct 68 ms 17684 KB Output is correct
67 Correct 65 ms 17604 KB Output is correct
68 Correct 66 ms 17596 KB Output is correct
69 Correct 73 ms 17684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 7 ms 1628 KB Output is correct
7 Correct 159 ms 30292 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 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 7 ms 1628 KB Output is correct
13 Correct 158 ms 26204 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 1116 KB Output is correct
17 Correct 77 ms 18776 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 42 ms 6972 KB Output is correct
21 Correct 165 ms 26728 KB Output is correct
22 Correct 161 ms 26196 KB Output is correct
23 Correct 162 ms 28240 KB Output is correct
24 Correct 159 ms 26548 KB Output is correct
25 Correct 65 ms 16724 KB Output is correct
26 Correct 64 ms 16212 KB Output is correct
27 Correct 164 ms 29440 KB Output is correct
28 Correct 159 ms 26212 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 1 ms 600 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 344 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 1628 KB Output is correct
37 Correct 157 ms 26300 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 8 ms 1636 KB Output is correct
41 Correct 174 ms 26304 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 1116 KB Output is correct
45 Correct 93 ms 17720 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 512 KB Output is correct
49 Correct 44 ms 7512 KB Output is correct
50 Correct 166 ms 27472 KB Output is correct
51 Correct 162 ms 27472 KB Output is correct
52 Correct 176 ms 27476 KB Output is correct
53 Correct 74 ms 17748 KB Output is correct
54 Correct 71 ms 17604 KB Output is correct
55 Correct 161 ms 27412 KB Output is correct
56 Correct 176 ms 27476 KB Output is correct
57 Correct 87 ms 17748 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 39 ms 6992 KB Output is correct
62 Correct 154 ms 26708 KB Output is correct
63 Correct 160 ms 26196 KB Output is correct
64 Correct 165 ms 28240 KB Output is correct
65 Correct 0 ms 348 KB Output is correct
66 Correct 40 ms 6980 KB Output is correct
67 Correct 163 ms 26228 KB Output is correct
68 Correct 159 ms 26192 KB Output is correct
69 Correct 174 ms 26284 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 1 ms 348 KB Output is correct
72 Correct 1 ms 344 KB Output is correct
73 Correct 40 ms 6996 KB Output is correct
74 Correct 162 ms 26196 KB Output is correct
75 Correct 164 ms 26708 KB Output is correct
76 Correct 159 ms 26200 KB Output is correct
77 Correct 159 ms 26460 KB Output is correct
78 Correct 1 ms 348 KB Output is correct
79 Correct 1 ms 344 KB Output is correct
80 Correct 3 ms 1368 KB Output is correct
81 Correct 76 ms 19588 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 1 ms 348 KB Output is correct
85 Correct 46 ms 7324 KB Output is correct
86 Correct 68 ms 17684 KB Output is correct
87 Correct 65 ms 17604 KB Output is correct
88 Correct 66 ms 17596 KB Output is correct
89 Correct 73 ms 17684 KB Output is correct
90 Correct 0 ms 600 KB Output is correct
91 Correct 1 ms 600 KB Output is correct
92 Correct 3 ms 1264 KB Output is correct
93 Correct 67 ms 17436 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 16 ms 4816 KB Output is correct
98 Correct 66 ms 17472 KB Output is correct
99 Correct 65 ms 17644 KB Output is correct
100 Correct 64 ms 17588 KB Output is correct
101 Correct 66 ms 17648 KB Output is correct
102 Correct 65 ms 17492 KB Output is correct
103 Correct 71 ms 17744 KB Output is correct
104 Correct 65 ms 17488 KB Output is correct
105 Correct 68 ms 17588 KB Output is correct