답안 #412385

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
412385 2021-05-26T19:10:19 Z kimbj0709 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
374 ms 28516 KB
#include "supertrees.h"
#include<bits/stdc++.h>
using namespace std;
void build(vector<vector<int> > ans);
#define maxn 1050
vector<set<int> > adj(maxn);
int cnt[maxn][maxn];
unordered_set<int> set1;
void merge(vector<int> &vect1,vector<int> &vect2){
  for(auto k:vect2){
    vect1.push_back(k);
  }
  vect2.clear();
}
void dfs(int node,int parent,int startnode){
  set1.insert(node);
  //cout << node << " " << parent << " " << startnode << endl;
  cnt[startnode][node]++;
  for(auto k:adj[node]){
    if(set1.find(k)!=set1.end()){
      continue;
    }
    dfs(k,node,startnode);
  }
  set1.erase(node);
}
int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
  std::vector<std::vector<int>> answer;
	for (int i = 0; i < n; i++) {
		std::vector<int> row(n,0);
		answer.push_back(row);
	}
  vector<vector<int> > vect1(n);
  vector<vector<int> > vect2(n);
  for(int i=0;i<n;i++){
    vect1[i].push_back(i);
  }
  for(int i=0;i<n;i++){
    for(int j=i+1;j<n;j++){
      if(p[i][j]==1&&vect1[i].size()!=0&&vect1[j].size()!=0){
        merge(vect1[i],vect1[j]);
        answer[i][j] = 1;
        answer[j][i] = 1;
        adj[i].insert(j);
        adj[j].insert(i);
      }
    }
  }
  for(int i=0;i<n;i++){
    if(vect1[i].size()!=0){
      vect2[i].push_back(i);
    }
  }
  for(int i=0;i<n;i++){
    for(int j=i+1;j<n;j++){
      if(p[i][j]==2&&vect2[i].size()!=0&&vect2[j].size()!=0){
        merge(vect2[i],vect2[j]);
      }
    }
  }
  for(int i=0;i<n;i++){
    if(vect2[i].size()>=2){
      for(int j=1;j<vect2[i].size();j++){
        answer[vect2[i][j-1]][vect2[i][j]] = 1;
        answer[vect2[i][j]][vect2[i][j-1]] = 1;
        adj[vect2[i][j-1]].insert(vect2[i][j]);
        adj[vect2[i][j]].insert(vect2[i][j-1]);
      }
      answer[vect2[i][0]][vect2[i][vect2[i].size()-1]] = 1;
      answer[vect2[i][vect2[i].size()-1]][vect2[i][0]] = 1;
      adj[vect2[i][0]].insert(vect2[i][vect2[i].size()-1]);
      adj[vect2[i][vect2[i].size()-1]].insert(vect2[i][0]);
    }
  }

  /*for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cout << answer[i][j] << " ";
    }
    cout << endl;
  }*/
  for(int i=0;i<n;i++){
    dfs(i,-1,i);
  }
  /*for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cout << cnt[i][j] << " ";
    }
    cout << endl;
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cout << answer[i][j] << " ";
    }
    cout << endl;
  }*/
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      if(cnt[i][j]!=p[i][j]){
        return 0;
      }
    }
  }
	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:64:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |       for(int j=1;j<vect2[i].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 1 ms 332 KB Output is correct
6 Correct 14 ms 2024 KB Output is correct
7 Correct 305 ms 26308 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 14 ms 2024 KB Output is correct
7 Correct 305 ms 26308 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 10 ms 2068 KB Output is correct
13 Correct 264 ms 26168 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 7 ms 1612 KB Output is correct
17 Correct 134 ms 16376 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 58 ms 7876 KB Output is correct
21 Correct 248 ms 26304 KB Output is correct
22 Correct 243 ms 26304 KB Output is correct
23 Correct 281 ms 26396 KB Output is correct
24 Correct 225 ms 26184 KB Output is correct
25 Correct 110 ms 16632 KB Output is correct
26 Correct 96 ms 16324 KB Output is correct
27 Correct 274 ms 26312 KB Output is correct
28 Correct 226 ms 26132 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 2100 KB Output is correct
9 Correct 226 ms 28092 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 356 KB Output is correct
12 Correct 16 ms 2164 KB Output is correct
13 Correct 374 ms 28456 KB Output is correct
14 Correct 1 ms 352 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 8 ms 1740 KB Output is correct
17 Correct 162 ms 18488 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 352 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 60 ms 8352 KB Output is correct
22 Correct 255 ms 28356 KB Output is correct
23 Correct 230 ms 28356 KB Output is correct
24 Correct 335 ms 28516 KB Output is correct
25 Correct 97 ms 18220 KB Output is correct
26 Correct 118 ms 18356 KB Output is correct
27 Correct 228 ms 28312 KB Output is correct
28 Correct 321 ms 28424 KB Output is correct
29 Correct 97 ms 18244 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 59 ms 7868 KB Output is correct
5 Correct 249 ms 26384 KB Output is correct
6 Correct 249 ms 26308 KB Output is correct
7 Correct 277 ms 26332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 60 ms 7992 KB Output is correct
10 Correct 253 ms 26440 KB Output is correct
11 Correct 227 ms 26312 KB Output is correct
12 Correct 317 ms 26356 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 60 ms 7852 KB Output is correct
17 Correct 246 ms 26428 KB Output is correct
18 Correct 249 ms 26316 KB Output is correct
19 Correct 241 ms 26396 KB Output is correct
20 Correct 232 ms 26164 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 14 ms 2024 KB Output is correct
7 Correct 305 ms 26308 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 10 ms 2068 KB Output is correct
13 Correct 264 ms 26168 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 7 ms 1612 KB Output is correct
17 Correct 134 ms 16376 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 58 ms 7876 KB Output is correct
21 Correct 248 ms 26304 KB Output is correct
22 Correct 243 ms 26304 KB Output is correct
23 Correct 281 ms 26396 KB Output is correct
24 Correct 225 ms 26184 KB Output is correct
25 Correct 110 ms 16632 KB Output is correct
26 Correct 96 ms 16324 KB Output is correct
27 Correct 274 ms 26312 KB Output is correct
28 Correct 226 ms 26132 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 2100 KB Output is correct
37 Correct 226 ms 28092 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 356 KB Output is correct
40 Correct 16 ms 2164 KB Output is correct
41 Correct 374 ms 28456 KB Output is correct
42 Correct 1 ms 352 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 8 ms 1740 KB Output is correct
45 Correct 162 ms 18488 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 352 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 60 ms 8352 KB Output is correct
50 Correct 255 ms 28356 KB Output is correct
51 Correct 230 ms 28356 KB Output is correct
52 Correct 335 ms 28516 KB Output is correct
53 Correct 97 ms 18220 KB Output is correct
54 Correct 118 ms 18356 KB Output is correct
55 Correct 228 ms 28312 KB Output is correct
56 Correct 321 ms 28424 KB Output is correct
57 Correct 97 ms 18244 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 11 ms 1868 KB Output is correct
61 Correct 184 ms 18416 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 59 ms 8408 KB Output is correct
66 Correct 119 ms 18360 KB Output is correct
67 Correct 120 ms 18372 KB Output is correct
68 Correct 105 ms 18340 KB Output is correct
69 Correct 98 ms 18216 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 14 ms 2024 KB Output is correct
7 Correct 305 ms 26308 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 10 ms 2068 KB Output is correct
13 Correct 264 ms 26168 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 7 ms 1612 KB Output is correct
17 Correct 134 ms 16376 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 58 ms 7876 KB Output is correct
21 Correct 248 ms 26304 KB Output is correct
22 Correct 243 ms 26304 KB Output is correct
23 Correct 281 ms 26396 KB Output is correct
24 Correct 225 ms 26184 KB Output is correct
25 Correct 110 ms 16632 KB Output is correct
26 Correct 96 ms 16324 KB Output is correct
27 Correct 274 ms 26312 KB Output is correct
28 Correct 226 ms 26132 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 2100 KB Output is correct
37 Correct 226 ms 28092 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 356 KB Output is correct
40 Correct 16 ms 2164 KB Output is correct
41 Correct 374 ms 28456 KB Output is correct
42 Correct 1 ms 352 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 8 ms 1740 KB Output is correct
45 Correct 162 ms 18488 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 352 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 60 ms 8352 KB Output is correct
50 Correct 255 ms 28356 KB Output is correct
51 Correct 230 ms 28356 KB Output is correct
52 Correct 335 ms 28516 KB Output is correct
53 Correct 97 ms 18220 KB Output is correct
54 Correct 118 ms 18356 KB Output is correct
55 Correct 228 ms 28312 KB Output is correct
56 Correct 321 ms 28424 KB Output is correct
57 Correct 97 ms 18244 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 59 ms 7868 KB Output is correct
62 Correct 249 ms 26384 KB Output is correct
63 Correct 249 ms 26308 KB Output is correct
64 Correct 277 ms 26332 KB Output is correct
65 Correct 1 ms 332 KB Output is correct
66 Correct 60 ms 7992 KB Output is correct
67 Correct 253 ms 26440 KB Output is correct
68 Correct 227 ms 26312 KB Output is correct
69 Correct 317 ms 26356 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 60 ms 7852 KB Output is correct
74 Correct 246 ms 26428 KB Output is correct
75 Correct 249 ms 26316 KB Output is correct
76 Correct 241 ms 26396 KB Output is correct
77 Correct 232 ms 26164 KB Output is correct
78 Correct 1 ms 332 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 11 ms 1868 KB Output is correct
81 Correct 184 ms 18416 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 59 ms 8408 KB Output is correct
86 Correct 119 ms 18360 KB Output is correct
87 Correct 120 ms 18372 KB Output is correct
88 Correct 105 ms 18340 KB Output is correct
89 Correct 98 ms 18216 KB Output is correct
90 Correct 1 ms 332 KB Output is correct
91 Correct 1 ms 332 KB Output is correct
92 Correct 9 ms 1740 KB Output is correct
93 Correct 161 ms 18348 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 1 ms 348 KB Output is correct
96 Correct 1 ms 332 KB Output is correct
97 Correct 26 ms 5956 KB Output is correct
98 Correct 117 ms 18360 KB Output is correct
99 Correct 117 ms 18368 KB Output is correct
100 Correct 113 ms 18364 KB Output is correct
101 Correct 96 ms 18216 KB Output is correct
102 Correct 103 ms 18244 KB Output is correct
103 Correct 164 ms 18476 KB Output is correct
104 Correct 141 ms 18344 KB Output is correct
105 Correct 105 ms 18248 KB Output is correct