답안 #732269

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
732269 2023-04-28T22:33:59 Z aykhn 슈퍼트리 잇기 (IOI20_supertrees) C++14
96 / 100
223 ms 22420 KB
#include <bits/stdc++.h>
#include "supertrees.h"
 
/*
    author: aykhn
    4/29/2023
*/
 
using namespace std;
 
typedef long long ll;
 
#define OPT ios_base::sync_with_stdio(0); \
            cin.tie(0); \
            cout.tie(0)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pss pair<long,long>
#define all(v) v.begin(), v.end()
#define mpr make_pair
#define pb push_back
#define ts to_string
#define fi first
#define se second
#define inf 0x3F3F3F3F
#define infll 0x3F3F3F3F3F3F3F3FLL
#define bpc __builtin_popcount
#define print(v) for(int i = 0; i < v.size(); i++) cout << v[i] << " "; cout<<endl;
 
struct DSU
{
    vector<int> par;
    vector<vector<int>> e;
 
    void init(int n)
    {
        e.resize(n);
        par.resize(n);
        for (int i = 0; i < n; i++)
        {
            e[i].pb(i);
            par[i] = i;
        }
    }
 
    int get(int x)
    {
        if (par[x] == x) return x;
        return par[x] = get(par[x]);
    }
 
    bool tog(int x, int y)
    {
        return get(x) == get(y);
    }
 
    void unite(int x, int y)
    {
        x = get(x);
        y = get(y);
 
        if (x == y) return;
 
        if (e[x].size() < e[y].size()) swap(x, y);
 
        for (int a : e[y])
        {
            e[x].pb(a);
            par[a] = x;
        }
 
        par[y] = x;
 
        e[y].clear();
    }
};
 
int construct(vector<vector<int>> p)
{
    int n = p.size();
 
    vector<vector<int>> b(n, vector<int> (n, 0));
    vector<int> used(n, 0), used1(n, 0), used2(n, 0);
 
    DSU dsu, dsu1, dsu2;
    dsu.init(n);
    dsu1.init(n);
    dsu2.init(n);
 
    bool flag = false;
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (p[i][j] == 1) dsu.unite(i, j);
        }
    }
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (p[i][j] != 1 && dsu.tog(i, j)) return 0;
        }
    }
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (p[i][j] == 2) dsu1.unite(dsu.get(i), dsu.get(j));
            else if (p[i][j] == 3) dsu2.unite(dsu.get(i), dsu.get(j));
        }
    }
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (i == j) continue;
            if (dsu1.tog(i, j) && dsu2.tog(i, j)) return 0;
            if (!p[i][j] && (dsu1.tog(i, j) || dsu2.tog(i, j))) return 0;
          	if (p[i][j] == 2 && dsu2.tog(i, j)) return 0;
          	if (p[i][j] == 3 && dsu1.tog(i, j)) return 0;
        }
    }
/*
4
1 1 1 2
1 1 2 2
1 2 1 2
2 2 2 1
*/
    for (int i = 0; i < n; i++)
    {
        int x = dsu.get(i);
        if (dsu1.e[x].size() >= 3 && dsu2.e[x].size() >= 4) return 0;
        if (dsu2.e[x].size() == 2 || dsu2.e[x].size() == 3) return 0;
        if (dsu1.e[x].size() == 2) return 0;
        if (!used[x])
        {
            for (int j = 0; j + 1 < dsu.e[x].size(); j++)
            {
                b[dsu.e[x][j]][dsu.e[x][j + 1]] = 1;
                b[dsu.e[x][j + 1]][dsu.e[x][j]] = 1;
            }
            used[x] = 1;
        }
        x = dsu1.get(i);
        if (!used1[x])
        {
            for (int j = 0; j + 1 < dsu1.e[x].size(); j++)
            {
                b[dsu1.e[x][j]][dsu1.e[x][j + 1]] = 1;
                b[dsu1.e[x][j + 1]][dsu1.e[x][j]] = 1;
            }
 
            if (dsu1.e[x].size() >= 3)
            {
                b[dsu1.e[x][dsu1.e[x].size() - 1]][dsu1.e[x][0]] = 1;
                b[dsu1.e[x][0]][dsu1.e[x][dsu1.e[x].size() - 1]] = 1;
            }
            used1[x] = 1;
        }
        x = dsu2.get(i);
        if (!used2[x])
        {
            for (int j = 0; j + 1 < dsu2.e[x].size(); j++)
            {
                b[dsu2.e[x][j]][dsu2.e[x][j + 1]] = 1;
                b[dsu2.e[x][j + 1]][dsu2.e[x][j]] = 1;
            }
 
            if (dsu2.e[x].size() >= 4)
            {
                b[dsu2.e[x][dsu2.e[x].size() - 1]][dsu2.e[x][0]] = 1;
                b[dsu2.e[x][0]][dsu2.e[x][dsu2.e[x].size() - 1]] = 1;
                b[dsu2.e[x][2]][dsu2.e[x][0]] = 1;
                b[dsu2.e[x][0]][dsu2.e[x][2]] = 1;
            }
 
            used2[x] = 1;
        }
    }
    for (int i = 0; i < n; i++) b[i][i] = 0;
 
    build(b);
 
    return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:143:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  143 |             for (int j = 0; j + 1 < dsu.e[x].size(); j++)
      |                             ~~~~~~^~~~~~~~~~~~~~~~~
supertrees.cpp:153:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  153 |             for (int j = 0; j + 1 < dsu1.e[x].size(); j++)
      |                             ~~~~~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:169:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  169 |             for (int j = 0; j + 1 < dsu2.e[x].size(); j++)
      |                             ~~~~~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:90:10: warning: unused variable 'flag' [-Wunused-variable]
   90 |     bool flag = false;
      |          ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 8 ms 1220 KB Output is correct
7 Correct 176 ms 22212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 8 ms 1220 KB Output is correct
7 Correct 176 ms 22212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 9 ms 1108 KB Output is correct
13 Correct 169 ms 22160 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 724 KB Output is correct
17 Correct 87 ms 12288 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 49 ms 5760 KB Output is correct
21 Correct 180 ms 22348 KB Output is correct
22 Correct 215 ms 22220 KB Output is correct
23 Correct 194 ms 22220 KB Output is correct
24 Correct 172 ms 22116 KB Output is correct
25 Correct 75 ms 12308 KB Output is correct
26 Correct 94 ms 12364 KB Output is correct
27 Correct 189 ms 22152 KB Output is correct
28 Correct 180 ms 22152 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 8 ms 1108 KB Output is correct
9 Correct 179 ms 22112 KB Output is correct
10 Correct 1 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 223 ms 22152 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 86 ms 12292 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 51 ms 5824 KB Output is correct
22 Correct 184 ms 22144 KB Output is correct
23 Correct 184 ms 22176 KB Output is correct
24 Correct 203 ms 22208 KB Output is correct
25 Correct 94 ms 12236 KB Output is correct
26 Correct 74 ms 12236 KB Output is correct
27 Correct 191 ms 22176 KB Output is correct
28 Correct 211 ms 22296 KB Output is correct
29 Correct 100 ms 12288 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 212 KB Output is correct
4 Correct 46 ms 5812 KB Output is correct
5 Correct 180 ms 22220 KB Output is correct
6 Correct 206 ms 22148 KB Output is correct
7 Correct 220 ms 22220 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 47 ms 5768 KB Output is correct
10 Correct 184 ms 22420 KB Output is correct
11 Correct 181 ms 22220 KB Output is correct
12 Correct 206 ms 22324 KB Output is correct
13 Correct 0 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 47 ms 5812 KB Output is correct
17 Correct 196 ms 22144 KB Output is correct
18 Correct 205 ms 22152 KB Output is correct
19 Correct 192 ms 22232 KB Output is correct
20 Correct 187 ms 22160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 8 ms 1220 KB Output is correct
7 Correct 176 ms 22212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 9 ms 1108 KB Output is correct
13 Correct 169 ms 22160 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 724 KB Output is correct
17 Correct 87 ms 12288 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 49 ms 5760 KB Output is correct
21 Correct 180 ms 22348 KB Output is correct
22 Correct 215 ms 22220 KB Output is correct
23 Correct 194 ms 22220 KB Output is correct
24 Correct 172 ms 22116 KB Output is correct
25 Correct 75 ms 12308 KB Output is correct
26 Correct 94 ms 12364 KB Output is correct
27 Correct 189 ms 22152 KB Output is correct
28 Correct 180 ms 22152 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 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 8 ms 1108 KB Output is correct
37 Correct 179 ms 22112 KB Output is correct
38 Correct 1 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 223 ms 22152 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 724 KB Output is correct
45 Correct 86 ms 12292 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 51 ms 5824 KB Output is correct
50 Correct 184 ms 22144 KB Output is correct
51 Correct 184 ms 22176 KB Output is correct
52 Correct 203 ms 22208 KB Output is correct
53 Correct 94 ms 12236 KB Output is correct
54 Correct 74 ms 12236 KB Output is correct
55 Correct 191 ms 22176 KB Output is correct
56 Correct 211 ms 22296 KB Output is correct
57 Correct 100 ms 12288 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 4 ms 724 KB Output is correct
61 Correct 80 ms 12292 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 47 ms 5764 KB Output is correct
66 Correct 77 ms 12292 KB Output is correct
67 Correct 71 ms 12296 KB Output is correct
68 Correct 90 ms 12236 KB Output is correct
69 Correct 91 ms 12300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 8 ms 1220 KB Output is correct
7 Correct 176 ms 22212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 9 ms 1108 KB Output is correct
13 Correct 169 ms 22160 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 724 KB Output is correct
17 Correct 87 ms 12288 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 49 ms 5760 KB Output is correct
21 Correct 180 ms 22348 KB Output is correct
22 Correct 215 ms 22220 KB Output is correct
23 Correct 194 ms 22220 KB Output is correct
24 Correct 172 ms 22116 KB Output is correct
25 Correct 75 ms 12308 KB Output is correct
26 Correct 94 ms 12364 KB Output is correct
27 Correct 189 ms 22152 KB Output is correct
28 Correct 180 ms 22152 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 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 8 ms 1108 KB Output is correct
37 Correct 179 ms 22112 KB Output is correct
38 Correct 1 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 223 ms 22152 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 724 KB Output is correct
45 Correct 86 ms 12292 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 51 ms 5824 KB Output is correct
50 Correct 184 ms 22144 KB Output is correct
51 Correct 184 ms 22176 KB Output is correct
52 Correct 203 ms 22208 KB Output is correct
53 Correct 94 ms 12236 KB Output is correct
54 Correct 74 ms 12236 KB Output is correct
55 Correct 191 ms 22176 KB Output is correct
56 Correct 211 ms 22296 KB Output is correct
57 Correct 100 ms 12288 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 46 ms 5812 KB Output is correct
62 Correct 180 ms 22220 KB Output is correct
63 Correct 206 ms 22148 KB Output is correct
64 Correct 220 ms 22220 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 47 ms 5768 KB Output is correct
67 Correct 184 ms 22420 KB Output is correct
68 Correct 181 ms 22220 KB Output is correct
69 Correct 206 ms 22324 KB Output is correct
70 Correct 0 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 47 ms 5812 KB Output is correct
74 Correct 196 ms 22144 KB Output is correct
75 Correct 205 ms 22152 KB Output is correct
76 Correct 192 ms 22232 KB Output is correct
77 Correct 187 ms 22160 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 4 ms 724 KB Output is correct
81 Correct 80 ms 12292 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 47 ms 5764 KB Output is correct
86 Correct 77 ms 12292 KB Output is correct
87 Correct 71 ms 12296 KB Output is correct
88 Correct 90 ms 12236 KB Output is correct
89 Correct 91 ms 12300 KB Output is correct
90 Correct 0 ms 212 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 4 ms 724 KB Output is correct
93 Correct 77 ms 12288 KB Output is correct
94 Correct 0 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 0 ms 212 KB Output is correct
97 Correct 22 ms 3340 KB Output is correct
98 Correct 109 ms 12288 KB Output is correct
99 Correct 83 ms 12316 KB Output is correct
100 Correct 79 ms 12300 KB Output is correct
101 Correct 82 ms 12304 KB Output is correct
102 Incorrect 200 ms 22144 KB Answer gives possible 1 while actual possible 0
103 Halted 0 ms 0 KB -