Submission #675548

# Submission time Handle Problem Language Result Execution time Memory
675548 2022-12-27T12:31:02 Z speedyArda Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
189 ms 24120 KB
#include "supertrees.h"
//#include <vector>
#define vi vector<int>
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 1005;
int par[MAXN];
int twos[MAXN];
vector<vector<int> > elems(MAXN);

auto cmp = [](int a, int b)
{
	//cout << a << " " << b << "\n";
	if(twos[a] != twos[b])
		return twos[a] > twos[b];
	else
		return a > b;
};
int findset(int a)
{
	if(par[a] == a)
		return a;
	return par[a] = findset(par[a]);
}
int mergeset(int a, int b)
{
	a = findset(a), b = findset(b);
	if(a == b)
		return 0;
	//if(a > b)
		//swap(a,b);
	par[b] = a;
	return a;	
}

int construct(vector<vector<int>> p) {
	int n = p.size();
	vector<vector<int>> answer(n, vector<int>(n));
	for(int i = 0; i < n; i++) {
		par[i] = i;
		twos[i] = 0;
	}
	bool valid = true;
	for(int i = 0; i < n; i++)
	{
		if(findset(i) == i) // New component
		{
			//elems[i].push_back(i);
			for(int a = 0; a < n; a++)
			{

				if(p[i][a] == 0)
					continue;
				//cout << i << " " << a << "\n";
				if(findset(a) == a)
				{
					mergeset(i, a);
					elems[i].push_back(a);
				}
			}
		}
	}

	//bool valid = true;
	for(int i = 0; i < n; i++)
	{
		vi incycle;
		if(elems[i].size() == 0)
			continue;
		set<int, decltype(cmp)> curr(cmp);
		
		for(int a : elems[i])
		{
			for(int l = 0; l < elems[i].size(); l++)
			{
				if(p[a][elems[i][l]] == 2)
					twos[a]++;
			}
			//cout << a << ""
			curr.insert(a);
		}
		int lastelem = -1;

		while(!curr.empty())
		{
			int v = *(curr.begin());
			curr.erase(curr.begin());
			if(incycle.size() > 0)
			{
				lastelem = incycle.back();
				answer[v][lastelem] = answer[lastelem][v] = 1;
				
			}
			incycle.push_back(v);
			vi torem;
			torem.push_back(v);
			for(int a : curr)
			{	
				//cout << v << " " << a << " " << p[a][v] << "\n";
				if(p[v][a] == 1) {
					//cout << v << " " << a << "\n";
					answer[v][a] = answer[a][v] = 1;
					torem.push_back(a);
					//curr.erase(a);
				}
			}
			for(int l = 0; l < torem.size(); l++)
			{
				for(int a = 0; a < torem.size(); a++)
				{
					if(l == a)
						continue;
					if(p[torem[l]][torem[a]] != 1)
						valid = false;
				}
			}
			for(int e : torem) 
				curr.erase(e);
			
		}

		if(incycle.size() > 2) {
			answer[incycle.back()][incycle[0]] = answer[incycle[0]][incycle.back()] = 1;
		} else 
		{
			if(twos[incycle[0]] > 0)
				valid = false;

		}

		for(int l = 0; l < elems[i].size(); l++)
		{
			for(int a = 0; a < elems[i].size(); a++)
			{
				//if(elems[i][l] == elems[i][a])
					//continue;
				if(p[elems[i][l]][elems[i][a]] == 0)
					valid = false;
			}
		}
		for(int l = 0; l < incycle.size(); l++)
		{
			for(int a = 0; a < incycle.size(); a++)
			{
				if(incycle[l] == incycle[a])
					continue;
				if(p[incycle[l]][incycle[a]] != 2)
					valid = false;
			}
		}
		//else if(incycle.size() == 1)
			//valid = false;


	}

	for(int i = 0; i < n; i++)
	{
		for(int a = 0; a < n; a++)
		{
			if((findset(i) == findset(a) && p[i][a] == 0) || (findset(i) != findset(a) && p[i][a] != 0))
				valid = false;
		}
	}
	//if(incycle[i] != )
	if(valid) {
		build(answer);
		return 1;
	}
	else
		return 0;
}


Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:74:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |    for(int l = 0; l < elems[i].size(); l++)
      |                   ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:107:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |    for(int l = 0; l < torem.size(); l++)
      |                   ~~^~~~~~~~~~~~~~
supertrees.cpp:109:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |     for(int a = 0; a < torem.size(); a++)
      |                    ~~^~~~~~~~~~~~~~
supertrees.cpp:131:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |   for(int l = 0; l < elems[i].size(); l++)
      |                  ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:133:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |    for(int a = 0; a < elems[i].size(); a++)
      |                   ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:141:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |   for(int l = 0; l < incycle.size(); l++)
      |                  ~~^~~~~~~~~~~~~~~~
supertrees.cpp:143:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  143 |    for(int a = 0; a < incycle.size(); a++)
      |                   ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1220 KB Output is correct
7 Correct 170 ms 22060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1220 KB Output is correct
7 Correct 170 ms 22060 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1236 KB Output is correct
13 Correct 164 ms 22060 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 83 ms 12204 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 42 ms 5708 KB Output is correct
21 Correct 168 ms 22028 KB Output is correct
22 Correct 163 ms 22048 KB Output is correct
23 Correct 180 ms 21964 KB Output is correct
24 Correct 165 ms 22044 KB Output is correct
25 Correct 74 ms 12076 KB Output is correct
26 Correct 71 ms 12060 KB Output is correct
27 Correct 175 ms 22044 KB Output is correct
28 Correct 166 ms 22064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 7 ms 1236 KB Output is correct
9 Correct 189 ms 24044 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 173 ms 24012 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 824 KB Output is correct
17 Correct 87 ms 14100 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 43 ms 6220 KB Output is correct
22 Correct 169 ms 24120 KB Output is correct
23 Correct 170 ms 24012 KB Output is correct
24 Correct 177 ms 23956 KB Output is correct
25 Correct 69 ms 14108 KB Output is correct
26 Correct 73 ms 14096 KB Output is correct
27 Correct 166 ms 23952 KB Output is correct
28 Correct 181 ms 23960 KB Output is correct
29 Correct 68 ms 14100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 240 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 43 ms 5708 KB Output is correct
5 Correct 175 ms 22080 KB Output is correct
6 Correct 165 ms 22028 KB Output is correct
7 Correct 176 ms 22044 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 43 ms 5792 KB Output is correct
10 Correct 171 ms 22172 KB Output is correct
11 Correct 163 ms 22092 KB Output is correct
12 Correct 179 ms 22092 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 43 ms 5784 KB Output is correct
17 Correct 168 ms 22048 KB Output is correct
18 Correct 184 ms 21996 KB Output is correct
19 Correct 175 ms 22048 KB Output is correct
20 Correct 161 ms 22048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1220 KB Output is correct
7 Correct 170 ms 22060 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1236 KB Output is correct
13 Correct 164 ms 22060 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 83 ms 12204 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 42 ms 5708 KB Output is correct
21 Correct 168 ms 22028 KB Output is correct
22 Correct 163 ms 22048 KB Output is correct
23 Correct 180 ms 21964 KB Output is correct
24 Correct 165 ms 22044 KB Output is correct
25 Correct 74 ms 12076 KB Output is correct
26 Correct 71 ms 12060 KB Output is correct
27 Correct 175 ms 22044 KB Output is correct
28 Correct 166 ms 22064 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 328 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1236 KB Output is correct
37 Correct 189 ms 24044 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 8 ms 1236 KB Output is correct
41 Correct 173 ms 24012 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 4 ms 824 KB Output is correct
45 Correct 87 ms 14100 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 43 ms 6220 KB Output is correct
50 Correct 169 ms 24120 KB Output is correct
51 Correct 170 ms 24012 KB Output is correct
52 Correct 177 ms 23956 KB Output is correct
53 Correct 69 ms 14108 KB Output is correct
54 Correct 73 ms 14096 KB Output is correct
55 Correct 166 ms 23952 KB Output is correct
56 Correct 181 ms 23960 KB Output is correct
57 Correct 68 ms 14100 KB Output is correct
58 Correct 1 ms 328 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 4 ms 852 KB Output is correct
61 Correct 91 ms 14096 KB Output is correct
62 Correct 0 ms 328 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Correct 43 ms 6260 KB Output is correct
66 Correct 76 ms 14108 KB Output is correct
67 Correct 76 ms 14108 KB Output is correct
68 Correct 73 ms 14028 KB Output is correct
69 Correct 75 ms 14156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1220 KB Output is correct
7 Correct 170 ms 22060 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1236 KB Output is correct
13 Correct 164 ms 22060 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 83 ms 12204 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 42 ms 5708 KB Output is correct
21 Correct 168 ms 22028 KB Output is correct
22 Correct 163 ms 22048 KB Output is correct
23 Correct 180 ms 21964 KB Output is correct
24 Correct 165 ms 22044 KB Output is correct
25 Correct 74 ms 12076 KB Output is correct
26 Correct 71 ms 12060 KB Output is correct
27 Correct 175 ms 22044 KB Output is correct
28 Correct 166 ms 22064 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 328 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1236 KB Output is correct
37 Correct 189 ms 24044 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 8 ms 1236 KB Output is correct
41 Correct 173 ms 24012 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 4 ms 824 KB Output is correct
45 Correct 87 ms 14100 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 43 ms 6220 KB Output is correct
50 Correct 169 ms 24120 KB Output is correct
51 Correct 170 ms 24012 KB Output is correct
52 Correct 177 ms 23956 KB Output is correct
53 Correct 69 ms 14108 KB Output is correct
54 Correct 73 ms 14096 KB Output is correct
55 Correct 166 ms 23952 KB Output is correct
56 Correct 181 ms 23960 KB Output is correct
57 Correct 68 ms 14100 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 240 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 43 ms 5708 KB Output is correct
62 Correct 175 ms 22080 KB Output is correct
63 Correct 165 ms 22028 KB Output is correct
64 Correct 176 ms 22044 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 43 ms 5792 KB Output is correct
67 Correct 171 ms 22172 KB Output is correct
68 Correct 163 ms 22092 KB Output is correct
69 Correct 179 ms 22092 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 43 ms 5784 KB Output is correct
74 Correct 168 ms 22048 KB Output is correct
75 Correct 184 ms 21996 KB Output is correct
76 Correct 175 ms 22048 KB Output is correct
77 Correct 161 ms 22048 KB Output is correct
78 Correct 1 ms 328 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 4 ms 852 KB Output is correct
81 Correct 91 ms 14096 KB Output is correct
82 Correct 0 ms 328 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 0 ms 212 KB Output is correct
85 Correct 43 ms 6260 KB Output is correct
86 Correct 76 ms 14108 KB Output is correct
87 Correct 76 ms 14108 KB Output is correct
88 Correct 73 ms 14028 KB Output is correct
89 Correct 75 ms 14156 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 4 ms 852 KB Output is correct
93 Correct 89 ms 14096 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 1 ms 340 KB Output is correct
96 Correct 0 ms 212 KB Output is correct
97 Correct 24 ms 3796 KB Output is correct
98 Correct 79 ms 14108 KB Output is correct
99 Correct 76 ms 14096 KB Output is correct
100 Correct 73 ms 14028 KB Output is correct
101 Correct 142 ms 14108 KB Output is correct
102 Correct 82 ms 14172 KB Output is correct
103 Correct 84 ms 14096 KB Output is correct
104 Correct 76 ms 14104 KB Output is correct
105 Correct 102 ms 14148 KB Output is correct