답안 #603601

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
603601 2022-07-24T08:47:37 Z Mher 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
194 ms 28108 KB
#include <bits/stdc++.h>
#include "supertrees.h"
#include <vector>

using namespace std;

int n;
vector<vector<int>> a;
vector<int> col;
vector<vector<int>> r;
vector<int> gr;
vector<vector<int>> c;
vector<vector<int>> ans;

bool check()
{
    for (int i = 0; i < n; i++)
    {
        //cout << col[i] << ' ' << gr[col[i]] << endl;
        for (int j = 0; j < n; j++)
        {
            if (a[i][j] == 1)
            {
                if (col[i] != col[j])
                    return false;
            }
            else if (a[i][j] == 2)
            {
                if (col[i] == col[j] || gr[col[i]] != gr[col[j]])
                    return false;
            }
            else
            {
                if (col[i] == col[j] || gr[col[i]] == gr[col[j]])
                    return false;
            }
        }
    }
    for (int i = 0; i < c.size(); i++)
    {
        if (c[i].size() == 2)
            return false;
    }
    return true;
}

void conect(int v, int u)
{
    ans[v][u] = 1;
    ans[u][v] = 1;
}

void dfs(int v)
{
    for (int i = 0; i < n; i++)
    {
        if (i == v)
            continue;
        if (a[v][i] == 1 && col[i] == -1)
        {
            r[col[v]].push_back(i);
            col[i] = col[v];
            dfs(i);
        }
    }
}

void dfs2(int v)
{
    int u = r[v][0];
    for (int i = 0; i < r.size(); i++)
    {
        if (i == v)
            continue;
        int to = r[i][0];
        if (a[u][to] == 2 && gr[i] == -1)
        {
            c[gr[v]].push_back(i);
            gr[i] = gr[v];
            dfs2(i);
        }
    }
}

int construct(vector<vector<int>> p) {
	n = p.size();
	for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (p[i][j] == 3)
                return 0;
        }
    }
    a = p;
    ans = vector<vector<int>>(n, vector<int>(n, 0));
    int cnt = 0;
    col = vector<int>(n, -1);
    for (int i = 0; i < n; i++)
    {
        if (col[i] == -1)
        {
            r.push_back(vector<int>(1, i));
            col[i] = cnt++;
            dfs(i);
        }
    }
    cnt = 0;
    gr = vector<int>(r.size(), -1);
    for (int i = 0; i < r.size(); i++)
    {
        if (gr[i] == -1)
        {
            c.push_back(vector<int>(1, i));
            gr[i] = cnt++;
            dfs2(i);
        }
    }
    if (!check())
        return 0;
    for (int i = 0; i < r.size(); i++)
    {
        for (int j = 0; j < r[i].size() - 1; j++)
        {
            conect(r[i][j], r[i][j + 1]);
        }
    }
    for (int i = 0; i < c.size(); i++)
    {
        for (int j = 0; j < c[i].size() - 1; j++)
        {
            //cout << r[c[i][j]][0] << ' ' << r[c[i][j + 1]][0] << endl;
            conect(r[c[i][j]][0], r[c[i][j + 1]][0]);
        }
        if (c[i].size() != 1)
        {
            //cout << r[c[i][c[i].size() - 1]][0] << ' ' << r[c[i][0]][0] << endl;
            conect(r[c[i][c[i].size() - 1]][0], r[c[i][0]][0]);
        }
    }
    build(ans);
	return 1;
}

Compilation message

supertrees.cpp: In function 'bool check()':
supertrees.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     for (int i = 0; i < c.size(); i++)
      |                     ~~^~~~~~~~~~
supertrees.cpp: In function 'void dfs2(int)':
supertrees.cpp:71:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |     for (int i = 0; i < r.size(); i++)
      |                     ~~^~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:110:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |     for (int i = 0; i < r.size(); i++)
      |                     ~~^~~~~~~~~~
supertrees.cpp:121:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |     for (int i = 0; i < r.size(); i++)
      |                     ~~^~~~~~~~~~
supertrees.cpp:123:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |         for (int j = 0; j < r[i].size() - 1; j++)
      |                         ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:128:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  128 |     for (int i = 0; i < c.size(); i++)
      |                     ~~^~~~~~~~~~
supertrees.cpp:130:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |         for (int j = 0; j < c[i].size() - 1; j++)
      |                         ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 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 1340 KB Output is correct
7 Correct 166 ms 27956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 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 1340 KB Output is correct
7 Correct 166 ms 27956 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1364 KB Output is correct
13 Correct 167 ms 28052 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 300 KB Output is correct
16 Correct 4 ms 928 KB Output is correct
17 Correct 88 ms 18084 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 7220 KB Output is correct
21 Correct 182 ms 27852 KB Output is correct
22 Correct 181 ms 27952 KB Output is correct
23 Correct 178 ms 27948 KB Output is correct
24 Correct 173 ms 28020 KB Output is correct
25 Correct 76 ms 18124 KB Output is correct
26 Correct 70 ms 18084 KB Output is correct
27 Correct 192 ms 27884 KB Output is correct
28 Correct 174 ms 27988 KB Output is correct
# 결과 실행 시간 메모리 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 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 8 ms 1364 KB Output is correct
9 Correct 169 ms 28048 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1464 KB Output is correct
13 Correct 184 ms 27968 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 980 KB Output is correct
17 Correct 90 ms 18164 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 43 ms 7232 KB Output is correct
22 Correct 171 ms 28032 KB Output is correct
23 Correct 167 ms 28016 KB Output is correct
24 Correct 182 ms 28012 KB Output is correct
25 Correct 74 ms 18036 KB Output is correct
26 Correct 81 ms 18036 KB Output is correct
27 Correct 172 ms 27948 KB Output is correct
28 Correct 194 ms 28108 KB Output is correct
29 Correct 73 ms 18156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 44 ms 6932 KB Output is correct
5 Correct 171 ms 26084 KB Output is correct
6 Correct 172 ms 26128 KB Output is correct
7 Correct 176 ms 26068 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 44 ms 7020 KB Output is correct
10 Correct 176 ms 26092 KB Output is correct
11 Correct 165 ms 26188 KB Output is correct
12 Correct 183 ms 26204 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 43 ms 7240 KB Output is correct
17 Correct 172 ms 28000 KB Output is correct
18 Correct 168 ms 27980 KB Output is correct
19 Correct 174 ms 27896 KB Output is correct
20 Correct 171 ms 28064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 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 1340 KB Output is correct
7 Correct 166 ms 27956 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1364 KB Output is correct
13 Correct 167 ms 28052 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 300 KB Output is correct
16 Correct 4 ms 928 KB Output is correct
17 Correct 88 ms 18084 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 7220 KB Output is correct
21 Correct 182 ms 27852 KB Output is correct
22 Correct 181 ms 27952 KB Output is correct
23 Correct 178 ms 27948 KB Output is correct
24 Correct 173 ms 28020 KB Output is correct
25 Correct 76 ms 18124 KB Output is correct
26 Correct 70 ms 18084 KB Output is correct
27 Correct 192 ms 27884 KB Output is correct
28 Correct 174 ms 27988 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 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1364 KB Output is correct
37 Correct 169 ms 28048 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 7 ms 1464 KB Output is correct
41 Correct 184 ms 27968 KB Output is correct
42 Correct 1 ms 300 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 4 ms 980 KB Output is correct
45 Correct 90 ms 18164 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 43 ms 7232 KB Output is correct
50 Correct 171 ms 28032 KB Output is correct
51 Correct 167 ms 28016 KB Output is correct
52 Correct 182 ms 28012 KB Output is correct
53 Correct 74 ms 18036 KB Output is correct
54 Correct 81 ms 18036 KB Output is correct
55 Correct 172 ms 27948 KB Output is correct
56 Correct 194 ms 28108 KB Output is correct
57 Correct 73 ms 18156 KB Output is correct
58 Correct 1 ms 296 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 4 ms 980 KB Output is correct
61 Correct 80 ms 18036 KB Output is correct
62 Correct 0 ms 304 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Correct 45 ms 6896 KB Output is correct
66 Correct 93 ms 16548 KB Output is correct
67 Correct 72 ms 16516 KB Output is correct
68 Correct 71 ms 16516 KB Output is correct
69 Correct 73 ms 16556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 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 1340 KB Output is correct
7 Correct 166 ms 27956 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1364 KB Output is correct
13 Correct 167 ms 28052 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 300 KB Output is correct
16 Correct 4 ms 928 KB Output is correct
17 Correct 88 ms 18084 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 7220 KB Output is correct
21 Correct 182 ms 27852 KB Output is correct
22 Correct 181 ms 27952 KB Output is correct
23 Correct 178 ms 27948 KB Output is correct
24 Correct 173 ms 28020 KB Output is correct
25 Correct 76 ms 18124 KB Output is correct
26 Correct 70 ms 18084 KB Output is correct
27 Correct 192 ms 27884 KB Output is correct
28 Correct 174 ms 27988 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 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1364 KB Output is correct
37 Correct 169 ms 28048 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 7 ms 1464 KB Output is correct
41 Correct 184 ms 27968 KB Output is correct
42 Correct 1 ms 300 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 4 ms 980 KB Output is correct
45 Correct 90 ms 18164 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 43 ms 7232 KB Output is correct
50 Correct 171 ms 28032 KB Output is correct
51 Correct 167 ms 28016 KB Output is correct
52 Correct 182 ms 28012 KB Output is correct
53 Correct 74 ms 18036 KB Output is correct
54 Correct 81 ms 18036 KB Output is correct
55 Correct 172 ms 27948 KB Output is correct
56 Correct 194 ms 28108 KB Output is correct
57 Correct 73 ms 18156 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 44 ms 6932 KB Output is correct
62 Correct 171 ms 26084 KB Output is correct
63 Correct 172 ms 26128 KB Output is correct
64 Correct 176 ms 26068 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 44 ms 7020 KB Output is correct
67 Correct 176 ms 26092 KB Output is correct
68 Correct 165 ms 26188 KB Output is correct
69 Correct 183 ms 26204 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 43 ms 7240 KB Output is correct
74 Correct 172 ms 28000 KB Output is correct
75 Correct 168 ms 27980 KB Output is correct
76 Correct 174 ms 27896 KB Output is correct
77 Correct 171 ms 28064 KB Output is correct
78 Correct 1 ms 296 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 4 ms 980 KB Output is correct
81 Correct 80 ms 18036 KB Output is correct
82 Correct 0 ms 304 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 0 ms 212 KB Output is correct
85 Correct 45 ms 6896 KB Output is correct
86 Correct 93 ms 16548 KB Output is correct
87 Correct 72 ms 16516 KB Output is correct
88 Correct 71 ms 16516 KB Output is correct
89 Correct 73 ms 16556 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 596 KB Output is correct
93 Correct 72 ms 8464 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 0 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 19 ms 2396 KB Output is correct
98 Correct 65 ms 8488 KB Output is correct
99 Correct 67 ms 8468 KB Output is correct
100 Correct 76 ms 8448 KB Output is correct
101 Correct 63 ms 8484 KB Output is correct
102 Correct 66 ms 8472 KB Output is correct
103 Correct 63 ms 8468 KB Output is correct
104 Correct 69 ms 8480 KB Output is correct
105 Correct 74 ms 8476 KB Output is correct