답안 #413156

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
413156 2021-05-28T10:13:14 Z losmi247 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
930 ms 26076 KB
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
typedef long long ll;
const int N = 1005;

int n;
int p[N][N];



int linija(){
    vector <vector<int>> ans;
    for(int i = 1; i <= n; i++){
        vector <int> fg;
        for(int j = 1; j <= n; j++){
            if(j == i+1 || j == i-1) fg.push_back(1);
            else fg.push_back(0);
        }
        ans.push_back(fg);
    }
    build(ans);
    return 1;
}

bool visited[N];
int komp[N];
int cnt = 1;
vector <int> sad;
void dfs(int x){
    visited[x] = 1;
    komp[x] = cnt;
    sad.push_back(x);
    for(int j = 1; j <= n; j++){
        if(p[x][j] && !visited[j]) dfs(j);
    }
}

int drvo(){
    vector <vector<int>> ans;
    for(int i = 0; i < n; i++){
        vector <int> sta(n,0);
        ans.push_back(sta);
    }

    for(int i = 1; i <= n; i++){
        if(visited[i]) continue;
        sad.clear();
        dfs(i);
        for(int j = 0; j < sad.size()-1; j++){
            ans[sad[j]-1][sad[j+1]-1] = ans[sad[j+1]-1][sad[j]-1] = 1;
        }
        cnt++;
    }

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(komp[i] == komp[j] && !p[i][j]) return 0;
        }
    }

    build(ans);
    return 1;
}

int krugovi(){
    vector <vector<int>> ans;
    for(int i = 0; i < n; i++){
        vector <int> sta(n,0);
        ans.push_back(sta);
    }

    for(int i = 1; i <= n; i++){
        if(visited[i]) continue;
        sad.clear();
        dfs(i);
        if(sad.size() == 2) return 0;
        for(int j = 0; j < sad.size()-1; j++){
            ans[sad[j]-1][sad[j+1]-1] = ans[sad[j+1]-1][sad[j]-1] = 1;
        }
        if(sad.size() > 1) ans[sad.back()-1][sad[0]-1] = ans[sad[0]-1][sad.back()-1] = 1;
        cnt++;
    }

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(komp[i] == komp[j] && !p[i][j]) return 0;
        }
    }

    build(ans);
    return 1;
}

int pre[N],bio[N],biopre[N],komstb[N],rut[N];
int brs = 1;
bool nevalja = 0;
vector <int> stablo;
void dfs1(int x){
    bio[x] = 1;
    komstb[x] = brs;
    stablo.push_back(x);

    for(int h = 1; h <= n; h++){
        if(p[x][h] == 1){
            if(pre[x] || biopre[x]){
                nevalja = 1;
                return;
            }
            if(!bio[h]) dfs1(h);
        }
    }
}

vector <vector<int>> stb[N];
int dodva(){
    vector <vector<int>> ans;
    for(int i = 0; i < n; i++){
        vector <int> sta(n,0);
        ans.push_back(sta);
    }

    for(int k = 1; k <= n; k++){
        for(int i = 1; i <= n; i++){
            if(p[i][k] == 1) continue;
            for(int j = 1; j <= n; j++){
                if(p[i][j] == 1 && p[j][k] == 1/* && p[i][k] != 1*/) return 0;
            }
        }
    }

    for(int i = 1; i <= n; i++){
        if(visited[i]) continue;
        sad.clear();
        for(int j = 1; j <= n; j++) pre[j] = visited[j];
        dfs(i);

        /// resi komponentu
        stb[cnt].push_back({0});
        for(auto f : sad) bio[f] = komstb[f] = 0;
        brs = 1;
        for(auto u : sad){
            if(bio[u]){
                continue;
            }

            stablo.clear();
            nevalja = 0;
            dfs1(u);
            if(nevalja) return 0;
            for(auto h : stablo) biopre[h] = 1;

            stb[cnt].push_back(stablo);
            rut[brs] = u;
            brs++;
        }

        for(auto u : sad){
            for(auto v : sad){
                if(komstb[u] != komstb[v] && p[u][v] != 2) return 0;
            }
        }

        for(int j = 1; j < brs; j++){
            for(int h = 0; h < stb[cnt][j].size()-1; h++){
                ans[stb[cnt][j][h]-1][stb[cnt][j][h+1]-1] = ans[stb[cnt][j][h+1]-1][stb[cnt][j][h]-1] = 1;
            }
            if(j < brs-1) ans[rut[j]-1][rut[j+1]-1] = ans[rut[j+1]-1][rut[j]-1] = 1;
        }
        if(brs-1 == 2){
            return 0;
        }
        if(brs-1 >= 2) ans[rut[brs-1]-1][rut[1]-1] = ans[rut[1]-1][rut[brs-1]-1] = 1;

        cnt++;
    }

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(komp[i] != komp[j] && p[i][j] != 0) return 0;
        }
    }

    for(int i = 1; i < cnt; i++){
        for(int j = 1; j < stb[cnt].size(); j++){
            for(auto u : stb[cnt][j]){
                for(auto v : stb[cnt][j]){
                    if(p[u][v] != 1) return 0;
                }
            }
        }
    }

    build(ans);
    return 1;
}

int construct(vector <vector<int>> nz){
    n = nz.size();
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            p[i][j] = nz[i-1][j-1];
        }
    }

    bool prvi = 0;
    for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) prvi |= (p[i][j] != 1);
    if(!prvi){
        return linija();
    }

    bool drugi = 1;
    for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(p[i][j] >= 2) drugi = 0;
    if(drugi){
        return drvo();
    }

    bool treci = 1;
    for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(i != j && p[i][j] != 0 && p[i][j] != 2) treci = 0;
    if(treci){
        return krugovi();
    }

    return dodva();
}

Compilation message

supertrees.cpp: In function 'int drvo()':
supertrees.cpp:50:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         for(int j = 0; j < sad.size()-1; j++){
      |                        ~~^~~~~~~~~~~~~~
supertrees.cpp: In function 'int krugovi()':
supertrees.cpp:78:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |         for(int j = 0; j < sad.size()-1; j++){
      |                        ~~^~~~~~~~~~~~~~
supertrees.cpp: In function 'int dodva()':
supertrees.cpp:165:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |             for(int h = 0; h < stb[cnt][j].size()-1; h++){
      |                            ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:185:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  185 |         for(int j = 1; j < stb[cnt].size(); j++){
      |                        ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 10 ms 1996 KB Output is correct
7 Correct 234 ms 25924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 10 ms 1996 KB Output is correct
7 Correct 234 ms 25924 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 11 ms 1996 KB Output is correct
13 Correct 239 ms 25908 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 6 ms 1612 KB Output is correct
17 Correct 144 ms 16048 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 60 ms 7748 KB Output is correct
21 Correct 243 ms 26028 KB Output is correct
22 Correct 230 ms 25960 KB Output is correct
23 Correct 286 ms 26004 KB Output is correct
24 Correct 234 ms 26012 KB Output is correct
25 Correct 123 ms 16068 KB Output is correct
26 Correct 121 ms 16036 KB Output is correct
27 Correct 243 ms 26028 KB Output is correct
28 Correct 232 ms 26024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 11 ms 1904 KB Output is correct
9 Correct 231 ms 26020 KB Output is correct
10 Correct 1 ms 284 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 10 ms 1996 KB Output is correct
13 Correct 234 ms 26076 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 6 ms 1612 KB Output is correct
17 Correct 115 ms 16044 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 264 KB Output is correct
21 Correct 63 ms 7748 KB Output is correct
22 Correct 237 ms 25992 KB Output is correct
23 Correct 230 ms 25904 KB Output is correct
24 Correct 278 ms 25924 KB Output is correct
25 Correct 102 ms 16068 KB Output is correct
26 Correct 101 ms 16044 KB Output is correct
27 Correct 239 ms 25920 KB Output is correct
28 Correct 243 ms 25924 KB Output is correct
29 Correct 98 ms 16048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 436 KB Output is correct
4 Correct 58 ms 7756 KB Output is correct
5 Correct 234 ms 25904 KB Output is correct
6 Correct 249 ms 26020 KB Output is correct
7 Correct 245 ms 25924 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 60 ms 7660 KB Output is correct
10 Correct 251 ms 25900 KB Output is correct
11 Correct 260 ms 25900 KB Output is correct
12 Correct 279 ms 25940 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 139 ms 7708 KB Output is correct
17 Correct 237 ms 26028 KB Output is correct
18 Correct 930 ms 26008 KB Output is correct
19 Correct 930 ms 25992 KB Output is correct
20 Correct 775 ms 26072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 10 ms 1996 KB Output is correct
7 Correct 234 ms 25924 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 11 ms 1996 KB Output is correct
13 Correct 239 ms 25908 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 6 ms 1612 KB Output is correct
17 Correct 144 ms 16048 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 60 ms 7748 KB Output is correct
21 Correct 243 ms 26028 KB Output is correct
22 Correct 230 ms 25960 KB Output is correct
23 Correct 286 ms 26004 KB Output is correct
24 Correct 234 ms 26012 KB Output is correct
25 Correct 123 ms 16068 KB Output is correct
26 Correct 121 ms 16036 KB Output is correct
27 Correct 243 ms 26028 KB Output is correct
28 Correct 232 ms 26024 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 11 ms 1904 KB Output is correct
37 Correct 231 ms 26020 KB Output is correct
38 Correct 1 ms 284 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 10 ms 1996 KB Output is correct
41 Correct 234 ms 26076 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 6 ms 1612 KB Output is correct
45 Correct 115 ms 16044 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 264 KB Output is correct
49 Correct 63 ms 7748 KB Output is correct
50 Correct 237 ms 25992 KB Output is correct
51 Correct 230 ms 25904 KB Output is correct
52 Correct 278 ms 25924 KB Output is correct
53 Correct 102 ms 16068 KB Output is correct
54 Correct 101 ms 16044 KB Output is correct
55 Correct 239 ms 25920 KB Output is correct
56 Correct 243 ms 25924 KB Output is correct
57 Correct 98 ms 16048 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 5 ms 1484 KB Output is correct
61 Correct 105 ms 15984 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 151 ms 7784 KB Output is correct
66 Correct 649 ms 16108 KB Output is correct
67 Correct 142 ms 16108 KB Output is correct
68 Correct 808 ms 16172 KB Output is correct
69 Correct 672 ms 16028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 10 ms 1996 KB Output is correct
7 Correct 234 ms 25924 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 11 ms 1996 KB Output is correct
13 Correct 239 ms 25908 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 6 ms 1612 KB Output is correct
17 Correct 144 ms 16048 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 60 ms 7748 KB Output is correct
21 Correct 243 ms 26028 KB Output is correct
22 Correct 230 ms 25960 KB Output is correct
23 Correct 286 ms 26004 KB Output is correct
24 Correct 234 ms 26012 KB Output is correct
25 Correct 123 ms 16068 KB Output is correct
26 Correct 121 ms 16036 KB Output is correct
27 Correct 243 ms 26028 KB Output is correct
28 Correct 232 ms 26024 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 11 ms 1904 KB Output is correct
37 Correct 231 ms 26020 KB Output is correct
38 Correct 1 ms 284 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 10 ms 1996 KB Output is correct
41 Correct 234 ms 26076 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 6 ms 1612 KB Output is correct
45 Correct 115 ms 16044 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 264 KB Output is correct
49 Correct 63 ms 7748 KB Output is correct
50 Correct 237 ms 25992 KB Output is correct
51 Correct 230 ms 25904 KB Output is correct
52 Correct 278 ms 25924 KB Output is correct
53 Correct 102 ms 16068 KB Output is correct
54 Correct 101 ms 16044 KB Output is correct
55 Correct 239 ms 25920 KB Output is correct
56 Correct 243 ms 25924 KB Output is correct
57 Correct 98 ms 16048 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 2 ms 436 KB Output is correct
61 Correct 58 ms 7756 KB Output is correct
62 Correct 234 ms 25904 KB Output is correct
63 Correct 249 ms 26020 KB Output is correct
64 Correct 245 ms 25924 KB Output is correct
65 Correct 1 ms 332 KB Output is correct
66 Correct 60 ms 7660 KB Output is correct
67 Correct 251 ms 25900 KB Output is correct
68 Correct 260 ms 25900 KB Output is correct
69 Correct 279 ms 25940 KB Output is correct
70 Correct 1 ms 332 KB Output is correct
71 Correct 1 ms 332 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 139 ms 7708 KB Output is correct
74 Correct 237 ms 26028 KB Output is correct
75 Correct 930 ms 26008 KB Output is correct
76 Correct 930 ms 25992 KB Output is correct
77 Correct 775 ms 26072 KB Output is correct
78 Correct 1 ms 332 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 5 ms 1484 KB Output is correct
81 Correct 105 ms 15984 KB Output is correct
82 Correct 1 ms 332 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 151 ms 7784 KB Output is correct
86 Correct 649 ms 16108 KB Output is correct
87 Correct 142 ms 16108 KB Output is correct
88 Correct 808 ms 16172 KB Output is correct
89 Correct 672 ms 16028 KB Output is correct
90 Correct 1 ms 332 KB Output is correct
91 Correct 1 ms 332 KB Output is correct
92 Correct 6 ms 1616 KB Output is correct
93 Correct 103 ms 17936 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 1 ms 332 KB Output is correct
96 Correct 1 ms 332 KB Output is correct
97 Correct 139 ms 5760 KB Output is correct
98 Correct 697 ms 17992 KB Output is correct
99 Correct 715 ms 18060 KB Output is correct
100 Correct 842 ms 18064 KB Output is correct
101 Correct 698 ms 18064 KB Output is correct
102 Correct 666 ms 18064 KB Output is correct
103 Correct 685 ms 18080 KB Output is correct
104 Correct 112 ms 17960 KB Output is correct
105 Correct 669 ms 18064 KB Output is correct