Submission #414956

# Submission time Handle Problem Language Result Execution time Memory
414956 2021-05-31T11:07:48 Z MKopchev Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
289 ms 24048 KB
#include "supertrees.h"
#include<bits/stdc++.h>
using namespace std;

//void build(std::vector<std::vector<int>> _b);

const int nmax=1e3+42;

int parent[nmax];
int root(int node)
{
    if(node==parent[node])return node;
    parent[node]=root(parent[node]);
    return parent[node];
}

vector< int > seen[nmax];

int construct(std::vector<std::vector<int>> p) {
	int n = p.size();

	for(int i=0;i<n;i++)parent[i]=i;

	for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            if(p[i][j])parent[root(i)]=root(j);

	for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            if(p[i][j]==0&&root(i)==root(j))return 0;

    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            if(p[i][j]==3)return 0;

    for(int i=0;i<n;i++)
        seen[root(i)].push_back(i);

	std::vector<std::vector<int>> answer;
	for (int i = 0; i < n; i++) {
		std::vector<int> row;
		row.resize(n);
		answer.push_back(row);
	}

	for(int i=0;i<n;i++)
        if(seen[i].size()>1)
        {
            vector<int> me=seen[i];

            for(auto w:me)parent[w]=w;

            for(auto u:me)
                for(auto v:me)
                    if(p[u][v]==1)parent[root(u)]=root(v);

            for(auto u:me)
                for(auto v:me)
                    if(p[u][v]==2&&root(u)==root(v))return 0;

            map<int, vector<int> > help={};

            for(auto u:me)
                help[root(u)].push_back(u);

            vector<int> starts={};

            for(auto w:help)
            {
                vector<int> path=w.second;

                starts.push_back(path[0]);

                for(int j=1;j<path.size();j++)
                {
                    int s=path[j-1];
                    int t=path[j];

                    answer[s][t]=1;
                    answer[t][s]=1;
                }
            }

            if(starts.size()==2)return 0;

            if(starts.size()>1)
            {
                for(int i=0;i<starts.size();i++)
                {
                    int u=starts[i];
                    int v=starts[(i+1)%starts.size()];

                    answer[u][v]=1;
                    answer[v][u]=1;
                }
            }
        }

	build(answer);
	return 1;
}
/*
static int n;
static std::vector<std::vector<int>> p;
static std::vector<std::vector<int>> b;
static bool called = false;

static void check(bool cond, std::string message) {
    if (!cond) {
        printf("%s\n", message.c_str());
        fclose(stdout);
        exit(0);
    }
}

void build(std::vector<std::vector<int>> _b) {
    check(!called, "build is called more than once");
    called = true;
    check((int)_b.size() == n, "Invalid number of rows in b");
    for (int i = 0; i < n; i++) {
        check((int)_b[i].size() == n, "Invalid number of columns in b");
    }
    b = _b;
}

int main() {
    assert(scanf("%d", &n) == 1);

    p.resize(n);
    for (int i = 0; i < n; i++) {
        p[i].resize(n);
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            assert(scanf("%d", &p[i][j]) == 1);
        }
    }
    fclose(stdin);

    int possible = construct(p);

    check(possible == 0 || possible == 1, "Invalid return value of construct");
    if (possible == 1) {
        check(called, "construct returned 1 without calling build");
    } else {
        check(!called, "construct called build but returned 0");
    }

    printf("%d\n", possible);
    if (possible == 1) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (j) {
                    printf(" ");
                }
                printf("%d", b[i][j]);
            }
            printf("\n");
        }
    }
    fclose(stdout);
}
*/

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:36:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   36 |     for(int i=0;i<n;i++)
      |     ^~~
supertrees.cpp:39:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   39 |  std::vector<std::vector<int>> answer;
      |  ^~~
supertrees.cpp:74:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |                 for(int j=1;j<path.size();j++)
      |                             ~^~~~~~~~~~~~
supertrees.cpp:88:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |                 for(int i=0;i<starts.size();i++)
      |                             ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 12 ms 1300 KB Output is correct
7 Correct 251 ms 22468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 12 ms 1300 KB Output is correct
7 Correct 251 ms 22468 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 12 ms 1228 KB Output is correct
13 Correct 227 ms 22440 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 692 KB Output is correct
17 Correct 139 ms 8496 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 66 ms 6168 KB Output is correct
21 Correct 236 ms 22396 KB Output is correct
22 Correct 255 ms 22436 KB Output is correct
23 Correct 262 ms 22372 KB Output is correct
24 Correct 227 ms 22500 KB Output is correct
25 Correct 96 ms 8516 KB Output is correct
26 Correct 94 ms 8508 KB Output is correct
27 Correct 256 ms 22476 KB Output is correct
28 Correct 230 ms 22424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 10 ms 1228 KB Output is correct
9 Correct 230 ms 24004 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1232 KB Output is correct
13 Correct 241 ms 23952 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 716 KB Output is correct
17 Correct 122 ms 10120 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 324 KB Output is correct
21 Correct 60 ms 6196 KB Output is correct
22 Correct 264 ms 24016 KB Output is correct
23 Correct 245 ms 23952 KB Output is correct
24 Correct 258 ms 23984 KB Output is correct
25 Correct 102 ms 14148 KB Output is correct
26 Correct 105 ms 10120 KB Output is correct
27 Correct 237 ms 24048 KB Output is correct
28 Correct 249 ms 23944 KB Output is correct
29 Correct 113 ms 14092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 60 ms 6100 KB Output is correct
5 Correct 239 ms 22464 KB Output is correct
6 Correct 243 ms 22468 KB Output is correct
7 Correct 289 ms 22468 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 60 ms 6112 KB Output is correct
10 Correct 233 ms 22400 KB Output is correct
11 Correct 229 ms 22468 KB Output is correct
12 Correct 265 ms 22388 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 64 ms 6176 KB Output is correct
17 Correct 235 ms 22476 KB Output is correct
18 Correct 241 ms 22476 KB Output is correct
19 Correct 251 ms 22388 KB Output is correct
20 Correct 250 ms 22432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 12 ms 1300 KB Output is correct
7 Correct 251 ms 22468 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 12 ms 1228 KB Output is correct
13 Correct 227 ms 22440 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 692 KB Output is correct
17 Correct 139 ms 8496 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 66 ms 6168 KB Output is correct
21 Correct 236 ms 22396 KB Output is correct
22 Correct 255 ms 22436 KB Output is correct
23 Correct 262 ms 22372 KB Output is correct
24 Correct 227 ms 22500 KB Output is correct
25 Correct 96 ms 8516 KB Output is correct
26 Correct 94 ms 8508 KB Output is correct
27 Correct 256 ms 22476 KB Output is correct
28 Correct 230 ms 22424 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 324 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 10 ms 1228 KB Output is correct
37 Correct 230 ms 24004 KB Output is correct
38 Correct 1 ms 320 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 11 ms 1232 KB Output is correct
41 Correct 241 ms 23952 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 716 KB Output is correct
45 Correct 122 ms 10120 KB Output is correct
46 Correct 1 ms 320 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 324 KB Output is correct
49 Correct 60 ms 6196 KB Output is correct
50 Correct 264 ms 24016 KB Output is correct
51 Correct 245 ms 23952 KB Output is correct
52 Correct 258 ms 23984 KB Output is correct
53 Correct 102 ms 14148 KB Output is correct
54 Correct 105 ms 10120 KB Output is correct
55 Correct 237 ms 24048 KB Output is correct
56 Correct 249 ms 23944 KB Output is correct
57 Correct 113 ms 14092 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 5 ms 716 KB Output is correct
61 Correct 111 ms 10132 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 316 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 58 ms 6264 KB Output is correct
66 Correct 99 ms 10260 KB Output is correct
67 Correct 96 ms 10052 KB Output is correct
68 Correct 116 ms 10116 KB Output is correct
69 Correct 102 ms 14120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 12 ms 1300 KB Output is correct
7 Correct 251 ms 22468 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 12 ms 1228 KB Output is correct
13 Correct 227 ms 22440 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 692 KB Output is correct
17 Correct 139 ms 8496 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 66 ms 6168 KB Output is correct
21 Correct 236 ms 22396 KB Output is correct
22 Correct 255 ms 22436 KB Output is correct
23 Correct 262 ms 22372 KB Output is correct
24 Correct 227 ms 22500 KB Output is correct
25 Correct 96 ms 8516 KB Output is correct
26 Correct 94 ms 8508 KB Output is correct
27 Correct 256 ms 22476 KB Output is correct
28 Correct 230 ms 22424 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 324 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 10 ms 1228 KB Output is correct
37 Correct 230 ms 24004 KB Output is correct
38 Correct 1 ms 320 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 11 ms 1232 KB Output is correct
41 Correct 241 ms 23952 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 716 KB Output is correct
45 Correct 122 ms 10120 KB Output is correct
46 Correct 1 ms 320 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 324 KB Output is correct
49 Correct 60 ms 6196 KB Output is correct
50 Correct 264 ms 24016 KB Output is correct
51 Correct 245 ms 23952 KB Output is correct
52 Correct 258 ms 23984 KB Output is correct
53 Correct 102 ms 14148 KB Output is correct
54 Correct 105 ms 10120 KB Output is correct
55 Correct 237 ms 24048 KB Output is correct
56 Correct 249 ms 23944 KB Output is correct
57 Correct 113 ms 14092 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 60 ms 6100 KB Output is correct
62 Correct 239 ms 22464 KB Output is correct
63 Correct 243 ms 22468 KB Output is correct
64 Correct 289 ms 22468 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 60 ms 6112 KB Output is correct
67 Correct 233 ms 22400 KB Output is correct
68 Correct 229 ms 22468 KB Output is correct
69 Correct 265 ms 22388 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 320 KB Output is correct
73 Correct 64 ms 6176 KB Output is correct
74 Correct 235 ms 22476 KB Output is correct
75 Correct 241 ms 22476 KB Output is correct
76 Correct 251 ms 22388 KB Output is correct
77 Correct 250 ms 22432 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 5 ms 716 KB Output is correct
81 Correct 111 ms 10132 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 1 ms 316 KB Output is correct
84 Correct 1 ms 204 KB Output is correct
85 Correct 58 ms 6264 KB Output is correct
86 Correct 99 ms 10260 KB Output is correct
87 Correct 96 ms 10052 KB Output is correct
88 Correct 116 ms 10116 KB Output is correct
89 Correct 102 ms 14120 KB Output is correct
90 Correct 1 ms 220 KB Output is correct
91 Correct 1 ms 204 KB Output is correct
92 Correct 7 ms 692 KB Output is correct
93 Correct 109 ms 10124 KB Output is correct
94 Correct 1 ms 204 KB Output is correct
95 Correct 1 ms 316 KB Output is correct
96 Correct 1 ms 204 KB Output is correct
97 Correct 24 ms 2700 KB Output is correct
98 Correct 106 ms 10124 KB Output is correct
99 Correct 108 ms 10156 KB Output is correct
100 Correct 98 ms 10052 KB Output is correct
101 Correct 98 ms 10128 KB Output is correct
102 Correct 123 ms 10124 KB Output is correct
103 Correct 132 ms 10036 KB Output is correct
104 Correct 105 ms 10044 KB Output is correct
105 Correct 117 ms 10128 KB Output is correct