Submission #1042048

# Submission time Handle Problem Language Result Execution time Memory
1042048 2024-08-02T13:15:33 Z amine_aroua Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
121 ms 27480 KB
#include "supertrees.h"
#include <vector>
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> pp;
vector<vector<int>> adj;
int nn;
struct DSU
{
	vector<int> e;
	DSU(int _n)
	{
		e.assign(_n , -1);
	}	
	
	int get(int u )
	{
		return (e[u] < 0 ? u : e[u] = get(e[u]));
	}
	bool same(int u , int v)
	{
		return get(u) == get(v);
	}
	void unite(int u , int v)
	{
		u = get(u) , v = get(v);
		if(u == v)
			return;
		if(e[u] > e[v])
			swap(u , v);
		e[u]+=e[v];
		e[v] = u;
	}
};
bool check_conn()
{
	DSU dsu(nn);
	bool b = 0;
	for(int i = 0 ; i < nn ;i++)
	{
		for(int j = 0 ; j < nn ; j++)
		{
		    if(pp[i][j] == 3)
		        b = 1;
			if(pp[i][j])
			{
				dsu.unite(i , j);
			}
		}
	}
	for(int i = 0 ; i < nn ; i++)
	{
		for(int j = 0 ; j <  nn; j++)
		{
			if(pp[i][j] == 0 && dsu.same(i , j))
			{
				return 0;
			}
		}
	}
	DSU maker(nn);
	for(int i = 0 ; i < nn ;i++)
	{
		for(int j = 0 ; j < nn ;j++)
		{
			if(pp[i][j] == 1)
			{
				maker.unite(i , j);
			}
		}
	}
	for(int i = 0 ; i < nn ; i++)
	{
		for(int j = 0 ; j < nn ; j++)
		{
			if(pp[i][j] >= 2 && maker.same(i , j))
			{
				return 0;
			}
		}
	}
	vector<vector<int>> comp(nn);
	for(int i = 0 ; i < nn ; i++)
	{
		comp[dsu.get(i)].push_back(i);
	}
	for(auto c : comp)
	{
		vector<int> v;
		map<int , vector<int>> trees;
		for(auto i : c)
		{
			int par = maker.get(i);
			trees[par].push_back(i);
			if(i == par)
			{
				v.push_back(i);
				continue;
			}
			adj[par][i] = 1;
			adj[i][par] = 1;
		}
		set<int> s;
		for(auto &x : trees)
		{
			for(auto &y : trees)
			{
				if(x.first == y.first)
					continue;
				for(auto &z : x.second)
				{
					for(auto &w : y.second)
					{
						s.insert(pp[z][w]);
					}
				}
			}
		}
		if(s.empty())
		{
		    if(b)
		        return 0;
		    else
		        continue;
		}
		if((int)s.size() > 1 || *s.begin() < 2)
		{
			return 0;
		}
		if((int)v.size() == 2)
		{
			return 0;
		}
		for(int i = 0 ; i < (int)v.size() ; i++)
		{
			adj[v[i]][v[(i + 1)%((int)v.size())]] = 1;
			adj[v[(i + 1)%((int)v.size())]][v[i]] = 1;
		}
		if(*s.begin() == 3 && v.size() == 3)
		{
			return 0;
		}		
		if(*s.begin() == 3)
		{adj[v[0]][v[2]] = 1;
		adj[v[2]][v[0]] = 1;}
	}
	build(adj);
	return 1;
}
int construct(std::vector<std::vector<int>> P) {
	
	pp = P;
	nn = (int)pp.size();
	adj.assign(nn , vector<int>(nn));
	return check_conn();
}

Compilation message

supertrees.cpp: In function 'bool check_conn()':
supertrees.cpp:43:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   43 |       if(pp[i][j] == 3)
      |       ^~
supertrees.cpp:45:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   45 |    if(pp[i][j])
      |    ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 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 4 ms 1396 KB Output is correct
7 Correct 107 ms 27220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 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 4 ms 1396 KB Output is correct
7 Correct 107 ms 27220 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 5 ms 1444 KB Output is correct
13 Correct 104 ms 27480 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 2 ms 932 KB Output is correct
17 Correct 56 ms 17488 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 26 ms 7264 KB Output is correct
21 Correct 121 ms 27400 KB Output is correct
22 Correct 106 ms 27216 KB Output is correct
23 Correct 120 ms 27320 KB Output is correct
24 Correct 115 ms 27472 KB Output is correct
25 Correct 57 ms 17348 KB Output is correct
26 Correct 54 ms 17504 KB Output is correct
27 Correct 113 ms 27408 KB Output is correct
28 Correct 103 ms 27224 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 0 ms 344 KB Output is correct
4 Correct 0 ms 436 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 1372 KB Output is correct
9 Correct 119 ms 27332 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 1540 KB Output is correct
13 Correct 107 ms 27368 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 1116 KB Output is correct
17 Correct 62 ms 17488 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 27 ms 7212 KB Output is correct
22 Correct 107 ms 27216 KB Output is correct
23 Correct 121 ms 27320 KB Output is correct
24 Correct 117 ms 27216 KB Output is correct
25 Correct 50 ms 17528 KB Output is correct
26 Correct 52 ms 17260 KB Output is correct
27 Correct 106 ms 27396 KB Output is correct
28 Correct 112 ms 27220 KB Output is correct
29 Correct 46 ms 17492 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 26 ms 7376 KB Output is correct
5 Correct 107 ms 27220 KB Output is correct
6 Correct 107 ms 27400 KB Output is correct
7 Correct 115 ms 27252 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 26 ms 7352 KB Output is correct
10 Correct 107 ms 27476 KB Output is correct
11 Correct 103 ms 27300 KB Output is correct
12 Correct 113 ms 27220 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 28 ms 7420 KB Output is correct
17 Correct 105 ms 27380 KB Output is correct
18 Correct 120 ms 27332 KB Output is correct
19 Correct 110 ms 27216 KB Output is correct
20 Correct 103 ms 27436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 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 4 ms 1396 KB Output is correct
7 Correct 107 ms 27220 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 5 ms 1444 KB Output is correct
13 Correct 104 ms 27480 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 2 ms 932 KB Output is correct
17 Correct 56 ms 17488 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 26 ms 7264 KB Output is correct
21 Correct 121 ms 27400 KB Output is correct
22 Correct 106 ms 27216 KB Output is correct
23 Correct 120 ms 27320 KB Output is correct
24 Correct 115 ms 27472 KB Output is correct
25 Correct 57 ms 17348 KB Output is correct
26 Correct 54 ms 17504 KB Output is correct
27 Correct 113 ms 27408 KB Output is correct
28 Correct 103 ms 27224 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 0 ms 436 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 1372 KB Output is correct
37 Correct 119 ms 27332 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 1540 KB Output is correct
41 Correct 107 ms 27368 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 1116 KB Output is correct
45 Correct 62 ms 17488 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 27 ms 7212 KB Output is correct
50 Correct 107 ms 27216 KB Output is correct
51 Correct 121 ms 27320 KB Output is correct
52 Correct 117 ms 27216 KB Output is correct
53 Correct 50 ms 17528 KB Output is correct
54 Correct 52 ms 17260 KB Output is correct
55 Correct 106 ms 27396 KB Output is correct
56 Correct 112 ms 27220 KB Output is correct
57 Correct 46 ms 17492 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 436 KB Output is correct
60 Correct 3 ms 1116 KB Output is correct
61 Correct 52 ms 17352 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 30 ms 7212 KB Output is correct
66 Correct 47 ms 17500 KB Output is correct
67 Correct 48 ms 17492 KB Output is correct
68 Correct 44 ms 17488 KB Output is correct
69 Correct 47 ms 17488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 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 4 ms 1396 KB Output is correct
7 Correct 107 ms 27220 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 5 ms 1444 KB Output is correct
13 Correct 104 ms 27480 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 2 ms 932 KB Output is correct
17 Correct 56 ms 17488 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 26 ms 7264 KB Output is correct
21 Correct 121 ms 27400 KB Output is correct
22 Correct 106 ms 27216 KB Output is correct
23 Correct 120 ms 27320 KB Output is correct
24 Correct 115 ms 27472 KB Output is correct
25 Correct 57 ms 17348 KB Output is correct
26 Correct 54 ms 17504 KB Output is correct
27 Correct 113 ms 27408 KB Output is correct
28 Correct 103 ms 27224 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 0 ms 436 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 1372 KB Output is correct
37 Correct 119 ms 27332 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 1540 KB Output is correct
41 Correct 107 ms 27368 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 1116 KB Output is correct
45 Correct 62 ms 17488 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 27 ms 7212 KB Output is correct
50 Correct 107 ms 27216 KB Output is correct
51 Correct 121 ms 27320 KB Output is correct
52 Correct 117 ms 27216 KB Output is correct
53 Correct 50 ms 17528 KB Output is correct
54 Correct 52 ms 17260 KB Output is correct
55 Correct 106 ms 27396 KB Output is correct
56 Correct 112 ms 27220 KB Output is correct
57 Correct 46 ms 17492 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 26 ms 7376 KB Output is correct
62 Correct 107 ms 27220 KB Output is correct
63 Correct 107 ms 27400 KB Output is correct
64 Correct 115 ms 27252 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 26 ms 7352 KB Output is correct
67 Correct 107 ms 27476 KB Output is correct
68 Correct 103 ms 27300 KB Output is correct
69 Correct 113 ms 27220 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 28 ms 7420 KB Output is correct
74 Correct 105 ms 27380 KB Output is correct
75 Correct 120 ms 27332 KB Output is correct
76 Correct 110 ms 27216 KB Output is correct
77 Correct 103 ms 27436 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 436 KB Output is correct
80 Correct 3 ms 1116 KB Output is correct
81 Correct 52 ms 17352 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 30 ms 7212 KB Output is correct
86 Correct 47 ms 17500 KB Output is correct
87 Correct 48 ms 17492 KB Output is correct
88 Correct 44 ms 17488 KB Output is correct
89 Correct 47 ms 17488 KB Output is correct
90 Correct 1 ms 344 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 2 ms 1116 KB Output is correct
93 Correct 57 ms 17368 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 12 ms 4700 KB Output is correct
98 Correct 52 ms 17932 KB Output is correct
99 Correct 45 ms 17936 KB Output is correct
100 Correct 45 ms 18000 KB Output is correct
101 Correct 47 ms 17988 KB Output is correct
102 Correct 52 ms 18004 KB Output is correct
103 Correct 67 ms 17968 KB Output is correct
104 Correct 56 ms 17928 KB Output is correct
105 Correct 54 ms 18000 KB Output is correct