Submission #363298

# Submission time Handle Problem Language Result Execution time Memory
363298 2021-02-05T12:53:23 Z mathking1021 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
274 ms 24300 KB
#include "supertrees.h"
#include <vector>

using namespace std;

int cp[1005];
int cp2[1005];
vector<int> ve[1005];
vector<int> ve2[1005];
vector<int> ve3;

int fnd(int p)
{
    if(cp[p] == p) return p;
    return cp[p] = fnd(cp[p]);
}

void uni(int p, int q)
{
    if(fnd(p) == fnd(q)) return;
    cp[fnd(p)] = fnd(q);
}

int fnd2(int p)
{
    if(cp2[p] == p) return p;
    return cp2[p] = fnd2(cp2[p]);
}

void uni2(int p, int q)
{
    if(fnd2(p) == fnd2(q)) return;
    cp2[fnd2(p)] = fnd2(q);
}

int construct(vector<vector<int> > p) {
	int n = p.size();
	vector<vector<int> > answer;
	for(int i = 0; i < n; i++)
    {
        cp[i] = i;
        cp2[i] = i;
    }
	for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if(p[i][j] > 2) return 0;
            if(p[i][j]) uni(i, j);
        }
    }
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if(!p[i][j] && fnd(i) == fnd(j)) return 0;
        }
    }
	for(int i = 0; i < n; i++)
    {
		vector<int> row;
		for(int j = 0; j < n; j++)
        {
            row.push_back(0);
        }
		answer.push_back(row);
	}
	for(int i = 0; i < n; i++)
    {
        ve[fnd(i)].push_back(i);
    }
    for(int i = 0; i < n; i++)
    {
        if(ve[i].size() == 0) continue;
        if(ve[i].size() == 1) continue;
        for(int j = 0; j < ve[i].size(); j++)
        {
            for(int k = j + 1; k < ve[i].size(); k++)
            {
                if(p[ve[i][j]][ve[i][k]] == 1) uni2(ve[i][j], ve[i][k]);
            }
        }
        for(int j = 0; j < ve[i].size(); j++)
        {
            ve2[fnd2(ve[i][j])].push_back(ve[i][j]);
        }
        ve3.clear();
        for(int j = 0; j < ve[i].size(); j++)
        {
            if(ve2[ve[i][j]].size() == 0) continue;
            for(int k = 0; k < ve2[ve[i][j]].size(); k++)
            {
                for(int kk = 0; kk < ve2[ve[i][j]].size(); kk++)
                {
                    if(p[ve2[ve[i][j]][k]][ve2[ve[i][j]][kk]] != 1) return 0;
                }
            }
            for(int k = 1; k < ve2[ve[i][j]].size(); k++)
            {
                answer[ve2[ve[i][j]][k]][ve2[ve[i][j]][k - 1]] = 1;
                answer[ve2[ve[i][j]][k - 1]][ve2[ve[i][j]][k]] = 1;
            }
            ve3.push_back(ve[i][j]);
        }
        if(ve3.size() == 2) return 0;
        if(ve3.size() == 1) continue;
        for(int j = 1; j < ve3.size(); j++)
        {
            answer[ve3[j]][ve3[j - 1]] = 1;
            answer[ve3[j - 1]][ve3[j]] = 1;
        }
        answer[ve3[0]][ve3[ve3.size() - 1]] = 1;
        answer[ve3[ve3.size() - 1]][ve3[0]] = 1;
    }
	build(answer);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:76:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |         for(int j = 0; j < ve[i].size(); j++)
      |                        ~~^~~~~~~~~~~~~~
supertrees.cpp:78:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |             for(int k = j + 1; k < ve[i].size(); k++)
      |                                ~~^~~~~~~~~~~~~~
supertrees.cpp:83:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         for(int j = 0; j < ve[i].size(); j++)
      |                        ~~^~~~~~~~~~~~~~
supertrees.cpp:88:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |         for(int j = 0; j < ve[i].size(); j++)
      |                        ~~^~~~~~~~~~~~~~
supertrees.cpp:91:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |             for(int k = 0; k < ve2[ve[i][j]].size(); k++)
      |                            ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:93:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |                 for(int kk = 0; kk < ve2[ve[i][j]].size(); kk++)
      |                                 ~~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:98:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |             for(int k = 1; k < ve2[ve[i][j]].size(); k++)
      |                            ~~^~~~~~~~~~~~~~~~~~~~~~
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 = 1; j < ve3.size(); j++)
      |                        ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 11 ms 1260 KB Output is correct
7 Correct 255 ms 22252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 11 ms 1260 KB Output is correct
7 Correct 255 ms 22252 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 10 ms 1260 KB Output is correct
13 Correct 243 ms 22124 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 5 ms 620 KB Output is correct
17 Correct 119 ms 8428 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 62 ms 5868 KB Output is correct
21 Correct 252 ms 22252 KB Output is correct
22 Correct 248 ms 22252 KB Output is correct
23 Correct 274 ms 22252 KB Output is correct
24 Correct 241 ms 22124 KB Output is correct
25 Correct 105 ms 8300 KB Output is correct
26 Correct 104 ms 8300 KB Output is correct
27 Correct 266 ms 22252 KB Output is correct
28 Correct 243 ms 22252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 10 ms 1388 KB Output is correct
9 Correct 245 ms 24184 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1388 KB Output is correct
13 Correct 255 ms 24300 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 120 ms 10220 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 65 ms 6380 KB Output is correct
22 Correct 254 ms 24172 KB Output is correct
23 Correct 246 ms 24172 KB Output is correct
24 Correct 268 ms 24172 KB Output is correct
25 Correct 112 ms 14316 KB Output is correct
26 Correct 106 ms 10220 KB Output is correct
27 Correct 245 ms 24172 KB Output is correct
28 Correct 266 ms 24172 KB Output is correct
29 Correct 111 ms 14188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 64 ms 5868 KB Output is correct
5 Correct 251 ms 22152 KB Output is correct
6 Correct 249 ms 22252 KB Output is correct
7 Correct 270 ms 22272 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 62 ms 5868 KB Output is correct
10 Correct 252 ms 22332 KB Output is correct
11 Correct 248 ms 22148 KB Output is correct
12 Correct 270 ms 22252 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 62 ms 5868 KB Output is correct
17 Correct 252 ms 22252 KB Output is correct
18 Correct 249 ms 22124 KB Output is correct
19 Correct 248 ms 22124 KB Output is correct
20 Correct 246 ms 22124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 11 ms 1260 KB Output is correct
7 Correct 255 ms 22252 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 10 ms 1260 KB Output is correct
13 Correct 243 ms 22124 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 5 ms 620 KB Output is correct
17 Correct 119 ms 8428 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 62 ms 5868 KB Output is correct
21 Correct 252 ms 22252 KB Output is correct
22 Correct 248 ms 22252 KB Output is correct
23 Correct 274 ms 22252 KB Output is correct
24 Correct 241 ms 22124 KB Output is correct
25 Correct 105 ms 8300 KB Output is correct
26 Correct 104 ms 8300 KB Output is correct
27 Correct 266 ms 22252 KB Output is correct
28 Correct 243 ms 22252 KB Output is correct
29 Correct 0 ms 364 KB Output is correct
30 Correct 0 ms 364 KB Output is correct
31 Correct 0 ms 364 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 0 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 10 ms 1388 KB Output is correct
37 Correct 245 ms 24184 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 11 ms 1388 KB Output is correct
41 Correct 255 ms 24300 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 5 ms 748 KB Output is correct
45 Correct 120 ms 10220 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 65 ms 6380 KB Output is correct
50 Correct 254 ms 24172 KB Output is correct
51 Correct 246 ms 24172 KB Output is correct
52 Correct 268 ms 24172 KB Output is correct
53 Correct 112 ms 14316 KB Output is correct
54 Correct 106 ms 10220 KB Output is correct
55 Correct 245 ms 24172 KB Output is correct
56 Correct 266 ms 24172 KB Output is correct
57 Correct 111 ms 14188 KB Output is correct
58 Correct 0 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 5 ms 748 KB Output is correct
61 Correct 119 ms 10348 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 364 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 63 ms 6380 KB Output is correct
66 Correct 106 ms 10232 KB Output is correct
67 Correct 106 ms 10220 KB Output is correct
68 Correct 107 ms 10220 KB Output is correct
69 Correct 112 ms 14188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 0 ms 364 KB Output is correct
6 Correct 11 ms 1260 KB Output is correct
7 Correct 255 ms 22252 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 10 ms 1260 KB Output is correct
13 Correct 243 ms 22124 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 5 ms 620 KB Output is correct
17 Correct 119 ms 8428 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 62 ms 5868 KB Output is correct
21 Correct 252 ms 22252 KB Output is correct
22 Correct 248 ms 22252 KB Output is correct
23 Correct 274 ms 22252 KB Output is correct
24 Correct 241 ms 22124 KB Output is correct
25 Correct 105 ms 8300 KB Output is correct
26 Correct 104 ms 8300 KB Output is correct
27 Correct 266 ms 22252 KB Output is correct
28 Correct 243 ms 22252 KB Output is correct
29 Correct 0 ms 364 KB Output is correct
30 Correct 0 ms 364 KB Output is correct
31 Correct 0 ms 364 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 0 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 10 ms 1388 KB Output is correct
37 Correct 245 ms 24184 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 11 ms 1388 KB Output is correct
41 Correct 255 ms 24300 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 5 ms 748 KB Output is correct
45 Correct 120 ms 10220 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 65 ms 6380 KB Output is correct
50 Correct 254 ms 24172 KB Output is correct
51 Correct 246 ms 24172 KB Output is correct
52 Correct 268 ms 24172 KB Output is correct
53 Correct 112 ms 14316 KB Output is correct
54 Correct 106 ms 10220 KB Output is correct
55 Correct 245 ms 24172 KB Output is correct
56 Correct 266 ms 24172 KB Output is correct
57 Correct 111 ms 14188 KB Output is correct
58 Correct 0 ms 364 KB Output is correct
59 Correct 0 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 64 ms 5868 KB Output is correct
62 Correct 251 ms 22152 KB Output is correct
63 Correct 249 ms 22252 KB Output is correct
64 Correct 270 ms 22272 KB Output is correct
65 Correct 0 ms 364 KB Output is correct
66 Correct 62 ms 5868 KB Output is correct
67 Correct 252 ms 22332 KB Output is correct
68 Correct 248 ms 22148 KB Output is correct
69 Correct 270 ms 22252 KB Output is correct
70 Correct 1 ms 364 KB Output is correct
71 Correct 0 ms 364 KB Output is correct
72 Correct 1 ms 364 KB Output is correct
73 Correct 62 ms 5868 KB Output is correct
74 Correct 252 ms 22252 KB Output is correct
75 Correct 249 ms 22124 KB Output is correct
76 Correct 248 ms 22124 KB Output is correct
77 Correct 246 ms 22124 KB Output is correct
78 Correct 0 ms 364 KB Output is correct
79 Correct 1 ms 364 KB Output is correct
80 Correct 5 ms 748 KB Output is correct
81 Correct 119 ms 10348 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct
84 Correct 1 ms 364 KB Output is correct
85 Correct 63 ms 6380 KB Output is correct
86 Correct 106 ms 10232 KB Output is correct
87 Correct 106 ms 10220 KB Output is correct
88 Correct 107 ms 10220 KB Output is correct
89 Correct 112 ms 14188 KB Output is correct
90 Correct 1 ms 364 KB Output is correct
91 Correct 1 ms 364 KB Output is correct
92 Correct 5 ms 748 KB Output is correct
93 Correct 107 ms 10220 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 1 ms 364 KB Output is correct
96 Correct 1 ms 364 KB Output is correct
97 Correct 28 ms 2796 KB Output is correct
98 Correct 105 ms 10220 KB Output is correct
99 Correct 104 ms 10348 KB Output is correct
100 Correct 101 ms 10220 KB Output is correct
101 Correct 101 ms 10240 KB Output is correct
102 Correct 106 ms 10220 KB Output is correct
103 Correct 107 ms 10348 KB Output is correct
104 Correct 104 ms 10232 KB Output is correct
105 Correct 111 ms 10348 KB Output is correct